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
-
#circle_ci_token ⇒ String?
Retrieve the stored CircleCI access token of the user.
-
#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_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
#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_file ⇒ Pathname
Returns The path of the configuration file.
75 76 77 |
# File 'lib/ci_runner/configuration/user.rb', line 75 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_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.
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 |