Skip to content

Commit

Permalink
[#107] (bug) align lab help functionality with git help and --help
Browse files Browse the repository at this point in the history
lab should be wrapping git help functionality and only showing its on when appropriate. The bulk of these changes address the challenges in doing so
  • Loading branch information
zaquestion committed Feb 17, 2018
1 parent 5ed79f2 commit 5ee2cc1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var cloneCmd = &cobra.Command{
log.Fatal(err)
}

// Clone project was a fork belonging to the user so user is
// Clone project was a fork belonging to the user; user is
// treating forks as origin. Add upstream as remoted pointing
// to forked from repo
if project.ForkedFromProject != nil &&
Expand Down
68 changes: 53 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,7 @@ var RootCmd = &cobra.Command{
Short: "A Git Wrapper for GitLab",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
formatChar := "\n"
if git.IsHub {
formatChar = ""
}

git := git.New()
git.Stdout = nil
git.Stderr = nil
usage, _ := git.CombinedOutput()
fmt.Printf("%s%sThese GitLab commands are provided by lab:\n%s\n\n", string(usage), formatChar, labUsage(cmd))
helpCmd.Run(cmd, args)
},
}

Expand All @@ -55,7 +46,7 @@ var templateFuncs = template.FuncMap{
const labUsageTmpl = `{{range .Commands}}{{if (and (or .IsAvailableCommand (ne .Name "help")) (and (ne .Name "clone") (ne .Name "version") (ne .Name "ci")))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}`

func labUsage(c *cobra.Command) string {
func labUsageFormat(c *cobra.Command) string {
t := template.New("top")
t.Funcs(templateFuncs)
template.Must(t.Parse(labUsageTmpl))
Expand All @@ -68,6 +59,50 @@ func labUsage(c *cobra.Command) string {
return buf.String()
}

func helpFunc(cmd *cobra.Command, args []string) {
if len(args) == 0 {
args = os.Args[1:]
}
rootCmd := cmd.Root()
if cmd, _, err := rootCmd.Find(args); err == nil && cmd != rootCmd {
// Cobra will check parent commands for a helpFunc and we only
// want the root command to actually use this custom help func.
// Here we trick cobra into thinking that there is no help func
// so it will use the default help for the subcommands
cmd.Root().SetHelpFunc(nil)
err2 := cmd.Help()
if err2 != nil {
log.Fatal(err)
}
return
}
formatChar := "\n"
if git.IsHub {
formatChar = ""
}

git := git.New()
git.Stdout = nil
git.Stderr = nil
usage, _ := git.CombinedOutput()
fmt.Printf("%s%sThese GitLab commands are provided by lab:\n%s\n\n", string(usage), formatChar, labUsageFormat(cmd.Root()))
}

var helpCmd = &cobra.Command{
Use: "help",
Short: "Show the help for lab",
Long: ``,
Run: helpFunc,
}

func init() {
// NOTE: Calling SetHelpCommand like this causes helpFunc to be called
// with correct arguments. If the default cobra help func is used no
// arguments are passed through and subcommand help breaks.
RootCmd.SetHelpCommand(helpCmd)
RootCmd.SetHelpFunc(helpFunc)
}

// parseArgsRemote returns the remote and a number if parsed. Many commands
// accept a remote to operate on and number such as a page id
func parseArgsRemote(args []string) (string, int64, error) {
Expand Down Expand Up @@ -138,9 +173,13 @@ func Execute() {
forkRemote = lab.User()
}
}
// Check if the user is calling a lab command or if we should passthrough
// NOTE: The help command won't be found by Find, which we are counting on
if cmd, _, err := RootCmd.Find(os.Args[1:]); err != nil || cmd.Use == "clone" {
// Determine if any undefined flags were passed to "clone"
if cmd.Use == "clone" && len(os.Args) > 2 {
// TODO: Evaluate and support some of these flags
// NOTE: `hub help -a` wraps the `git help -a` output
if (cmd.Use == "clone" && len(os.Args) > 2) || os.Args[1] == "help" {
// ParseFlags will err in these cases
err = cmd.ParseFlags(os.Args[1:])
if err == nil {
Expand All @@ -152,9 +191,8 @@ func Execute() {
}
}

// Passthrough to git for any unrecognised commands
git := git.New(os.Args[1:]...)
err = git.Run()
// Passthrough to git for any unrecognized commands
err = git.New(os.Args[1:]...).Run()
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
Expand Down

0 comments on commit 5ee2cc1

Please sign in to comment.