Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve custom config flag handlers #5543

Merged
merged 1 commit into from
Nov 10, 2017
Merged

Conversation

urso
Copy link

@urso urso commented Nov 8, 2017

  • move support for flags collecting usage into a []string to libbeat/common
  • fix printing default value of -c flag
  • move flags manipulating config objects from config.go to flags.go
  • change flag namings to always end with xxxFlag
  • introduce unit tests for config flags
  • check the flags usage string in unit tests
  • fix potential panic when printing defaults on flag collecting into []string (nil pointer being passed)
  • fix printing defaults and setting types
  • add Type method improving cobra flag parameter in help message

Old flags usage messages for filebeat -h:

  -E, --E Flag                      Configuration overwrite (default null)
  -M, --M Flag                      Module configuration overwrite (default null)
  -N, --N                           Disable actual publishing for testing
  -c, --c argList                   Configuration file, relative to path.config (default beat.yml)
      --cpuprofile string           Write cpu profile to file
  -d, --d string                    Enable certain debug selectors
  -e, --e                           Log to stderr and disable syslog/file output
  -h, --help                        help for filebeat
      --httpprof string             Start pprof http server
      --memprofile string           Write memory profile to this file
      --modules string              List of enabled modules (comma separated)
      --once                        Run filebeat only once until all harvesters reach EOF
      --path.config flagOverwrite   Configuration path
      --path.data flagOverwrite     Data path
      --path.home flagOverwrite     Home path
      --path.logs flagOverwrite     Logs path
      --setup                       Load the sample Kibana dashboards
      --strict.perms                Strict permission checking on config files (default true)
  -v, --v                           Log at INFO level

New flags usage messages for filebeat -h:

  -E, --E setting=value      Configuration overwrite
  -M, --M setting=value      Module configuration overwrite
  -N, --N                    Disable actual publishing for testing
  -c, --c string             Configuration file, relative to path.config (default "filebeat.yml")
      --cpuprofile string    Write cpu profile to file
  -d, --d string             Enable certain debug selectors
  -e, --e                    Log to stderr and disable syslog/file output
  -h, --help                 help for filebeat
      --httpprof string      Start pprof http server
      --memprofile string    Write memory profile to this file
      --modules string       List of enabled modules (comma separated)
      --once                 Run filebeat only once until all harvesters reach EOF
      --path.config string   Configuration path
      --path.data string     Data path
      --path.home string     Home path
      --path.logs string     Logs path
      --setup                Load the sample Kibana dashboards
      --strict.perms         Strict permission checking on config files (default true)
  -v, --v                    Log at INFO level

@urso urso added the review label Nov 8, 2017
return f.List()
}

func (f *StringsFlag) List() []string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.List should have comment or be unexported

return nil
}

func (f *StringsFlag) Get() interface{} {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.Get should have comment or be unexported

}
}

func (f *StringsFlag) Set(v string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.Set should have comment or be unexported

return strings.Join(*f.list, ", ")
}

func (f StringsFlag) SetDefault(v string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.SetDefault should have comment or be unexported

return StringArrVarFlag(arr, name, usage)
}

func StringArrVarFlag(arr *[]string, name, usage string) *StringsFlag {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function StringArrVarFlag should have comment or be unexported

flag *flag.Flag
}

func StringArrFlag(name, def, usage string) *StringsFlag {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function StringArrFlag should have comment or be unexported

"strings"
)

type StringsFlag struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type StringsFlag should have comment or be unexported

@urso urso added in progress Pull request is currently in progress. and removed review labels Nov 8, 2017
Copy link
Contributor

@ph ph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Maybe fix the hound complains?

The failures seems to be unrelated.

--- FAIL: TestFetch (81.44s)
	Error Trace:	status_integration_test.go:20
	Error:		Received unexpected error "error making http request: Get http://kibana:5601/api/status: lookup kibana 

assert.Equal(t, test.expected, flag.List())
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wohoo tests :)

return *f.list
}

func (f *StringsFlag) Type() string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.Type should have comment or be unexported

return strings.Join(l, ", ")
}

func (f *StringsFlag) SetDefault(v string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.SetDefault should have comment or be unexported

return &StringsFlag{list: arr, isDefault: true}
}

func (f *StringsFlag) Register(fs *flag.FlagSet, name, usage string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.Register should have comment or be unexported

return f
}

func NewStringsFlag(arr *[]string) *StringsFlag {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function NewStringsFlag should have comment or be unexported

return *f.list
}

func (f *StringsFlag) Type() string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.Type should have comment or be unexported

return strings.Join(l, ", ")
}

func (f *StringsFlag) SetDefault(v string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.SetDefault should have comment or be unexported

return &StringsFlag{list: arr, isDefault: true}
}

func (f *StringsFlag) Register(fs *flag.FlagSet, name, usage string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method StringsFlag.Register should have comment or be unexported

return f
}

