config: Make ConfigFormat not a pointer

The nil value was never used. We can avoid a bunch of complications by
making the field a string value instead of a pointer.

This change is in preparation for fixing a silent config failure.
This commit is contained in:
Daniel Nephin 2020-05-01 18:43:15 -04:00
parent b7b652e8c9
commit c820a8de88
3 changed files with 5 additions and 6 deletions

View File

@ -204,11 +204,10 @@ func (b *Builder) ReadFile(path string) (Source, error) {
// shouldParse file determines whether the file to be read is of a supported extension
func (b *Builder) shouldParseFile(path string) bool {
configFormat := b.stringVal(b.options.ConfigFormat)
srcFormat := FormatFrom(path)
// If config-format is not set, only read files with supported extensions
if configFormat == "" && srcFormat != "hcl" && srcFormat != "json" {
if b.options.ConfigFormat == "" && srcFormat != "hcl" && srcFormat != "json" {
return false
}
return true
@ -244,7 +243,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
// ----------------------------------------------------------------
// merge config sources as follows
//
configFormat := b.stringVal(b.options.ConfigFormat)
configFormat := b.options.ConfigFormat
if configFormat != "" && configFormat != "json" && configFormat != "hcl" {
return RuntimeConfig{}, fmt.Errorf("config: -config-format must be either 'hcl' or 'json'")
}

View File

@ -18,7 +18,7 @@ type BuilderOpts struct {
// ConfigFormat forces all config files to be interpreted as this
// format independent of their extension.
ConfigFormat *string
ConfigFormat string
// DevMode indicates whether the agent should be started in development
// mode. This cannot be configured in a config file.
@ -60,7 +60,7 @@ func AddFlags(fs *flag.FlagSet, f *BuilderOpts) {
add(&f.Config.CheckOutputMaxSize, "check_output_max_size", "Sets the maximum output size for checks on this agent")
add(&f.ConfigFiles, "config-dir", "Path to a directory to read configuration files from. This will read every file ending in '.json' as configuration in this directory in alphabetical order. Can be specified multiple times.")
add(&f.ConfigFiles, "config-file", "Path to a file in JSON or HCL format with a matching file extension. Can be specified multiple times.")
add(&f.ConfigFormat, "config-format", "Config files are in this format irrespective of their extension. Must be 'hcl' or 'json'")
fs.StringVar(&f.ConfigFormat, "config-format", "", "Config files are in this format irrespective of their extension. Must be 'hcl' or 'json'")
add(&f.Config.DataDir, "data-dir", "Path to a data directory to store agent state.")
add(&f.Config.Datacenter, "datacenter", "Datacenter of the agent.")
add(&f.Config.DefaultQueryTime, "default-query-time", "the amount of time a blocking query will wait before Consul will force a response. This value can be overridden by the 'wait' query parameter.")

View File

@ -51,7 +51,7 @@ func (c *cmd) Run(args []string) int {
return 1
}
b, err := config.NewBuilder(config.BuilderOpts{ConfigFiles: configFiles, ConfigFormat: &c.configFormat})
b, err := config.NewBuilder(config.BuilderOpts{ConfigFiles: configFiles, ConfigFormat: c.configFormat})
if err != nil {
c.UI.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
return 1