From c820a8de88c3431dce935dcb981c35dfeddfe7cb Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 1 May 2020 18:43:15 -0400 Subject: [PATCH] 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. --- agent/config/builder.go | 5 ++--- agent/config/flags.go | 4 ++-- command/validate/validate.go | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/agent/config/builder.go b/agent/config/builder.go index 4fcdfce94..3110c5a14 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -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'") } diff --git a/agent/config/flags.go b/agent/config/flags.go index 3f1312182..8cc596a49 100644 --- a/agent/config/flags.go +++ b/agent/config/flags.go @@ -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.") diff --git a/command/validate/validate.go b/command/validate/validate.go index f85fc6422..fb3d55f47 100644 --- a/command/validate/validate.go +++ b/command/validate/validate.go @@ -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