Skip to content

Improve mackerel-agent configtest #810

@wafuwafu13

Description

@wafuwafu13

Now, we run resolveConfig to do configtest and detect Invalid syntax (config_test.go shows that).
But, we can't detect unexpected key, so I want to improve by using Undecoded().
It would be better if it could suggest intended key.

This is sample of mackerel-agent.conf which includes unexpected key.

1  apikey = "abcdefg"
2  podfile = "/path/to/pidfile"
3 
4  [foo]
5  command = "bar"
6 
7  [plugins.checks.foo]
8  command = "bar"
9 
10 [plugin.check.foo]
11 command = "bar"

Actual behavior

$ mackerel-agent configtest
/path/to/mackerel-agent.conf Syntax OK

Expected behavior

$ mackerel-agent configtest
warn: podfile is invalid key. Do you mean pidfile ?(line 2)
warn: foo is invalid key (line 4)
warn: plugins.checks.foo is invalid key. Do you mean plugin.checks.foo ? (line 7)
warn: plugin.check.foo is invalid key. Do you mean plugin.checks.foo ? (line 10)

Implementation image

Create config/validate.go#ValidateConfigFile and use it in commands.go#doConfigtest

func doConfigtest(fs *flag.FlagSet, argv []string) error {
	conf, err := resolveConfig(fs, argv)
	if err != nil {
		return fmt.Errorf("failed to test config: %s", err)
	}
	warnings := config.ValidateConfigFile(conf.Conffile)
	if warnings != nil {
                 return <display warnings>
        }
        fmt.Fprintf(os.Stderr, "%s Syntax OK\n", conf.Conffile)
	return nil
}

Implementation stage

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions