Skip to content

Commit

Permalink
updated shell completion
Browse files Browse the repository at this point in the history
- uses github.com/rsteube/carapace
- thus no need for cobra fork anymore
- replaced completion_function.go with go callbacks
- provides shell completion for bash, elvish, fish, powershell and zsh
  • Loading branch information
rsteube committed Aug 20, 2020
1 parent 6f24bc8 commit 7bebe48
Show file tree
Hide file tree
Showing 41 changed files with 722 additions and 342 deletions.
4 changes: 4 additions & 0 deletions cmd/ci_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -35,4 +36,7 @@ var ciLintCmd = &cobra.Command{

func init() {
ciCmd.AddCommand(ciLintCmd)
carapace.Gen(ciLintCmd).PositionalCompletion(
carapace.ActionFiles(".gitlab-ci.yml"),
)
}
10 changes: 8 additions & 2 deletions cmd/ci_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -131,14 +133,18 @@ func parseCIVariables(vars []string) (map[string]string, error) {
}

func init() {
ciCreateCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote_branches origin")
ciCreateCmd.Flags().StringP("project", "p", "", "Project to create pipeline on")
ciCmd.AddCommand(ciCreateCmd)
carapace.Gen(ciCreateCmd).PositionalCompletion(
action.Remotes(),
)

ciTriggerCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote_branches")
ciTriggerCmd.Flags().StringP("project", "p", "", "Project to run pipeline trigger on")
ciTriggerCmd.Flags().StringP("token", "t", os.Getenv("CI_JOB_TOKEN"), "Pipeline trigger token, optional if run within GitLabCI")
ciTriggerCmd.Flags().StringSliceP("variable", "v", []string{}, "Variables to pass to pipeline")

ciCmd.AddCommand(ciTriggerCmd)
carapace.Gen(ciTriggerCmd).PositionalCompletion(
action.RemoteBranches(-1),
)
}
8 changes: 7 additions & 1 deletion cmd/ci_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"time"

"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand Down Expand Up @@ -91,7 +93,11 @@ lab ci status --wait`,
}

func init() {
ciStatusCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote_branches")
ciStatusCmd.Flags().Bool("wait", false, "Continuously print the status and wait to exit until the pipeline finishes. Exit code indicates pipeline status")
ciCmd.AddCommand(ciStatusCmd)

carapace.Gen(ciStatusCmd).PositionalCompletion(
action.Remotes(),
action.RemoteBranches(0),
)
}
8 changes: 6 additions & 2 deletions cmd/ci_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"time"

"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -106,7 +108,9 @@ func doTrace(ctx context.Context, w io.Writer, pid interface{}, sha, name string
}

func init() {
ciTraceCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
ciTraceCmd.MarkZshCompPositionalArgumentCustom(2, "__lab_completion_remote_branches $words[2]")
ciCmd.AddCommand(ciTraceCmd)
carapace.Gen(ciTraceCmd).PositionalCompletion(
action.Remotes(),
action.RemoteBranches(0),
)
}
8 changes: 6 additions & 2 deletions cmd/ci_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"github.com/gdamore/tcell"
"github.com/pkg/errors"
"github.com/rivo/tview"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"

"github.com/lunixbochs/vtclean"
gitlab "github.com/xanzy/go-gitlab"

"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand Down Expand Up @@ -623,7 +625,9 @@ func latestJobs(jobs []*gitlab.Job) []*gitlab.Job {
}

func init() {
ciViewCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
ciViewCmd.MarkZshCompPositionalArgumentCustom(2, "__lab_completion_remote_branches $words[2]")
ciCmd.AddCommand(ciViewCmd)
carapace.Gen(ciViewCmd).PositionalCompletion(
action.Remotes(),
action.RemoteBranches(0),
)
}
25 changes: 13 additions & 12 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package cmd

import (
"os"
"fmt"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates the shell autocompletion",
Long: `'completion bash' generates the bash and 'completion zsh' the zsh autocompletion`,
Short: "Generates the shell autocompletion [bash, elvish, fish, powershell, zsh]",
Long: `Generates the shell autocompletion [bash, elvish, fish, powershell, zsh]
Most scripts can be direcly sourced (though using pre-generated versions is recommended):
bash : source <(lab completion bash)
elvish : lab completion elvish > lab.elv; -source lab.elv
fish : lab completion fish | source
powershell : lab completion powershell | Out-String | Invoke-Expression
zsh : source <(lab completion zsh)`,
Args: cobra.ExactArgs(1),
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{"bash", "elvish", "fish", "powershell", "zsh"},
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
RootCmd.GenBashCompletion(os.Stdout)
case "zsh":
RootCmd.GenZshCompletion(os.Stdout)
default:
println("only 'bash' or 'zsh' allowed")
}
fmt.Println(carapace.Gen(cmd).Snippet(args[0]))
},
}

