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 --help output #436

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/config/cmdoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ type Options struct {
Measurements MeasurementOpts `group:"Measurements"`
Logging LoggingOpts `group:"Logging"`
WebUI WebUIOpts `group:"WebUI"`
Version bool `long:"version" mapstructure:"version" description:"Show Git build version and exit" env:"PW3_VERSION"`
Ping bool `long:"ping" mapstructure:"ping" description:"Try to connect to all configured DB-s, report errors and then exit" env:"PW3_PING"`
Help bool
}
Expand All @@ -86,13 +85,13 @@ func New(writer io.Writer) (*Options, error) {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
cmdOpts.Help = true
}
if !flags.WroteHelp(err) {
parser.WriteHelp(writer)
}
}
if err == nil {
err = validateConfig(cmdOpts)
}
if !flags.WroteHelp(err) {
parser.WriteHelp(writer)
}
return cmdOpts, err
}

Expand All @@ -101,11 +100,6 @@ func (c Options) Verbose() bool {
return c.Logging.LogLevel == "debug"
}

// VersionOnly returns true if the `--version` is the only argument
func (c Options) VersionOnly() bool {
return len(os.Args) == 2 && c.Version
}

func (c Options) GetConfigKind() (_ Kind, err error) {
if _, err := pgx.ParseConfig(c.Sources.Config); err == nil {
return Kind(ConfigPgURL), nil
Expand All @@ -121,9 +115,6 @@ func (c Options) GetConfigKind() (_ Kind, err error) {
}

func validateConfig(c *Options) error {
if c.Sources.Config == "" && !c.VersionOnly() {
return errors.New("--config was not specified")
}
if c.Sources.Refresh <= 1 {
return errors.New("--servers-refresh-loop-seconds must be greater than 1")
}
Expand Down
7 changes: 4 additions & 3 deletions src/config/cmdoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ func TestParseFail(t *testing.T) {

func TestParseSuccess(t *testing.T) {
tests := [][]string{
{0: "go-test", "--version"},
{0: "go-test", "--help"},
}
for _, d := range tests {
os.Args = d
_, err := New(nil)
assert.NoError(t, err)
c, err := New(nil)
assert.True(t, c.Help)
assert.Error(t, err)
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,11 @@ func main() {
defer cancel()

if opts, err = config.New(os.Stdout); err != nil {
printVersion()
fmt.Println(err)
if !opts.Help {
exitCode.Store(ExitCodeConfigError)
}
fmt.Println(err)
return
}
if opts.VersionOnly() || opts.Help {
printVersion()
return
}
logger = log.Init(opts.Logging)
Expand Down
Loading