From 15723008062b941b53743295784554b2f3ba875e Mon Sep 17 00:00:00 2001 From: Anirudh H M Date: Tue, 14 Mar 2023 22:15:03 +0530 Subject: [PATCH 1/5] Initial addition of shell completion script generator --- commands/run.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 6 ++--- go.sum | 14 ++++++----- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/commands/run.go b/commands/run.go index e6af53b61d..72a05d4012 100644 --- a/commands/run.go +++ b/commands/run.go @@ -61,6 +61,68 @@ func Run() int { root := NewCommand("git-lfs", gitlfsCommand) root.PreRun = nil + + completionCmd := &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: fmt.Sprintf(`To load completions: + +Bash: + + $ source <(%[1]s completion bash) + + # To load completions for each session, execute once: + # Linux: + $ %[1]s completion bash > /etc/bash_completion.d/%[1]s + # macOS: + $ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s + +Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ %[1]s completion zsh > "${fpath[1]}/_%[1]s" + + # You will need to start a new shell for this setup to take effect. + +fish: + + $ %[1]s completion fish | source + + # To load completions for each session, execute once: + $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish + +PowerShell: + + PS> %[1]s completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> %[1]s completion powershell > %[1]s.ps1 + # and source this file from your PowerShell profile. +`, root.Name()), + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + } + }, + } + + root.AddCommand(completionCmd) + // Set up help/usage funcs based on manpage text helpcmd := &cobra.Command{ diff --git a/go.mod b/go.mod index 195814e47c..83dc168d05 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 github.com/pkg/errors v0.0.0-20170505043639-c605e284fe17 github.com/rubyist/tracerx v0.0.0-20170927163412-787959303086 - github.com/spf13/cobra v0.0.3 + github.com/spf13/cobra v1.6.0 github.com/ssgelm/cookiejarparser v1.0.1 github.com/stretchr/testify v1.6.1 github.com/xeipuuv/gojsonschema v0.0.0-20170210233622-6b67b3fab74d @@ -27,7 +27,7 @@ require ( github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/hashicorp/go-uuid v1.0.2 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect github.com/jcmturner/gofork v1.0.0 // indirect @@ -35,7 +35,7 @@ require ( github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect github.com/jcmturner/rpc/v2 v2.0.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/spf13/pflag v1.0.3 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect diff --git a/go.sum b/go.sum index fdbce023f1..4a0ac71d8b 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3Uu github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4= github.com/avast/retry-go v2.4.2+incompatible h1:+ZjCypQT/CyP0kyJO2EcU4d/ZEJWSbP8NENI578cPmA= github.com/avast/retry-go v2.4.2+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -21,8 +22,8 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE= github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8= github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs= github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo= @@ -47,10 +48,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rubyist/tracerx v0.0.0-20170927163412-787959303086 h1:mncRSDOqYCng7jOD+Y6+IivdRI6Kzv2BLWYkWkdQfu0= github.com/rubyist/tracerx v0.0.0-20170927163412-787959303086/go.mod h1:YpdgDXpumPB/+EGmGTYHeiW/0QVFRzBYTNFaxWfPDk4= -github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= -github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI= +github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/ssgelm/cookiejarparser v1.0.1 h1:cRdXauUbOTFzTPJFaeiWbHnQ+tRGlpKKzvIK9PUekE4= github.com/ssgelm/cookiejarparser v1.0.1/go.mod h1:DUfC0mpjIzlDN7DzKjXpHj0qMI5m9VrZuz3wSlI+OEI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= From e8330d123ffa1fcaf97b33051b9cf1a09334ecd7 Mon Sep 17 00:00:00 2001 From: Anirudh H M Date: Sat, 18 Mar 2023 09:45:04 +0530 Subject: [PATCH 2/5] Add _git_lfs function for git bash completion --- commands/run.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/commands/run.go b/commands/run.go index 72a05d4012..384b53f1f9 100644 --- a/commands/run.go +++ b/commands/run.go @@ -1,6 +1,7 @@ package commands import ( + "bytes" "fmt" "log" "os" @@ -110,7 +111,10 @@ PowerShell: Run: func(cmd *cobra.Command, args []string) { switch args[0] { case "bash": - cmd.Root().GenBashCompletion(os.Stdout) + buf := new(bytes.Buffer) + cmd.Root().GenBashCompletion(buf) + buf.WriteString("_git_lfs() { __start_git-lfs; }\n") // this is needed for git bash completion to pick up the completion for the subcommand + buf.WriteTo(os.Stdout) case "zsh": cmd.Root().GenZshCompletion(os.Stdout) case "fish": From 4c9987de9be07bd760e01b39eb9cd72d155c1b25 Mon Sep 17 00:00:00 2001 From: Anirudh H M Date: Sat, 18 Mar 2023 10:24:43 +0530 Subject: [PATCH 3/5] Modify requestComps for git zsh completion --- commands/run.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/commands/run.go b/commands/run.go index 384b53f1f9..03e595f2e1 100644 --- a/commands/run.go +++ b/commands/run.go @@ -111,12 +111,15 @@ PowerShell: Run: func(cmd *cobra.Command, args []string) { switch args[0] { case "bash": - buf := new(bytes.Buffer) - cmd.Root().GenBashCompletion(buf) - buf.WriteString("_git_lfs() { __start_git-lfs; }\n") // this is needed for git bash completion to pick up the completion for the subcommand - buf.WriteTo(os.Stdout) + completion := new(bytes.Buffer) + cmd.Root().GenBashCompletion(completion) + completion.WriteString("_git_lfs() { __start_git-lfs; }\n") // this is needed for git bash completion to pick up the completion for the subcommand + completion.WriteTo(os.Stdout) case "zsh": - cmd.Root().GenZshCompletion(os.Stdout) + completion := new(bytes.Buffer) + cmd.Root().GenZshCompletion(completion) + newCompletion := bytes.NewBuffer(bytes.Replace(completion.Bytes(), []byte("requestComp=\"${words[1]}"), []byte("requestComp=\"git-${words[1]#*git-}"), 1)) // this is needed for git zsh completion to use the right command for completion + newCompletion.WriteTo(os.Stdout) case "fish": cmd.Root().GenFishCompletion(os.Stdout, true) case "powershell": From 83e997436fc8fc7e06f982bf33f25881d7919d3c Mon Sep 17 00:00:00 2001 From: Anirudh H M Date: Wed, 22 Mar 2023 18:41:52 +0530 Subject: [PATCH 4/5] Fix for localization --- commands/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/run.go b/commands/run.go index 03e595f2e1..fc9f07b254 100644 --- a/commands/run.go +++ b/commands/run.go @@ -66,7 +66,7 @@ func Run() int { completionCmd := &cobra.Command{ Use: "completion [bash|zsh|fish|powershell]", Short: "Generate completion script", - Long: fmt.Sprintf(`To load completions: + Long: tr.Tr.Get(`To load completions: Bash: From 9b4f8505c8c7c21c71f96f43370be1f79fb81d80 Mon Sep 17 00:00:00 2001 From: Anirudh H M Date: Wed, 22 Mar 2023 22:58:50 +0530 Subject: [PATCH 5/5] Format with goimports --- commands/run.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/commands/run.go b/commands/run.go index fc9f07b254..de2d9b6979 100644 --- a/commands/run.go +++ b/commands/run.go @@ -62,7 +62,7 @@ func Run() int { root := NewCommand("git-lfs", gitlfsCommand) root.PreRun = nil - + completionCmd := &cobra.Command{ Use: "completion [bash|zsh|fish|powershell]", Short: "Generate completion script", @@ -130,7 +130,6 @@ PowerShell: root.AddCommand(completionCmd) - // Set up help/usage funcs based on manpage text helpcmd := &cobra.Command{ Use: "help [command]",