Skip to content

Commit

Permalink
Merge pull request #88 from rsteube/init-script
Browse files Browse the repository at this point in the history
init script
  • Loading branch information
rsteube committed Mar 29, 2023
2 parents 28a77a5 + 35d3055 commit 2592b7c
Showing 1 changed file with 58 additions and 5 deletions.
63 changes: 58 additions & 5 deletions cmd/carapace-bridge/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"bytes"
"fmt"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/rsteube/carapace/pkg/ps"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)
Expand All @@ -15,6 +18,17 @@ var rootCmd = &cobra.Command{
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Example: ` carapace-bin completion:
bash: source <(carapace-bridge _carapace bash)
elvish: eval (carapace-bridge _carapace elvish | slurp)
fish: carapace-bridge _carapace fish | source
nushell: carapace-bridge _carapace nushell
oil: source <(carapace-bridge _carapace oil)
powershell: carapace-bridge _carapace powershell | Out-String | Invoke-Expression
tcsh: eval ` + "`" + `carapace-bridge _carapace tcsh` + "`" + `
xonsh: exec($(carapace-bridge _carapace xonsh))
zsh: source <(carapace-bridge _carapace zsh)
`,
}

func Execute(version string) error {
Expand All @@ -37,23 +51,62 @@ func init() {

func addSubCommand(use, short string, f func(s ...string) carapace.Action) {
cmd := &cobra.Command{
Use: use,
Use: use + " <command>[/<shell>]",
Short: short,
GroupID: "bridge",
Args: cobra.MinimumNArgs(1),
Example: fmt.Sprintf(` bridge <command>:
bash: source <(carapace-bridge %v command/bash)
elvish: eval (carapace-bridge %v command/elvish | slurp)
fish: carapace-bridge %v command/fish | source
nushell: carapace-bridge %v command/nushell
oil: source <(carapace-bridge %v command/oil)
powershell: carapace-bridge %v command/powershell | Out-String | Invoke-Expression
tcsh: eval `+"`"+`carapace-bridge %v command/tcsh`+"`"+`
xonsh: exec($(carapace-bridge %v command/xonsh))
zsh: source <(carapace-bridge %v command/zsh)
`,
use, use, use, use, use, use, use, use, use),
Args: cobra.MinimumNArgs(1),
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
splitted := strings.Split(args[0], "/")
args[0] = splitted[0]
shell := "export"
if len(splitted) > 1 {
shell = splitted[1]
}
rootCmd.SetArgs(append([]string{"_carapace", shell, "", use}, args...))
rootCmd.Execute()

switch len(args) {
case 1:
if shell == "export" {
shell = ps.DetermineShell()
}

cmd := &cobra.Command{Use: splitted[0]}
carapace.Gen(cmd)

stdout := bytes.Buffer{}
cmd.SetOut(&stdout)
cmd.SetArgs([]string{"_carapace", shell})
cmd.Execute()

output := stdout.String()
switch shell {
case "xonsh":
output = strings.Replace(output, fmt.Sprintf("'_carapace', '%v'", shell), fmt.Sprintf("'_carapace', '%v', '', '%v'", shell, use), -1) // xonsh callback
default:
output = strings.Replace(output, fmt.Sprintf("_carapace %v", shell), fmt.Sprintf("_carapace %v '' %v", shell, use), -1) // general callback
}

fmt.Fprint(rootCmd.OutOrStdout(), output)
default:
rootCmd.SetArgs(append([]string{"_carapace", shell, "", use}, args...))
rootCmd.Execute()
}
},
DisableFlagParsing: true,
}

// TODO remove/prevent help flag
carapace.Gen(cmd).Standalone()

rootCmd.AddCommand(cmd)
Expand Down

0 comments on commit 2592b7c

Please sign in to comment.