func NewStringsFlag(arr *[]string) *StringsFlag {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function NewStringsFlag should have comment or be unexported

return "setting=value"
}

func ConfigOverwriteFlag(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function ConfigOverwriteFlag should have comment or be unexported

return ""
}

func (f *SettingsFlag) Type() string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method SettingsFlag.Type should have comment or be unexported

return f.access().Set(s)
}

func (f *SettingsFlag) Get() interface{} {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method SettingsFlag.Get should have comment or be unexported

return fromConfig(f.access().Config())
}

func (f *SettingsFlag) Set(s string) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method SettingsFlag.Set should have comment or be unexported

return (*cfgflag.FlagValue)(f)
}

func (f *SettingsFlag) Config() *Config {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method SettingsFlag.Config should have comment or be unexported

fs.Var(f, name, usage)
}

func NewSettingsFlag(def *Config) *SettingsFlag {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function NewSettingsFlag should have comment or be unexported

return cfg
}

func SettingVarFlag(fs *flag.FlagSet, def *Config, name, usage string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function SettingVarFlag should have comment or be unexported

return "string"
}

func SettingFlag(fs *flag.FlagSet, name, usage string) *Config {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function SettingFlag should have comment or be unexported

return StringArrVarFlag(fs, arr, name, usage)
}

func StringArrVarFlag(fs *flag.FlagSet, arr *[]string, name, usage string) *StringsFlag {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function StringArrVarFlag should have comment or be unexported

@urso urso changed the title Move []string flag support to libbeat/common Improve custom config based flag handlers Nov 9, 2017
@urso urso changed the title Improve custom config based flag handlers Improve custom config flag handlers Nov 9, 2017
@urso urso added libbeat refactoring review and removed in progress Pull request is currently in progress. labels Nov 10, 2017
Copy link
Contributor

@ph ph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, waiting on CI to merge that.
+1 on refactoring
+1 on adding test
+100 for user facing improvement :)

@ph
Copy link
Contributor

ph commented Nov 10, 2017

@urso Would you mind squashing it?

@urso urso force-pushed the feature/common-flags branch from ff58fb3 to 138b48f Compare November 10, 2017 15:52
- move support for flags collecting usage into a []string to libbeat/common
- fix printing default value of `-c` flag
- move flags manipulating config objects from config.go to flags.go
- change flag namings to always end with xxxFlag
- introduce unit tests for config flags
- check the flags usage string in unit tests
- fix potential panic when printing defaults on flag collecting into []string (nil pointer being passed)
- fix printing defaults and setting types
 - add `Type` method improving cobra flag parameter in help message

Old flags usage messages for `filebeat -h`:
```
  -E, --E Flag                      Configuration overwrite (default null)
  -M, --M Flag                      Module configuration overwrite (default null)
  -N, --N                           Disable actual publishing for testing
  -c, --c argList                   Configuration file, relative to path.config (default beat.yml)
      --cpuprofile string           Write cpu profile to file
  -d, --d string                    Enable certain debug selectors
  -e, --e                           Log to stderr and disable syslog/file output
  -h, --help                        help for filebeat
      --httpprof string             Start pprof http server
      --memprofile string           Write memory profile to this file
      --modules string              List of enabled modules (comma separated)
      --once                        Run filebeat only once until all harvesters reach EOF
      --path.config flagOverwrite   Configuration path
      --path.data flagOverwrite     Data path
      --path.home flagOverwrite     Home path
      --path.logs flagOverwrite     Logs path
      --setup                       Load the sample Kibana dashboards
      --strict.perms                Strict permission checking on config files (default true)
  -v, --v                           Log at INFO level
```

New flags usage messages for `filebeat -h`:

```
  -E, --E setting=value      Configuration overwrite
  -M, --M setting=value      Module configuration overwrite
  -N, --N                    Disable actual publishing for testing
  -c, --c string             Configuration file, relative to path.config (default "filebeat.yml")
      --cpuprofile string    Write cpu profile to file
  -d, --d string             Enable certain debug selectors
  -e, --e                    Log to stderr and disable syslog/file output
  -h, --help                 help for filebeat
      --httpprof string      Start pprof http server
      --memprofile string    Write memory profile to this file
      --modules string       List of enabled modules (comma separated)
      --once                 Run filebeat only once until all harvesters reach EOF
      --path.config string   Configuration path
      --path.data string     Data path
      --path.home string     Home path
      --path.logs string     Logs path
      --setup                Load the sample Kibana dashboards
      --strict.perms         Strict permission checking on config files (default true)
  -v, --v                    Log at INFO level
```
@urso urso force-pushed the feature/common-flags branch from 138b48f to 21da625 Compare November 10, 2017 16:04
@ph ph merged commit 8ae5b4b into elastic:master Nov 10, 2017
@urso urso deleted the feature/common-flags branch February 19, 2019 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants