Skip to content

Commit

Permalink
added sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 20, 2020
1 parent 7abd233 commit 4fe18a9
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 32 deletions.
19 changes: 19 additions & 0 deletions actions/os/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package os

import (
"github.com/rsteube/carapace"
"os"
"strings"
)

func ActionEnvironmentVariables() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
env := os.Environ()
vars := make([]string, len(env))
for index, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
vars[index] = pair[0]
}
return carapace.ActionValues(vars...)
})
}
4 changes: 4 additions & 0 deletions carapace/cmd/completers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import (
sort "github.com/rsteube/carapace-bin/completers/sort_completer/cmd"
split "github.com/rsteube/carapace-bin/completers/split_completer/cmd"
stat "github.com/rsteube/carapace-bin/completers/stat_completer/cmd"
sudo "github.com/rsteube/carapace-bin/completers/sudo_completer/cmd"
sum "github.com/rsteube/carapace-bin/completers/sum_completer/cmd"
sync "github.com/rsteube/carapace-bin/completers/sync_completer/cmd"
tac "github.com/rsteube/carapace-bin/completers/tac_completer/cmd"
Expand Down Expand Up @@ -176,6 +177,7 @@ var completers = []string{
"sort",
"split",
"stat",
"sudo",
"sum",
"sync",
"tac",
Expand Down Expand Up @@ -352,6 +354,8 @@ func executeCompleter(completer string) {
split.Execute()
case "stat":
stat.Execute()
case "sudo":
sudo.Execute()
case "sum":
sum.Execute()
case "sync":
Expand Down
18 changes: 2 additions & 16 deletions completers/env_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"os"
"strings"

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

Expand Down Expand Up @@ -34,22 +32,10 @@ func init() {
rootCmd.Flags().Bool("version", false, "output version information and exit")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"unset": ActionEnvironmentVariables(),
"unset": os.ActionEnvironmentVariables(),
"chdir": carapace.ActionDirectories(),
"block-signal": carapace.ActionKillSignals(),
"default-signal": carapace.ActionKillSignals(),
"ignore-signal": carapace.ActionKillSignals(),
})
}

func ActionEnvironmentVariables() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
env := os.Environ()
vars := make([]string, len(env))
for index, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
vars[index] = pair[0]
}
return carapace.ActionValues(vars...)
})
}
18 changes: 2 additions & 16 deletions completers/printenv_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"os"
"strings"

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

Expand All @@ -24,17 +22,5 @@ func init() {
rootCmd.Flags().BoolP("null", "0", false, "end each output line with NUL, not newline")
rootCmd.Flags().Bool("version", false, "output version information and exit")

carapace.Gen(rootCmd).PositionalAnyCompletion(ActionEnvironmentVariables())
}

func ActionEnvironmentVariables() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
env := os.Environ()
vars := make([]string, len(env))
for index, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
vars[index] = pair[0]
}
return carapace.ActionValues(vars...)
})
carapace.Gen(rootCmd).PositionalAnyCompletion(os.ActionEnvironmentVariables())
}
57 changes: 57 additions & 0 deletions completers/sudo_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

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

var rootCmd = &cobra.Command{
Use: "sudo",
Short: "execute a command as another user",
Run: func(cmd *cobra.Command, args []string) {},
}

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

rootCmd.Flags().BoolP("askpass", "A", false, "use a helper program for password prompting")
rootCmd.Flags().BoolP("background", "b", false, "run command in the background")
rootCmd.Flags().BoolP("bell", "B", false, "ring bell when prompting")
rootCmd.Flags().StringP("close-from", "C", "", "close all file descriptors >= num")
rootCmd.Flags().BoolP("edit", "e", false, "edit files instead of running a command")
rootCmd.Flags().StringP("group", "g", "", "run command as the specified group name or ID")
rootCmd.Flags().BoolP("help", "h", false, "display help message and exit")
rootCmd.Flags().String("host", "", "run command on host (if supported by plugin)")
rootCmd.Flags().BoolP("list", "l", false, "list user's privileges or check a specific command; use twice for longer format")
rootCmd.Flags().BoolP("login", "i", false, "run login shell as the target user; a command may also be specified")
rootCmd.Flags().BoolP("non-interactive", "n", false, "non-interactive mode, no prompts are used")
rootCmd.Flags().StringP("other-user", "U", "", "in list mode, display privileges for user")
rootCmd.Flags().StringP("preserve-env", "E", "", "preserve user environment when running command")
rootCmd.Flags().BoolP("preserve-groups", "P", false, "preserve group vector instead of setting to target's")
rootCmd.Flags().StringP("prompt", "p", "", "use the specified password prompt")
rootCmd.Flags().BoolP("remove-timestamp", "K", false, "remove timestamp file completely")
rootCmd.Flags().BoolP("reset-timestamp", "k", false, "invalidate timestamp file")
rootCmd.Flags().BoolP("set-home", "H", false, "set HOME variable to target user's home dir")
rootCmd.Flags().BoolP("shell", "s", false, "run shell as the target user; a command may also be specified")
rootCmd.Flags().BoolP("stdin", "S", false, "read password from standard input")
rootCmd.Flags().StringP("user", "u", "", "run command (or edit file) as specified user name or ID")
rootCmd.Flags().BoolP("validate", "v", false, "update user's timestamp without running a command")
rootCmd.Flags().BoolP("version", "V", false, "display version information and exit")

rootCmd.Flag("preserve-env").NoOptDefVal = " "

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"group": carapace.ActionGroups(),
"other-user": carapace.ActionUsers(),
"preserve-env": os.ActionEnvironmentVariables(), // TODO comma separated list
"user": carapace.ActionUsers(),
})

carapace.Gen(rootCmd).PositionalCompletion(
carapace.ActionFiles(""),
)
}
7 changes: 7 additions & 0 deletions completers/sudo_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/sudo_completer/cmd"

func main() {
cmd.Execute()
}

0 comments on commit 4fe18a9

Please sign in to comment.