Skip to content

Commit

Permalink
added carapace
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 26, 2023
1 parent 95a5338 commit 2fb573d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
62 changes: 50 additions & 12 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
systemdDefine "github.com/containers/podman/v4/pkg/systemd/define"
"github.com/containers/podman/v4/pkg/util"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -1015,19 +1017,55 @@ func AutocompleteRestartOption(cmd *cobra.Command, args []string, toComplete str
}

// AutocompleteSecurityOption - Autocomplete security options options.
func AutocompleteSecurityOption(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
kv := keyValueCompletion{
"apparmor=": nil,
"no-new-privileges": nil,
"seccomp=": func(s string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveDefault },
"label=": func(s string) ([]string, cobra.ShellCompDirective) {
if strings.HasPrefix(s, "d") {
return []string{"disable"}, cobra.ShellCompDirectiveNoFileComp
// func AutocompleteSecurityOption(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// kv := keyValueCompletion{
// "apparmor=": nil,
// "no-new-privileges": nil,
// "seccomp=": func(s string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveDefault },
// "label=": func(s string) ([]string, cobra.ShellCompDirective) {
// if strings.HasPrefix(s, "d") {
// return []string{"disable"}, cobra.ShellCompDirectiveNoFileComp
// }
// return []string{"user:", "role:", "type:", "level:", "filetype:", "disable"}, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
// },
// }
// return completeKeyValues(toComplete, kv)
// }

func ActionSecurityOptions() carapace.Action {
return carapace.ActionMultiPartsN("=", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.Batch(
carapace.ActionValues("apparmor", "seccomp", "label").Suffix("="),
carapace.ActionValues("no-new-privileges"),
).ToA()
default:
switch c.Parts[0] {
case "seccomp":
return carapace.ActionFiles()
case "label":
return carapace.ActionMultiPartsN(":", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.Batch(
carapace.ActionValues("user", "role", "type", "level", "filetype").Suffix(":"),
carapace.ActionValues("disable").Style(style.Red),
).ToA()
default:
switch c.Parts[0] {
case "user":
return carapace.ActionValues("userA", "userB") // TODO correct completions
default:
return carapace.ActionValues()
}
}
})
default:
return carapace.ActionValues()
}
return []string{"user:", "role:", "type:", "level:", "filetype:", "disable"}, cobra.ShellCompDirectiveNoSpace | cobra.ShellCompDirectiveNoFileComp
},
}
return completeKeyValues(toComplete, kv)
}
})
}

// AutocompleteStopSignal - Autocomplete stop signal options.
Expand Down
6 changes: 5 additions & 1 deletion cmd/podman/common/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/containers/podman/v4/cmd/podman/registry"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -666,7 +667,10 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions,
securityOptFlagName, []string{},
"Security Options",
)
_ = cmd.RegisterFlagCompletionFunc(securityOptFlagName, AutocompleteSecurityOption)
// _ = cmd.RegisterFlagCompletionFunc(securityOptFlagName, AutocompleteSecurityOption)
carapace.Gen(cmd).FlagCompletion(carapace.ActionMap{
securityOptFlagName: ActionSecurityOptions(),
})

subgidnameFlagName := "subgidname"
createFlags.StringVar(
Expand Down
2 changes: 2 additions & 0 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/rsteube/carapace"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -56,6 +57,7 @@ func main() {
}

rootCmd = parseCommands()
carapace.Gen(rootCmd)

Execute()
os.Exit(0)
Expand Down

0 comments on commit 2fb573d

Please sign in to comment.