Expand Down
41 changes: 0 additions & 41 deletions cmd/completion_function.go

This file was deleted.

8 changes: 6 additions & 2 deletions cmd/issue_browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"log"
"strconv"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/browser"
"github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -42,7 +44,9 @@ var issueBrowseCmd = &cobra.Command{
}

func init() {
issueBrowseCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
issueBrowseCmd.MarkZshCompPositionalArgumentCustom(2, "__lab_completion_issue $words[2]")
issueCmd.AddCommand(issueBrowseCmd)
carapace.Gen(issueBrowseCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
)
}
8 changes: 6 additions & 2 deletions cmd/issue_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand Down Expand Up @@ -34,7 +36,9 @@ var issueCloseCmd = &cobra.Command{
}

func init() {
issueCloseCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
issueCloseCmd.MarkZshCompPositionalArgumentCustom(2, "__lab_completion_issue $words[2]")
issueCmd.AddCommand(issueCloseCmd)
carapace.Gen(issueCloseCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
)
}
6 changes: 5 additions & 1 deletion cmd/issue_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"strings"
"text/template"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -137,6 +139,8 @@ func init() {
issueCreateCmd.Flags().StringSliceP("assignees", "a", []string{}, "Set assignees by username")
issueCreateCmd.Flags().StringP("template", "t", "default", "use the given issue template")

issueCreateCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
issueCmd.AddCommand(issueCreateCmd)
carapace.Gen(issueCreateCmd).PositionalCompletion(
action.Remotes(),
)
}
8 changes: 6 additions & 2 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"strings"
"text/template"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)
Expand Down Expand Up @@ -281,7 +283,9 @@ func issueEditCmdAddFlags(flags *pflag.FlagSet) *pflag.FlagSet {

func init() {
issueEditCmdAddFlags(issueEditCmd.Flags())
issueEditCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
issueEditCmd.MarkZshCompPositionalArgumentCustom(2, "__lab_completion_issue $words[2]")
issueCmd.AddCommand(issueEditCmd)
carapace.Gen(issueEditCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
)
}
60 changes: 35 additions & 25 deletions cmd/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

Expand All @@ -27,29 +29,7 @@ lab issue list "search terms" # search issues for "search terms"
lab issue search "search terms" # same as above
lab issue list remote "search terms" # search "remote" for issues with "search terms"`,
Run: func(cmd *cobra.Command, args []string) {
rn, issueSearch, err := parseArgsRemoteString(args)
if err != nil {
log.Fatal(err)
}

opts := gitlab.ListProjectIssuesOptions{
ListOptions: gitlab.ListOptions{
PerPage: issueNumRet,
},
Labels: issueLabels,
State: &issueState,
OrderBy: gitlab.String("updated_at"),
}

if issueSearch != "" {
opts.Search = &issueSearch
}

num := issueNumRet
if issueAll {
num = -1
}
issues, err := lab.IssueList(rn, opts, num)
issues, err := issueList(args)
if err != nil {
log.Fatal(err)
}
Expand All @@ -59,6 +39,32 @@ lab issue list remote "search terms" # search "remote" for issues with "search
},
}

func issueList(args []string) ([]*gitlab.Issue, error) {
rn, issueSearch, err := parseArgsRemoteString(args)
if err != nil {
return nil, err
}

opts := gitlab.ListProjectIssuesOptions{
ListOptions: gitlab.ListOptions{
PerPage: issueNumRet,
},
Labels: issueLabels,
State: &issueState,
OrderBy: gitlab.String("updated_at"),
}

if issueSearch != "" {
opts.Search = &issueSearch
}

num := issueNumRet
if issueAll {
num = -1
}
return lab.IssueList(rn, opts, num)
}

func init() {
issueListCmd.Flags().StringSliceVarP(
&issueLabels, "label", "l", []string{},
Expand All @@ -73,7 +79,11 @@ func init() {
&issueAll, "all", "a", false,
"List all issues on the project")

issueListCmd.MarkZshCompPositionalArgumentCustom(1, "__lab_completion_remote")
issueListCmd.MarkFlagCustom("state", "(opened closed)")
issueCmd.AddCommand(issueListCmd)
carapace.Gen(issueListCmd).FlagCompletion(carapace.ActionMap{
"state": carapace.ActionValues("opened", "closed"),
})
carapace.Gen(issueListCmd).PositionalCompletion(
action.Remotes(),
)
}
Loading

0 comments on commit 7bebe48

Please sign in to comment.