Skip to content

Commit

Permalink
bridge: use shlex.Join
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jan 16, 2024
1 parent 148a54f commit 3ef8626
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 30 deletions.
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM ghcr.io/rsteube/carapace:latest

RUN apt-get update \
&& apt-get install -y npm \
pip

# argcomplete
RUN pip install azure-cli

# click
RUN pip install --break-system-packages td-watson

# cobra
RUN curl -Lo /usr/local/bin/minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
&& chmod +x /usr/local/bin/minikube

# complete
RUN curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh | sh -s -- --install-method deb

# inshellisense
RUN npm install -g @microsoft/inshellisense
RUN cd /usr/local/lib/node_modules/@microsoft/inshellisense/ \
&& npm i @withfig/[email protected]

# kingpin
RUN curl https://goteleport.com/static/install.sh | bash -s 14.3.3

# urvavecli
RUN curl -Lo /usr/local/bin/tea https://dl.gitea.com/tea/0.9.2/tea-0.9.2-linux-amd64 \
&& chmod +x /usr/local/bin/tea

# yargs
RUN npm install -g @angular/cli
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

services:
bridge:
build: .
command: elvish
environment:
TARGET: /carapace-bridge/cmd/carapace-bridge/carapace-bridge
volumes:
- '.:/carapace-bridge/'
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/rsteube/carapace-bridge
go 1.16

require (
github.com/rsteube/carapace v0.49.0
github.com/rsteube/carapace v0.49.1
github.com/rsteube/carapace-shlex v0.1.2
github.com/spf13/cobra v1.8.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/rsteube/carapace v0.49.0 h1:HhUyBCiZHdWusl73D652XuWS+3YsYBoS3QXNfpiyv9c=
github.com/rsteube/carapace v0.49.0/go.mod h1:4ZC5bulItu9t9sZ5yPcHgPREd8rPf274Q732n+wfl/o=
github.com/rsteube/carapace-shlex v0.1.1 h1:fRQEBBKyYKm4TXUabm4tzH904iFWSmXJl3UZhMfQNYU=
github.com/rsteube/carapace-shlex v0.1.1/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/rsteube/carapace v0.49.1 h1:ljGQpEkSATpvAnP6eO7Bc1iLJljxgfpCJoCPA/EkQlU=
github.com/rsteube/carapace v0.49.1/go.mod h1:syVOvI8e2rEEK/9aMZxfWuHvcnQK/EcnTV4roClEnLE=
github.com/rsteube/carapace-shlex v0.1.2 h1:ZKjhIfXoCkEnzensMglTaLbkNOaLkmM8SCRshpJKx6s=
github.com/rsteube/carapace-shlex v0.1.2/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
Expand Down
16 changes: 4 additions & 12 deletions pkg/actions/bridge/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)
Expand All @@ -18,35 +19,26 @@ var bashSnippet string
func ActionBash(command ...string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(command) == 0 {
return carapace.ActionMessage("missing argument [ActionFish]")
return carapace.ActionMessage("missing argument [ActionBash]")
}

configDir, err := xdg.UserConfigDir()
if err != nil {
return carapace.ActionMessage(err.Error())
}

// replacer := strings.NewReplacer(
// ` `, `\ `,
// `"`, `\""`,
// )

args := append(command, c.Args...)
args = append(args, c.Value)

// for index, arg := range args {
// args[index] = replacer.Replace(arg)
// }

configPath := fmt.Sprintf("%v/carapace/bridge/bash/.bashrc", configDir)
if err := ensureExists(configPath); err != nil {
return carapace.ActionMessage(err.Error())
}

c.Setenv("COMP_LINE", strings.Join(args, " "))
c.Setenv("COMP_LINE", shlex.Join(args))
return carapace.ActionExecCommand("bash", "--rcfile", configPath, "-i", "-c", bashSnippet, strings.Join(args, " "))(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...).StyleF(style.ForPath)
}).Invoke(c).ToA().NoSpace([]rune("/=@:.,")...) // TODO check compopt for nospace
}).NoSpace([]rune("/=@:.,")...) // TODO check compopt for nospace
})
}
3 changes: 3 additions & 0 deletions pkg/actions/bridge/bash.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash -i
# set -e
# set -o pipefail

