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

Add help statement for cli #460

Merged
merged 9 commits into from
Nov 3, 2020
Merged

Add help statement for cli #460

merged 9 commits into from
Nov 3, 2020

Conversation

yageek
Copy link
Contributor

@yageek yageek commented Oct 21, 2020

Refers to #372

@coveralls
Copy link

coveralls commented Oct 21, 2020

Pull Request Test Coverage Report for Build 917

  • 0 of 72 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.4%) to 55.205%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/cli/main.go 0 72 0.0%
Totals Coverage Status
Change from base Build 901: -0.4%
Covered Lines: 3044
Relevant Lines: 5514

💛 - Coveralls

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@@ -145,6 +163,11 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n")
}

case "goto":
if flag.Arg(1) == helpArgument {
Copy link
Member

Choose a reason for hiding this comment

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

Use a flag.NewFlagSet() to parse the the argument. Can you create create a new method that creates a FlagSet with -help argument parsing and use that for all of the sub-commands?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the PR

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my feedback! Some of the code is repetitive and can be refactored.

if err := createFlagSet.Parse(args); err != nil {
log.Println(err)
}

if *help {
log.Println(createUsage)
Copy link
Member

Choose a reason for hiding this comment

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

Use fmt.Fprintln(os.Stderr, ...) for help messages so they're not modified when -verbose is specified.

if err := createFlagSet.Parse(args); err != nil {
log.Println(err)
}

if *help {
Copy link
Member

Choose a reason for hiding this comment

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

Factor this out into a common function: func handleSubCmdHelp(help bool, usage string, flagSet flag.FlagSet)

@@ -145,11 +176,25 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n")
}

case "goto":

gotoSet := flag.NewFlagSet("goto", flag.ExitOnError)
Copy link
Member

Choose a reason for hiding this comment

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

Factor this out into a common function that creates a new FlagSet with a help flag: func newFlagSetWithHelp(name string, errHandling flag.ErrorHandling) (*FlagSet, bool)

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Thanks again for the PR and for addressing my feedback!

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Fix linter issues


func handleSubCmdHelp(help bool, usage string, flagSet *flag.FlagSet) {
if help {
fmt.Fprintln(os.Stderr, createUsage)
Copy link
Member

Choose a reason for hiding this comment

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

createUsage => usage

os.Exit(0)
}
}
func newFlagSetWithHelp(name string, errHandling flag.ErrorHandling) (*flag.FlagSet, *bool) {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the errHandling param is unused.

Might as well return bool instead of *bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I have to return a pointer to a bool otherwise the value will always be the one at the creation and I will not get updated after flag has finished to parse the arguement.

Copy link
Member

Choose a reason for hiding this comment

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

Remove the errHandling param since it's always being passed the same value, which triggers the unparam linter:

internal/cli/main.go:41:38: `newFlagSetWithHelp` - `errHandling` always receives `flag.ExitOnError` (`1`) (unparam)
func newFlagSetWithHelp(name string, errHandling flag.ErrorHandling) (*flag.FlagSet, *bool) {
                                     ^

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Thanks for the fixes. Noticed one more issue now that the diff is much smaller.

if err := downFlagSet.Parse(args); err != nil {
log.fatalErr(err)
log.Println(err)
Copy link
Member

Choose a reason for hiding this comment

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

We should be consistent about how parse failures are handled in sub-commands. Previously, both the down and drop sub-commands would log.fatalErr so let's continue doing that.

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Other flagset parsing that need to be updated to log.fatalErr

extPtr := createFlagSet.String("ext", "", "File extension")
dirPtr := createFlagSet.String("dir", "", "Directory to place file in (default: current working directory)")
formatPtr := createFlagSet.String("format", defaultTimeFormat, `The Go time format string to use. If the string "unix" or "unixNano" is specified, then the seconds or nanoseconds since January 1, 1970 UTC respectively will be used. Caution, due to the behavior of time.Time.Format(), invalid format strings will not error`)
createFlagSet.BoolVar(&seq, "seq", seq, "Use sequential numbers instead of timestamps (default: false)")
createFlagSet.IntVar(&seqDigits, "digits", seqDigits, "The number of digits to use in sequences (default: 6)")

if err := createFlagSet.Parse(args); err != nil {
log.Println(err)
Copy link
Member

Choose a reason for hiding this comment

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

log.fatalErr

gotoSet, helpPtr := newFlagSetWithHelp("goto")

if err := gotoSet.Parse(args); err != nil {
log.Println(err)
Copy link
Member

Choose a reason for hiding this comment

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

log.fatalErr

upSet, helpPtr := newFlagSetWithHelp("up")

if err := upSet.Parse(args); err != nil {
log.Println(err)
Copy link
Member

Choose a reason for hiding this comment

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

log.fatalErr

forceSet, helpPtr := newFlagSetWithHelp("force")

if err := forceSet.Parse(args); err != nil {
log.Println(err)
Copy link
Member

Choose a reason for hiding this comment

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

log.fatalErr

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Thanks again for the PR and for addressing all of the feedback!

@dhui dhui merged commit 8a56273 into golang-migrate:master Nov 3, 2020
Comment on lines -175 to +232
if flag.Arg(1) != "" {
if flag.NArg() > 0 {

Choose a reason for hiding this comment

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

This breaks the CLI using migrate up (sans limit argument) because flag.NArg() is always > 0 because flag.Arg(0) is always up

Copy link
Member

Choose a reason for hiding this comment

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

See: #467

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants