Skip to content

Commit

Permalink
Merge pull request #1848 from rsteube/add-carapce-builtin
Browse files Browse the repository at this point in the history
added get-env, set-env and unset-env
  • Loading branch information
rsteube authored Sep 20, 2023
2 parents 6860516 + 81b1765 commit 4d2165a
Show file tree
Hide file tree
Showing 14 changed files with 615 additions and 14 deletions.
66 changes: 56 additions & 10 deletions cmd/carapace/cmd/lazyInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
)

func bash_lazy(completers []string) string {
snippet := `%v
snippet := `%v%v
_carapace_lazy() {
source <(carapace $1 bash)
$"_$1_completion"
}
complete -F _carapace_lazy %v
`
return fmt.Sprintf(snippet, pathSnippet("bash"), strings.Join(completers, " "))
return fmt.Sprintf(snippet, pathSnippet("bash"), envSnippet("bash"), strings.Join(completers, " "))
}

func bash_ble_lazy(completers []string) string {
Expand Down Expand Up @@ -97,8 +97,54 @@ func pathSnippet(shell string) (snippet string) {
return
}

func envSnippet(shell string) string {
if os.Getenv("CARAPACE_ENV") == "0" {
return ""
}

switch shell {
case "bash":
return `
get-env () { echo "${!1}"; }
set-env () { export "$1=$2"; }
unset-env () { unset "$1"; }`

case "fish":
return `
function get-env -d "get environment variable"; echo $$argv[1]; end
function set-env -d "set environment variable"; set -g -x $argv[1] $argv[2]; end
function unset-env -d "unset environment variable"; set -e $argv[1]; end`

case "nushell":
return `
def-env get-env [name] { $env | get $name }
def-env set-env [name, value] { load-env { $name: $value } }
def-env unset-env [name] { hide-env $name }`

case "powershell":
return `
Function get-env([string]$name) { Get-Item "env:$name" }
Function set-env([string]$name, [string]$value) { Set-Item "env:$name" "$value" }
Function unset-env([string]$name) { Remove-Item "env:$name" }`

case "zsh":
return `
get-env () { echo "${(P)1}"; }
set-env () { export "$1=$2"; }
unset-env () { unset "$1"; }`

default:
return ""
}
}

func fish_lazy(completers []string) string {
snippet := `%v
snippet := `%v%v
function _carapace_lazy
complete -c $argv[1] -e
Expand All @@ -111,11 +157,11 @@ end
for index, completer := range completers {
complete[index] = fmt.Sprintf(`complete -c '%v' -f -a '(_carapace_lazy %v)'`, completer, completer)
}
return fmt.Sprintf(snippet, pathSnippet("fish"), strings.Join(complete, "\n"))
return fmt.Sprintf(snippet, pathSnippet("fish"), envSnippet("fish"), strings.Join(complete, "\n"))
}

func nushell_lazy(completers []string) string {
snippet := `%v
snippet := `%v%v
let carapace_completer = {|spans|
carapace $spans.0 nushell $spans | from json
Expand All @@ -130,7 +176,7 @@ $current.completions.external = ($current.completions.external
$env.config = $current
`

return fmt.Sprintf(snippet, pathSnippet("nushell"))
return fmt.Sprintf(snippet, pathSnippet("nushell"), envSnippet("nushell"))
}

func oil_lazy(completers []string) string {
Expand All @@ -146,7 +192,7 @@ complete -F _carapace_lazy %v
}

func powershell_lazy(completers []string) string {
snippet := `%v
snippet := `%v%v
$_carapace_lazy = {
param($wordToComplete, $commandAst, $cursorPosition)
Expand All @@ -160,7 +206,7 @@ $_carapace_lazy = {
for index, completer := range completers {
complete[index] = fmt.Sprintf(`Register-ArgumentCompleter -Native -CommandName '%v' -ScriptBlock $_carapace_lazy`, completer)
}
return fmt.Sprintf(snippet, pathSnippet("powershell"), strings.Join(complete, "\n"))
return fmt.Sprintf(snippet, pathSnippet("powershell"), envSnippet("powershell"), strings.Join(complete, "\n"))
}

func tcsh_lazy(completers []string) string {
Expand Down Expand Up @@ -198,12 +244,12 @@ def _carapace_lazy(context):
}

func zsh_lazy(completers []string) string {
snippet := `%v
snippet := `%v%v
function _carapace_lazy {
source <(carapace $words[1] zsh)
}
compdef _carapace_lazy %v
`
return fmt.Sprintf(snippet, pathSnippet("zsh"), strings.Join(completers, " "))
return fmt.Sprintf(snippet, pathSnippet("zsh"), envSnippet("zsh"), strings.Join(completers, " "))
}
25 changes: 25 additions & 0 deletions completers/get-env_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "get-env <name>",
Short: "get environment variable",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

carapace.Gen(rootCmd).PositionalCompletion(
os.ActionEnvironmentVariables(),
)
}
7 changes: 7 additions & 0 deletions completers/get-env_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/get-env_completer/cmd"

func main() {
cmd.Execute()
}
45 changes: 45 additions & 0 deletions completers/set-env_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/env"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "set-env <name> <value>",
Short: "set environment variable",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

carapace.Gen(rootCmd).PositionalCompletion(
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...)
if !strings.Contains(c.Value, "_") {
return a.MultiParts("_") // only do multipart completion for first underscore
}
return a
}),
os.ActionEnvironmentVariables().Style(style.Blue),
).ToA(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return env.ActionEnvironmentVariableValues(c.Args[0])
}),
)
}
7 changes: 7 additions & 0 deletions completers/set-env_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/set-env_completer/cmd"

func main() {
cmd.Execute()
}
25 changes: 25 additions & 0 deletions completers/unset-env_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "unset-env <name>",
Short: "unset environment variable",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

carapace.Gen(rootCmd).PositionalCompletion(
os.ActionEnvironmentVariables(),
)
}
7 changes: 7 additions & 0 deletions completers/unset-env_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/unset-env_completer/cmd"

func main() {
cmd.Execute()
}
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Examples](./spec/examples.md)
- [Macros](./spec/macros.md)
- [Overlay](./overlay.md)
- [Environment](./environment.md)
- [Development](./development.md)
- [Build](./development/build.md)
- [docker-compose](./development/docker-compose.md)
Expand Down
Loading

0 comments on commit 4d2165a

Please sign in to comment.