Merge pull request #1475 from shihanng/issue-1456__clone-flag-help

Provide better error message for gh repo clone
This commit is contained in:
Mislav Marohnić 2020-08-11 17:30:09 +02:00 committed by GitHub
commit 9f5daeca19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package clone
import (
"fmt"
"net/http"
"strings"
@ -13,6 +14,7 @@ import (
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type CloneOptions struct {
@ -33,16 +35,18 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
}
cmd := &cobra.Command{
Use: "clone <repository> [<directory>]",
DisableFlagsInUseLine: true,
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
Args: cobra.MinimumNArgs(1),
Short: "Clone a repository locally",
Long: heredoc.Doc(
`Clone a GitHub repository locally.
Long: heredoc.Doc(`
Clone a GitHub repository locally.
If the "OWNER/" portion of the "OWNER/REPO" repository argument is omitted, it
defaults to the name of the authenticating user.
To pass 'git clone' flags, separate them with '--'.
Pass additional 'git clone' flags by listing them after '--'.
`),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Repository = args[0]
@ -56,6 +60,13 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
},
}
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
}
return &cmdutil.FlagError{Err: fmt.Errorf("%w\nSeparate git clone flags with '--'.", err)}
})
return cmd
}

View file

@ -179,3 +179,10 @@ func Test_RepoClone_withoutUsername(t *testing.T) {
assert.Equal(t, 1, cs.Count)
assert.Equal(t, "git clone https://github.com/OWNER/REPO.git", strings.Join(cs.Calls[0].Args, " "))
}
func Test_RepoClone_flagError(t *testing.T) {
_, err := runCloneCommand(nil, "--depth 1 OWNER/REPO")
if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." {
t.Errorf("unexpected error %v", err)
}
}