Skip to content

Commit

Permalink
Add profile support to app configuration (#6)
Browse files Browse the repository at this point in the history
* Rename the api_key field since the key may not, and probably won't, be a JWT.

* Replace 'config-rs' usage with our own custom config handling.

'config-rs' is handy, but we plan to restructure the configuration file in a way that does not align well with that project's design. Rather than fight it, we've opted to replace it.

* Break configuration loading up a bit to make testing easier.

* Split up the `config` module to make the code easier to follow.

* Make `ConfigFile` the primary entry point for loading configuration from YAML.

* Allow loading non-default profiles from the application config file.

* Add new CLI option for selecting the configuration profile to use.

* Make `config` responsible for providing default values.

* Split up code for loading settings from environment variables.

* Split up code for loading settings from config files.

Also change profile merging to be a functional operation since it makes testing easier and it's not on a hot enough path to worry about extra data copies.

* Add support for profile inheritance in the app config file.

This change also adds error handling for config file issues.

* Update config file template to work with profiles.

* Update docs for new config file format supporting multiple profiles.
  • Loading branch information
nirvdrum authored Nov 25, 2020
1 parent e874deb commit 427d13b
Show file tree
Hide file tree
Showing 9 changed files with 567 additions and 146 deletions.
144 changes: 32 additions & 112 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ clap = "2.33.3"
[dependencies]
clap = "2.33.3"
color-eyre = "0.5"
config = "0.10"
directories = "3.0"
edit = "0.1.2"
graphql_client = "0.9.0"
indoc = "1.0"
once_cell = "1.4.1"
reqwest = { version = "0.10.8", features = ["blocking", "json"] }
serde = "1.0.116"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
termcolor = "1.1"

[dev-dependencies]
assert_cmd = "1.0"
assert_matches = "1.4"
predicates = "1.0"
rusty-hook = "0.11.2"
serial_test = "0.5.0"
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@ The available configuration options are:

```yaml
---
api_key: <Your personal access token>
profiles:
default:
api_key: <Your personal access token>

another_profile:
source_profile: default
api_key: <Another personal access token>
```
Note that you can have multiple named profiles in your configuration, allowing you to maintain multiple sets of configuration fields in the configuration file.
Values can be inherited from one profile to another by way of the `source_profile` configuration field.
Profiles without an explicit `source_profile` configuration implicitly inherit from the _default_ profile.
You may choose which profile to use by passing the `--profile` to the CloudTruth CLI binary:

`cloudtruth --profile another-profile <subcommand>`

If the `--profile` argument is not supplied, the profile named _default_ will be used.

### Environment-based Configuration

The CloudTruth CLI utility maps all environment variables with the `CT_` followed by a configuration name to the same settings as would be available in the configuration file format.
Expand Down Expand Up @@ -72,12 +87,21 @@ To see the full list of supported shells, you can run:

All subcommands support a `--help` option to show you how the command should be invoked.

### Switching Active Configuration Profile

CloudTruth CLI profiles are a way of organizing the application's configuration data into multiple named groups.
Profiles, in this sense, are unrelated to configuration values in your CloudTruth account.
They simply allow you to configure the CloudTruth CLI for multiple organizations or multiple API keys with different access restrictions.
By default, the profile named _default_ will be used, but you can select the active profile with the `--profile` flag:

`cloudtruth --profile my-profile parameters get my_param`

### Switching Active CloudTruth Environment

By default, all commands will run against the _default_ CloudTruth environment.
To change the target environment, you can supply the global `--env` flag:

`cloudtruth --env=production parameters get my_param`
`cloudtruth --env production parameters get my_param`


Development
Expand Down
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ pub fn build_cli() -> App<'static, 'static> {
.help("The CloudTruth environment to work with")
.takes_value(true),
)
.arg(
Arg::with_name("profile")
.short("p")
.long("profile")
.help("The profile from the application configuration file to use")
.takes_value(true)
.default_value("default")
)
.subcommand(
SubCommand::with_name("completions")
.about("Generate shell completions for this application")
Expand Down
Loading

0 comments on commit 427d13b

Please sign in to comment.