Skip to content

Commit

Permalink
Merge pull request #1057 from rsteube/fix-context-env
Browse files Browse the repository at this point in the history
fix context env
  • Loading branch information
rsteube authored Apr 25, 2022
2 parents 4d4a80e + 5e96574 commit 058b1a3
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 168 deletions.
54 changes: 0 additions & 54 deletions completers/aws_completer/cmd/complete.py

This file was deleted.

110 changes: 26 additions & 84 deletions completers/aws_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package cmd

import (
_ "embed"
"encoding/json"
exec "golang.org/x/sys/execabs"
"os"
"strings"

"github.com/rsteube/carapace"
Expand All @@ -23,91 +19,37 @@ func Execute() error {
return rootCmd.Execute()
}

//go:embed complete.py
var complete string

type completionResult struct {
Name string
HelpText string `json:"help_text"`
}

func init() {
carapace.Gen(rootCmd).Standalone()

carapace.Gen(rootCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
path, err := exec.LookPath("aws_completer")
if err != nil {
return carapace.ActionMessage(err.Error())
}

info, err := os.Stat(path)
if err != nil {
return carapace.ActionMessage(err.Error())
}
if info.Size() > 100000 { // python version is ~4 kb and compiled is >4 MB
return actionBinaryCompleter()
} else {
return actionPythonCompleter()
}
current := c.CallbackValue
if c.CallbackValue == "-" {
return carapace.ActionValues("--").NoSpace() // no shorthand flags so expand to longhand first (which is needed for the completer)
}

c.Setenv("COMP_LINE", "aws "+strings.Join(append(c.Args, current), " ")) // TODO escape/quote special characters
return carapace.ActionExecCommand("aws_completer")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
if lines[0] == "" {
return carapace.ActionValues()
}
nospace := true
for index, line := range lines {
if strings.HasSuffix(line, " ") {
lines[index] = strings.TrimSuffix(line, " ")
nospace = false
}
}
a := carapace.ActionValues(lines[:len(lines)-1]...)
if nospace ||
strings.HasPrefix(current, "file://") ||
strings.HasPrefix(current, "fileb://") {
return a.NoSpace()
}
return a
}).Invoke(c).ToA()
}),
)
}

func actionBinaryCompleter() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
current := c.CallbackValue
if c.CallbackValue == "-" {
return carapace.ActionValues("--").NoSpace() // no shorthand flags so expand to longhand first (which is needed for the completer)
}
os.Setenv("COMP_LINE", "aws "+strings.Join(append(c.Args, current), " ")) // TODO escape/quote special characters
return carapace.ActionExecCommand("aws_completer")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
if lines[0] == "" {
return carapace.ActionValues()
}
a := carapace.ActionValues(lines[:len(lines)-1]...)
if strings.HasPrefix(current, "file://") ||
strings.HasPrefix(current, "fileb://") {
return a.NoSpace()
}
return a
})
})
}

func actionPythonCompleter() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
current := c.CallbackValue
if c.CallbackValue == "-" {
return carapace.ActionValues("--").NoSpace() // no shorthand flags so expand to longhand first (which is needed for the completer)
}
os.Setenv("COMP_LINE", "aws "+strings.Join(append(c.Args, current), " ")) // TODO escape/quote special characters
return carapace.ActionExecCommand("python", "-c", complete)(func(output []byte) carapace.Action {
var completionResults []completionResult
if err := json.Unmarshal(output, &completionResults); err != nil {
return carapace.ActionMessage(err.Error())
}

prefix := ""
if index := strings.LastIndexAny(c.CallbackValue, "=,/"); index > -1 {
prefix = c.CallbackValue[:index+1]
}

nospace := false
vals := make([]string, 0, len(completionResults))
for _, c := range completionResults {
vals = append(vals, strings.TrimPrefix(c.Name, prefix), c.HelpText)
nospace = nospace || strings.ContainsAny(c.Name, "=,/")
}

a := carapace.ActionValuesDescribed(vals...).Invoke(c).Prefix(prefix).ToA()
if nospace ||
strings.HasPrefix(current, "file://") ||
strings.HasPrefix(current, "fileb://") {
return a.NoSpace()
}
return a
})
})
}
5 changes: 2 additions & 3 deletions completers/gcloud_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/bridge"
"github.com/spf13/cobra"
"os"
)

