This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
This repository was archived by the owner on Mar 24, 2022. It is now read-only.
config file support #9
Copy link
Copy link
Open
Labels
Description
The command line arguments being used in om sometimes represent large JSON payloads. For example, the configure-product --product-properties. This command has added support for a --config YAML file, so those command line arguments can be passed in -- over bash encoding a large JSON string.
We were investigating adding this behaviour to other commands on om, too. Since, --config file is mapping a file to command line arguments, we think jhanda might be a great place to centralize this logic.
Our team's proposal, is to have a special ConfigFile type for that if it exists on the struct will automatically load from the file.
Better to show than tell. If we were to write a test.
var options struct {
First string `long:"first" env:"FIRST"`
File ConfigFile `long:"config"`
}
_, err := jhanda.Parse(&options, []string{"--first", "hello"})
Expect(err).ToNot(HaveOccurred())
Expect(options.First).To(Equal("hello"))
// assume this file exists
// config.yaml
// ---
// first: world
_, err := jhanda.Parse(&options, []string{"--config", "config.yml"})
Expect(err).ToNot(HaveOccurred())
Expect(options.First).To(Equal("hello"))
_, err := jhanda.Parse(&options, []string{"--first", "testing", "--config", "config.yml"})
Expect(err).ToNot(HaveOccurred())
Expect(options.First).To(Equal("testing"))
// assume environment variable
// FIRST="I am env var"
_, err := jhanda.Parse(&options, []string{"--config", "config.yml"})
Expect(err).ToNot(HaveOccurred())
Expect(options.First).To(Equal("I am env var"))
// matching existing functionality: flag > env var
// assume environment variable
// FIRST="I am env var"
_, err := jhanda.Parse(&options, []string{"--first", "testing", "--config", "config.yml"})
Expect(err).ToNot(HaveOccurred())
Expect(options.First).To(Equal("testing"))
// so importance to set the param would be flag > env var > config file