Class: CIRunner::Configuration::User

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ci_runner/configuration/user.rb

Overview

Class to interact with the user’s configuration. The configuration is used to store the GitHub token amonst other things.

Constant Summary collapse

USER_CONFIG_PATH =
".ci_runner/config.yml"

Instance Method Summary collapse

Constructor Details

#initializevoid

Singleton class. This should/can’t be called directly.



22
23
24
# File 'lib/ci_runner/configuration/user.rb', line 22

def initialize
  load!
end

Instance Method Details

#circle_ci_tokenString?

Retrieve the stored CircleCI access token of the user.

Returns:

  • (String, nil)

    Depending if the user ran the ‘ci_runner circle_ci_token TOKEN` command.



45
46
47
# File 'lib/ci_runner/configuration/user.rb', line 45

def circle_ci_token
  @yaml_config.dig("circle_ci", "token")
end

#config_filePathname

Returns The path of the configuration file.

Examples:

puts config_file # ~/.ci_runner/config.yml

Returns:

  • (Pathname)

    The path of the configuration file.



75
76
77
# File 'lib/ci_runner/configuration/user.rb', line 75

def config_file
  Pathname(File.expand_path(USER_CONFIG_PATH, Dir.home))
end

#github_tokenString?

Retrieve the stored GitHub access token of the user.

Returns:

  • (String, nil)

    Depending if the user ran the ‘ci_runner github_token TOKEN` command.



38
39
40
# File 'lib/ci_runner/configuration/user.rb', line 38

def github_token
  @yaml_config.dig("github", "token")
end

#load!void

This method returns an undefined value.

Load the configuration of the user. If it doesn’t exist, write an empty one.



29
30
31
32
33
# File 'lib/ci_runner/configuration/user.rb', line 29

def load!
  save!({}) unless config_file.exist?

  @yaml_config = YAML.load_file(config_file)
end

#save_circle_ci_token(token) ⇒ void

This method returns an undefined value.

Write the Circle CI token to the user configuration file

Parameters:

  • token (String)

    A valid Circle CI access token.



65
66
67
68
69
# File 'lib/ci_runner/configuration/user.rb', line 65

def save_circle_ci_token(token)
  @yaml_config["circle_ci"] = { "token" => token }

  save!(@yaml_config)
end

#save_github_token(token) ⇒ void

This method returns an undefined value.

Write the GitHub token to the user configuration file

Parameters:

  • token (String)

    A valid GitHub access token.



54
55
56
57
58
# File 'lib/ci_runner/configuration/user.rb', line 54

def save_github_token(token)
  @yaml_config["github"] = { "token" => token }

  save!(@yaml_config)
end

#validate_token!void

This method returns an undefined value.

Ensure the user ran the ‘ci_runner github_token TOKEN` command prior to using CI Runner.

Note: Technically, it’s possible to access the GitHub API to retrieve checks and download logs on public repositories, but on private repositories the error GitHub sends back is a 404 which can be confusing, so I’d rather just make sure the token exists either way.

Raises:

  • (Error)

    If the user tries to run ‘ci_runner rerun` before it saved a token in its config file.



88
89
90
91
92
93
94
95
96
# File 'lib/ci_runner/configuration/user.rb', line 88

def validate_token!
  return if github_token

  raise(Error, <<~EOM)
    A GitHub token needs to be saved into your configuration before being able to use CI Runner.

    Have a look at the {{command:ci_runner help github_token}} command.
  EOM
end