var rootCmd = &cobra.Command{
Expand All @@ -29,8 +28,8 @@ func init() {
if c.CallbackValue == "-" {
return carapace.ActionValues("--").NoSpace() // seems shorthand flags aren't completed anyway so expand to longhand first
}
os.Setenv("CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK", "1")
return bridge.ActionArgcomplete("gcloud")
c.Setenv("CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK", "1")
return bridge.ActionArgcomplete("gcloud").Invoke(c).ToA()
}),
)
}
5 changes: 2 additions & 3 deletions completers/pulumi_completer/cmd/action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"os"
)

type config struct {
Expand All @@ -14,7 +13,7 @@ type config struct {

func ActionConfigKeys(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
os.Setenv("PULUMI_SKIP_UPDATE_CHECK", "1")
c.Setenv("PULUMI_SKIP_UPDATE_CHECK", "1")
return carapace.ActionExecCommand("pulumi", "--cwd", cmd.Flag("cwd").Value.String(), "config", "--json")(func(output []byte) carapace.Action {
var config map[string]config
if err := json.Unmarshal(output, &config); err != nil {
Expand All @@ -26,6 +25,6 @@ func ActionConfigKeys(cmd *cobra.Command) carapace.Action {
vals = append(vals, name, c.Value)
}
return carapace.ActionValuesDescribed(vals...).Invoke(c).ToMultiPartsA(":")
})
}).Invoke(c).ToA()
})
}
5 changes: 2 additions & 3 deletions completers/pulumi_completer/cmd/action/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"os"
)

type stack struct {
Expand All @@ -25,7 +24,7 @@ func ActionStacks(cmd *cobra.Command, opts StackOpts) carapace.Action {
args = append(args, "--all")
}

os.Setenv("PULUMI_SKIP_UPDATE_CHECK", "1")
c.Setenv("PULUMI_SKIP_UPDATE_CHECK", "1")
return carapace.ActionExecCommand("pulumi", args...)(func(output []byte) carapace.Action {
var stacks []stack
if err := json.Unmarshal(output, &stacks); err != nil {
Expand All @@ -40,6 +39,6 @@ func ActionStacks(cmd *cobra.Command, opts StackOpts) carapace.Action {
vals = append(vals, s.Name)
}
return carapace.ActionValues(vals...)
})
}).Invoke(c).ToA()
})
}
11 changes: 6 additions & 5 deletions completers/pulumi_completer/cmd/action/urn.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package action

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/spf13/cobra"
"os"
"path/filepath"
"regexp"
"strings"
"time"

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

