Skip to content

Commit

Permalink
Merge pull request #166 from rsteube/shlex-join
Browse files Browse the repository at this point in the history
bridge: use shlex.Join
  • Loading branch information
rsteube authored Jan 16, 2024
2 parents 148a54f + 5c32aa4 commit b880b99
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 31 deletions.
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM ghcr.io/rsteube/carapace:latest

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

# argcomplete
RUN pip install --break-system-packages 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
30 changes: 18 additions & 12 deletions pkg/actions/bridge/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package bridge
import (
_ "embed"
"fmt"
"os"
"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,33 +20,37 @@ 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, " "))
return carapace.ActionExecCommand("bash", "--rcfile", configPath, "-i", "-c", bashSnippet, strings.Join(args, " "))(func(output []byte) carapace.Action {
joined := shlex.Join(args)
if c.Value == "" {
joined = strings.TrimSuffix(joined, `""`)
}
c.Setenv("COMP_LINE", joined)

file, err := os.CreateTemp(os.TempDir(), "carapace-bridge_bash_*")
if err != nil {
return carapace.ActionMessage(err.Error())
}
defer os.Remove(file.Name())

os.WriteFile(file.Name(), []byte(bashSnippet), os.ModePerm)

return carapace.ActionExecCommand("bash", "--rcfile", configPath, "-i", file.Name())(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
Expand Down
7 changes: 6 additions & 1 deletion pkg/actions/bridge/bash.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/bash -i
#!/bin/bash

[ -f /usr/local/etc/bash_completion ] && source /usr/local/etc/bash_completion # osx
[ -f /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion # linux
[ -f /data/data/com.termux/files/usr/share/bash-completion/bash_completion ] && source /data/data/com.termux/files/usr/share/bash-completion/bash_completion # termux

# 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 b880b99

Please sign in to comment.