Class: CIRunner::Configuration::User
- Inherits:
-
Object
- Object
- CIRunner::Configuration::User
- 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
-
#buildkite_token(organization) ⇒ String?
Retrieve the stored Buildkite access token of the user that has access to the
organization
. -
#circle_ci_token ⇒ String?
Retrieve the stored CircleCI access token of the user.
-
#config_directory ⇒ Pathname
The path of the CI Runner directory configuration.
-
#config_file ⇒ Pathname
The path of the configuration file.
-
#github_token ⇒ String?
Retrieve the stored GitHub access token of the user.
-
#initialize ⇒ void
constructor
Singleton class.
-
#load! ⇒ void
Load the configuration of the user.
-
#save_buildkite_token(token, organization) ⇒ void
Write the Buildkite token to the user configuration file.
-
#save_circle_ci_token(token) ⇒ void
Write the Circle CI token to the user configuration file.
-
#save_github_token(token) ⇒ void
Write the GitHub token to the user configuration file.
-
#validate_token! ⇒ void
Ensure the user ran the ‘ci_runner github_token TOKEN` command prior to using CI Runner.
Constructor Details
#initialize ⇒ void
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
#buildkite_token(organization) ⇒ String?
Retrieve the stored Buildkite access token of the user that has access to the organization
.
74 75 76 |
# File 'lib/ci_runner/configuration/user.rb', line 74 def buildkite_token(organization) @yaml_config.dig("buildkite", "tokens", organization.downcase) end |
#circle_ci_token ⇒ String?
Retrieve the stored CircleCI access token of the user.
45 46 47 |
# File 'lib/ci_runner/configuration/user.rb', line 45 def circle_ci_token @yaml_config.dig("circle_ci", "token") end |
#config_directory ⇒ Pathname
Returns The path of the CI Runner directory configuration.
97 98 99 |
# File 'lib/ci_runner/configuration/user.rb', line 97 def config_directory config_file.dirname end |
#config_file ⇒ Pathname
Returns The path of the configuration file.
105 106 107 |
# File 'lib/ci_runner/configuration/user.rb', line 105 def config_file Pathname(File.(USER_CONFIG_PATH, Dir.home)) end |
#github_token ⇒ String?
Retrieve the stored GitHub access token of the user.
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_buildkite_token(token, organization) ⇒ void
This method returns an undefined value.
Write the Buildkite token to the user configuration file.
84 85 86 87 88 89 90 91 |
# File 'lib/ci_runner/configuration/user.rb', line 84 def save_buildkite_token(token, organization) existing_tokens = @yaml_config.dig("buildkite", "tokens") || {} existing_tokens[organization.downcase] = token @yaml_config["buildkite"] = { "tokens" => existing_tokens } save!(@yaml_config) end |
#save_circle_ci_token(token) ⇒ void
This method returns an undefined value.
Write the Circle CI token to the user configuration file
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
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.
118 119 120 121 122 123 124 125 126 |
# File 'lib/ci_runner/configuration/user.rb', line 118 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 |