type resource struct {
Expand All @@ -22,7 +23,7 @@ func ActionUrns(cmd *cobra.Command) carapace.Action {
cwd := cmd.Flag("cwd").Value.String()
stack := cmd.Flag("stack").Value.String()

os.Setenv("PULUMI_SKIP_UPDATE_CHECK", "1")
c.Setenv("PULUMI_SKIP_UPDATE_CHECK", "1")
return carapace.ActionExecCommand("pulumi", "--cwd", cwd, "stack", "--stack", stack, "--show-urns")(func(output []byte) carapace.Action {
reUrn := regexp.MustCompile(`URN: (?P<urn>urn:pulumi.*)`)
reId := regexp.MustCompile(`ID: (?P<id>.*)`)
Expand All @@ -42,7 +43,7 @@ func ActionUrns(cmd *cobra.Command) carapace.Action {
vals = append(vals, resource.Urn, resource.Id)
}
return carapace.ActionValuesDescribed(vals...)
})
}).Invoke(c).ToA()
}).Cache(5*time.Second, func() (string, error) {
workdir, err := os.Getwd()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-ps v1.0.0
github.com/pelletier/go-toml v1.9.5
github.com/rsteube/carapace v0.19.6
github.com/rsteube/carapace v0.19.7
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rsteube/carapace v0.19.6 h1:8J+Ln3lzccYnJRGzPPvFGtdZRlYiL2qZ2NhbEgP46DU=
github.com/rsteube/carapace v0.19.6/go.mod h1:GgiwpPVhucHNOv0AmtIkxhiEFkCMP5BBRauyQLP0mFY=
github.com/rsteube/carapace v0.19.7 h1:qbRzBiiYWs/cvvgtSZ13JVepKmDvfR8wZHBuYuq6gxo=
github.com/rsteube/carapace v0.19.7/go.mod h1:GgiwpPVhucHNOv0AmtIkxhiEFkCMP5BBRauyQLP0mFY=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
Expand Down
19 changes: 9 additions & 10 deletions pkg/actions/bridge/argcomplete.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bridge

import (
"os"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -51,15 +50,15 @@ func ActionArgcomplete(command string) carapace.Action {
}

compLine := command + " " + strings.Join(append(args, current), " ") // TODO escape/quote special characters
os.Setenv("_ARGCOMPLETE", "1")
os.Setenv("_ARGCOMPLETE_DFS", "\t")
os.Setenv("_ARGCOMPLETE_IFS", "\n")
os.Setenv("_ARGCOMPLETE_SHELL", "fish")
os.Setenv("_ARGCOMPLETE_SUPPRESS_SPACE", "1") // TODO needed? relevant for nospace detection?
// os.Setenv("_ARGCOMPLETE_COMP_WORDBREAKS", " ") // TODO set to space-only for multiparts?
os.Setenv("_ARGCOMPLETE", "1")
os.Setenv("COMP_LINE", compLine)
os.Setenv("COMP_POINT", strconv.Itoa(len(compLine)))
c.Setenv("_ARGCOMPLETE", "1")
c.Setenv("_ARGCOMPLETE_DFS", "\t")
c.Setenv("_ARGCOMPLETE_IFS", "\n")
c.Setenv("_ARGCOMPLETE_SHELL", "fish")
c.Setenv("_ARGCOMPLETE_SUPPRESS_SPACE", "1") // TODO needed? relevant for nospace detection?
// c.Setenv("_ARGCOMPLETE_COMP_WORDBREAKS", " ") // TODO set to space-only for multiparts?
c.Setenv("_ARGCOMPLETE", "1")
c.Setenv("COMP_LINE", compLine)
c.Setenv("COMP_POINT", strconv.Itoa(len(compLine)))
nospace := false
a := carapace.ActionExecCommand("sh", "-c", command+" 8>&1 9>&2 1>/dev/null 2>/dev/null")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
Expand Down
5 changes: 2 additions & 3 deletions pkg/actions/bridge/posener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bridge

import (
"fmt"
"os"
"strings"

"github.com/rsteube/carapace"
Expand All @@ -29,7 +28,7 @@ import (
// }
func ActionPosenerComplete(cmd string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
os.Setenv("COMP_LINE", fmt.Sprintf("%v %v %v", cmd, strings.Join(c.Args, " "), c.CallbackValue))
c.Setenv("COMP_LINE", fmt.Sprintf("%v %v %v", cmd, strings.Join(c.Args, " "), c.CallbackValue))
return carapace.ActionExecCommand(cmd)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

Expand All @@ -41,6 +40,6 @@ func ActionPosenerComplete(cmd string) carapace.Action {
}
}
return a
})
}).Invoke(c).ToA()
})
}

0 comments on commit 058b1a3

Please sign in to comment.