# COMP_LINE="$1"
COMP_WORDS=($COMP_LINE)
if [ "${COMP_LINE: -1}" = " " ]; then
Expand Down
3 changes: 2 additions & 1 deletion pkg/actions/bridge/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
)

// ActionClick bridges https://github.com/pallets/click
Expand Down Expand Up @@ -42,7 +43,7 @@ func ActionClick(command ...string) carapace.Action {
args := append(command[1:], c.Args...)
current := c.Value

compLine := command[0] + " " + strings.Join(append(args, current), " ") // TODO escape/quote special characters
compLine := command[0] + " " + shlex.Join(append(args, current))
c.Setenv(fmt.Sprintf("_%v_COMPLETE", strings.ToUpper(command[0])), "zsh_complete")
c.Setenv("COMP_WORDS", compLine)
c.Setenv("COMP_CWORD", strconv.Itoa(len(args)+1))
Expand Down
11 changes: 2 additions & 9 deletions pkg/actions/bridge/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)
Expand All @@ -22,23 +23,15 @@ func ActionFish(command ...string) carapace.Action {
return carapace.ActionMessage(err.Error())
}

replacer := strings.NewReplacer(
` `, `\ `,
`"`, `\""`,
)

args := append(command, c.Args...)
args = append(args, c.Value)
for index, arg := range args {
args[index] = replacer.Replace(arg)
}

configPath := fmt.Sprintf("%v/carapace/bridge/fish/config.fish", configDir)
if err := ensureExists(configPath); err != nil {
return carapace.ActionMessage(err.Error())
}

snippet := fmt.Sprintf(`source %#v;complete --do-complete="%v"`, configPath, strings.Join(args, " ")) // TODO needs custom escaping
snippet := fmt.Sprintf(`source "$__fish_data_dir/config.fish";source %#v;complete --do-complete="%v"`, configPath, shlex.Join(args)) // TODO needs custom escaping
return carapace.ActionExecCommand("fish", "--no-config", "--command", snippet)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

Expand Down
10 changes: 9 additions & 1 deletion pkg/actions/bridge/inshellisense.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
)

// ActionInshellisense bridges https://github.com/microsoft/inshellisense
Expand All @@ -16,7 +17,14 @@ func ActionInshellisense(command ...string) carapace.Action {

args := append(command, c.Args...)
args = append(args, c.Value)
input := strings.Join(args, " ") // TODO simple join for now as the lexer in inshellisense can't handle quotes and spaces anyway

input := shlex.Join(args)

if strings.HasSuffix(input, `""`) {
// TODO temporary fix as inshellisense can't handle quotes yet (won't work for those within)
input = input[:len(input)-2] + " "
}

return carapace.ActionExecCommand("inshellisense", "complete", input)(func(output []byte) carapace.Action {
var r struct {
Suggestions []struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/actions/bridge/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/rsteube/carapace"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace/pkg/xdg"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func ActionPowershell(command ...string) carapace.Action {
// args[index] = strings.Replace(arg, " ", "` ", -1)
// }

line := strings.Join(args, " ")
line := shlex.Join(args)
snippet := []string{
fmt.Sprintf(`Get-Content "%v/carapace/bridge/powershell/Microsoft.PowerShell_profile.ps1" | Out-String | Invoke-Expression`, configDir),
fmt.Sprintf(`[System.Management.Automation.CommandCompletion]::CompleteInput("%v", %v, $null).CompletionMatches | ConvertTo-Json `, line, len(line)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ setopt rcquotes
PROMPT=
# load completion system
autoload compinit
autoload -U compinit && compinit
compinit -d "${CARAPACE_BRIDGE_CONFIG_HOME:-~/.config}/carapace/bridge/zsh/.zcompdump_capture"
source "${CARAPACE_BRIDGE_CONFIG_HOME:-~/.config}/carapace/bridge/zsh/.zshrc"
# never run a command
bindkey ''^M'' undefined
bindkey ''^J'' undefined
Expand Down

0 comments on commit 3ef8626

Please sign in to comment.