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

support a non-aliased mode for lab with basic autocomplete #205

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "generate shell completion files",
Long: ``,
}

var bashCompletionCmd = &cobra.Command{
Use: "bash",
Short: "generate bash completion file",
Long: `To load completion run

. <(lab completion bash)

To configure your bash shell to load completions for each session add to your bashrc

# ~/.bashrc or ~/.profile
. <(lab completion bash)
`,
Run: func(cmd *cobra.Command, args []string) {
RootCmd.GenBashCompletion(os.Stdout)
},
}

var zshCompletionCmd = &cobra.Command{
Use: "zsh",
Short: "generate zsh completion file",
Long: `The author of lab has no idea how to load the generated zsh completion files, if you know how to load this output please open an issue at https://github.com/zaquestion/lab/issues and explain`,
Run: func(cmd *cobra.Command, args []string) {
RootCmd.GenZshCompletion(os.Stdout)
},
}

func init() {
completionCmd.AddCommand(bashCompletionCmd)
completionCmd.AddCommand(zshCompletionCmd)
RootCmd.AddCommand(completionCmd)
}
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "lab",
Short: "A Git Wrapper for GitLab",
Short: "Lab makes it simple to clone, fork, and interact with repositories on GitLab, including seamless workflows for creating merge requests, issues and snippets",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if ok, err := cmd.Flags().GetBool("version"); err == nil && ok {
Expand Down Expand Up @@ -58,6 +58,14 @@ func labUsageFormat(c *cobra.Command) string {
}

func helpFunc(cmd *cobra.Command, args []string) {
if !git.IsAliased {
cmd.Root().SetHelpFunc(nil)
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
return
}
// When help func is called from the help command args will be
// populated. When help is called with cmd.Help(), the args are not
// passed through, so we pick them up ourselves here
Expand Down
24 changes: 22 additions & 2 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -15,14 +16,33 @@ import (
gitconfig "github.com/tcnksm/go-gitconfig"
)

// IsHub is true when using "hub" as the git binary
var IsHub bool
var (
// IsHub is true when using "hub" as the git binary
IsHub bool
// IsAliased is true when git is aliased to lab
IsAliased bool
)

func init() {
_, err := exec.LookPath("hub")
if err == nil {
IsHub = true
}

out, err := exec.Command("ps", "-p", strconv.Itoa(os.Getppid()), "-o", "cmd=").CombinedOutput()
sh := strings.TrimSpace(string(out))
switch sh {
case "bash", "-bash":
out, err = exec.Command("bash", "-i", "-c", "type git").CombinedOutput()
if strings.HasSuffix(string(out), "git is aliased to `lab'\n") {
IsAliased = true
}
case "zsh", "-zsh":
out, err = exec.Command("zsh", "-i", "-c", "whence git").CombinedOutput()
if strings.HasSuffix(string(out), "lab\n") {
IsAliased = true
}
}
}

// New looks up the hub or git binary and returns a cmd which outputs to stdout
Expand Down