Skip to content

Commit

Permalink
actions: added env.ActionNameValues
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 13, 2024
1 parent 854901f commit 7b2ba8d
Show file tree
Hide file tree
Showing 35 changed files with 519 additions and 68 deletions.
3 changes: 2 additions & 1 deletion completers/bru_completer/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -29,7 +30,7 @@ func init() {

carapace.Gen(runCmd).FlagCompletion(carapace.ActionMap{
"cacert": carapace.ActionFiles(),
"env": carapace.ActionValues(), // TODO
"env": env.ActionNameValues(false),
"env-var": carapace.ActionValues(), // TODO
"format": carapace.ActionValues("json", "junit", "html"),
"output": carapace.ActionFiles(),
Expand Down
26 changes: 21 additions & 5 deletions completers/cargo-watch_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"strings"

"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/cargo"
"github.com/carapace-sh/carapace-bridge/pkg/actions/bridge"
Expand All @@ -27,12 +30,12 @@ func init() {
rootCmd.Flags().BoolP("clear", "c", false, "Clear the screen before each run")
rootCmd.Flags().Bool("debug", false, "Show debug output")
rootCmd.Flags().StringP("delay", "d", "", "File updates debounce delay in seconds [default: 0.5]")
rootCmd.Flags().StringP("env", "E", "", "Set environment variables for the command")
rootCmd.Flags().String("env-file", "", "Set environment variables from a .env file")
rootCmd.Flags().StringArrayP("env", "E", nil, "Set environment variables for the command")
rootCmd.Flags().StringArray("env-file", nil, "Set environment variables from a .env file")
rootCmd.Flags().StringArrayP("exec", "x", []string{}, "Cargo command(s) to execute on changes [default: check]")
rootCmd.Flags().String("features", "", "List of features passed to cargo invocations")
rootCmd.Flags().BoolP("help", "h", false, "Display this message")
rootCmd.Flags().StringP("ignore", "i", "", "Ignore a glob/gitignore-style pattern")
rootCmd.Flags().StringArrayP("ignore", "i", nil, "Ignore a glob/gitignore-style pattern")
rootCmd.Flags().Bool("ignore-nothing", false, "Ignore nothing, not even target/ and .git/")
rootCmd.Flags().Bool("no-dot-ignores", false, "Don’t use .ignore files")
rootCmd.Flags().Bool("no-process-group", false, "Do not use a process group when running the command")
Expand All @@ -46,12 +49,13 @@ func init() {
rootCmd.Flags().Bool("skip-local-deps", false, "Don't try to find local dependencies of the current crate and watch their working directories. Only watch the current directory.")
rootCmd.Flags().String("use-shell", "", "Use a different shell. E.g. --use-shell=bash")
rootCmd.Flags().BoolP("version", "V", false, "Display version information")
rootCmd.Flags().StringP("watch", "w", "", "Watch specific file(s) or folder(s). Disables finding and watching local dependencies.")
rootCmd.Flags().StringArrayP("watch", "w", nil, "Watch specific file(s) or folder(s). Disables finding and watching local dependencies.")
rootCmd.Flags().Bool("watch-when-idle", false, "Ignore events emitted while the commands run.")
rootCmd.Flags().Bool("why", false, "Show paths that changed")
rootCmd.Flags().StringP("workdir", "C", "", "Change working directory before running command [default: crate root]")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"env": env.ActionNameValues(false),
"exec": bridge.ActionCarapaceBin("cargo").Split(),
"features": cargo.ActionFeatures("").UniqueList(","),
"shell": bridge.ActionCarapaceBin().SplitP(),
Expand All @@ -65,6 +69,18 @@ func init() {
)

carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
return action.Chdir(rootCmd.Flag("workdir").Value.String())
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
envs, err := cmd.Flags().GetStringArray("env")
if err != nil {
return carapace.ActionValues(err.Error())
}
for _, e := range envs {
if k, v, ok := strings.Cut(e, "="); ok {
c.Setenv(k, v)
}
}
return action.Chdir(rootCmd.Flag("workdir").Value.String()).
Invoke(c).ToA()
})
})
}
2 changes: 2 additions & 0 deletions completers/circleci_completer/cmd/local_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -38,5 +39,6 @@ func init() {
}),
"checkout-key": carapace.ActionFiles(),
"config": carapace.ActionFiles(),
"env": env.ActionNameValues(false),
})
}
2 changes: 2 additions & 0 deletions completers/codecov_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -49,6 +50,7 @@ func init() {
// TODO rootDir for git actions
"branch": git.ActionRefs(git.RefOption{LocalBranches: true}),
"dir": carapace.ActionDirectories(),
"env": env.ActionNameValues(false),
"file": carapace.ActionFiles(),
"rootDir": carapace.ActionDirectories(),
"tag": git.ActionRefs(git.RefOption{Tags: true}),
Expand Down
2 changes: 2 additions & 0 deletions completers/docker-compose_completer/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/docker-compose_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -31,6 +32,7 @@ func init() {
// TODO workdir completion
// TODO index
carapace.Gen(execCmd).FlagCompletion(carapace.ActionMap{
"env": env.ActionNameValues(false),
"user": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(c.Args) > 0 {
if index, err := execCmd.Flags().GetInt("index"); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions completers/docker-compose_completer/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/docker-compose_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -41,6 +42,7 @@ func init() {

// TODO flag completion
carapace.Gen(runCmd).FlagCompletion(carapace.ActionMap{
"env": env.ActionNameValues(false),
"volume": action.ActionVolumes(runCmd),
})

Expand Down
2 changes: 2 additions & 0 deletions completers/docker_completer/cmd/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/docker"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -126,6 +127,7 @@ func init() {
"cidfile": carapace.ActionFiles(),
"cpu-shares": carapace.ActionValues("0", "10", "100", "200", "500", "800", "1000"),
"device": carapace.ActionFiles(),
"env": env.ActionNameValues(false),
"env-file": carapace.ActionFiles(),
"group-add": os.ActionGroups(),
"isolation": carapace.ActionValues("default", "hyperv", "process"),
Expand Down
2 changes: 2 additions & 0 deletions completers/docker_completer/cmd/container_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/docker"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -29,6 +30,7 @@ func init() {

carapace.Gen(container_execCmd).FlagCompletion(carapace.ActionMap{
"detach-keys": docker.ActionDetachKeys(),
"env": env.ActionNameValues(false),
"env-file": carapace.ActionFiles(),
"user": os.ActionUserGroup(),
})
Expand Down
2 changes: 2 additions & 0 deletions completers/docker_completer/cmd/container_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/docker"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -130,6 +131,7 @@ func init() {
"cpu-shares": carapace.ActionValues("0", "10", "100", "200", "500", "800", "1000"),
"detach-keys": docker.ActionDetachKeys(),
"device": carapace.ActionFiles(),
"env": env.ActionNameValues(false),
"env-file": carapace.ActionFiles(),
"group-add": os.ActionGroups(),
"isolation": carapace.ActionValues("default", "hyperv", "process"),
Expand Down
2 changes: 2 additions & 0 deletions completers/docker_completer/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/docker"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -29,6 +30,7 @@ func init() {

carapace.Gen(execCmd).FlagCompletion(carapace.ActionMap{
"detach-keys": docker.ActionDetachKeys(),
"env": env.ActionNameValues(false),
"env-file": carapace.ActionFiles(),
})
}
2 changes: 2 additions & 0 deletions completers/docker_completer/cmd/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/docker"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -95,6 +96,7 @@ func init() {

carapace.Gen(service_createCmd).FlagCompletion(carapace.ActionMap{
"endpoint-mode": carapace.ActionValues("vip", "dnsrr"),
"env": env.ActionNameValues(false),
"env-file": carapace.ActionFiles(),
"group": os.ActionGroups(),
"isolation": carapace.ActionValues("default", "hyperv", "process"),
Expand Down
30 changes: 7 additions & 23 deletions completers/env_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
"github.com/carapace-sh/carapace-bin/pkg/actions/ps"
"github.com/carapace-sh/carapace-bridge/pkg/actions/bridge"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -63,30 +63,14 @@ func init() {
}

return carapace.Batch(
carapace.ActionMultiPartsN("=", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.Batch(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
alreadySet := make([]string, 0)
for _, e := range c.Env {
alreadySet = append(alreadySet, strings.SplitN(e, "=", 2)[0])
}
a := env.ActionKnownEnvironmentVariables().Filter(alreadySet...).Suffix("=")
if !strings.Contains(c.Value, "_") {
return a.MultiParts("_") // only do multipart completion for first underscore
}
return a
}),
os.ActionEnvironmentVariables().Style(style.Blue).Suffix("="),
).ToA()
default:
return env.ActionEnvironmentVariableValues(c.Parts[0])
}
}),
env.ActionNameValues(false),
carapace.ActionExecutables(),
carapace.ActionFiles(),
).ToA()
).Invoke(c).Merge().ToA()
}),
)

carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
return action.Chdir(rootCmd.Flag("chdir").Value.String())
})
}
2 changes: 2 additions & 0 deletions completers/faas-cli_completer/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/faas-cli_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,6 +40,7 @@ func init() {
rootCmd.AddCommand(deployCmd)

carapace.Gen(deployCmd).FlagCompletion(carapace.ActionMap{
"env": env.ActionNameValues(false),
"handler": carapace.ActionDirectories(),
"lang": action.ActionLanguageTemplates(),
"namespace": action.ActionNamespaces(),
Expand Down
2 changes: 2 additions & 0 deletions completers/faas-cli_completer/cmd/store_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/faas-cli_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -30,6 +31,7 @@ func init() {
storeCmd.AddCommand(store_deployCmd)

carapace.Gen(store_deployCmd).FlagCompletion(carapace.ActionMap{
"env": env.ActionNameValues(false),
"namespace": action.ActionNamespaces(),
})

Expand Down
2 changes: 2 additions & 0 deletions completers/faas-cli_completer/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/faas-cli_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -51,6 +52,7 @@ func init() {

carapace.Gen(upCmd).FlagCompletion(carapace.ActionMap{
"copy-extra": carapace.ActionDirectories(),
"env": env.ActionNameValues(false),
"handler": carapace.ActionDirectories(),
"lang": action.ActionLanguageTemplates(),
"name": action.ActionFunctions(),
Expand Down
3 changes: 2 additions & 1 deletion completers/flatpak_completer/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ func init() {
"build-dir": carapace.ActionDirectories(),
// "device": carapace.ActionValues(),
// "disallow": carapace.ActionValues(),
// "env": carapace.ActionValues(),
"env": env.ActionNameValues(false),
// "env-fd": carapace.ActionValues(),
// "filesystem": carapace.ActionValues(),
// "metadata": carapace.ActionValues(),
Expand Down
3 changes: 2 additions & 1 deletion completers/flatpak_completer/cmd/buildFinish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ func init() {
// "command": carapace.ActionValues().
// "device": carapace.ActionValues().
// "disallow": carapace.ActionValues().
// "env": carapace.ActionValues().
"env": env.ActionNameValues(false),
// "env-fd": carapace.ActionValues().
// "extension": carapace.ActionValues().
// "extension-priority": carapace.ActionValues().
Expand Down
3 changes: 2 additions & 1 deletion completers/flatpak_completer/cmd/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/flatpak"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func init() {
// "allow": carapace.ActionValues(),
// "device": carapace.ActionValues(),
// "disallow": carapace.ActionValues(),
// "env": carapace.ActionValues(),
"env": env.ActionNameValues(false),
// "env-fd": carapace.ActionValues(),
// "filesystem": carapace.ActionValues(),
// "installation": carapace.ActionValues(),
Expand Down
3 changes: 2 additions & 1 deletion completers/flatpak_completer/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/env"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/flatpak"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -85,7 +86,7 @@ func init() {
// "cwd": carapace.ActionValues(),
// "device": carapace.ActionValues(),
// "disallow": carapace.ActionValues(),
// "env": carapace.ActionValues(),
"env": env.ActionNameValues(false),
// "env-fd": carapace.ActionValues(),
// "filesystem": carapace.ActionValues(),
// "installation": carapace.ActionValues(),
Expand Down
Loading

0 comments on commit 7b2ba8d

Please sign in to comment.