Skip to content

Commit

Permalink
docker: re-add support for legacy commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Feb 21, 2024
1 parent 7cb0457 commit 4942d0b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
36 changes: 36 additions & 0 deletions completers/docker_completer/cmd/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"os"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/docker"
"github.com/spf13/cobra"
)

var inspectCmd = &cobra.Command{
Use: "inspect [OPTIONS] NAME|ID [NAME|ID...]",
Short: "Return low-level information on Docker objects",
Hidden: os.Getenv("DOCKER_HIDE_LEGACY_COMMANDS") == "1",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(inspectCmd).Standalone()
inspectCmd.Flags().StringP("format", "f", "", "Format output using a custom template:")
inspectCmd.Flags().BoolP("size", "s", false, "Display total file sizes if the type is container")
inspectCmd.Flags().String("type", "", "Return JSON for specified type")
rootCmd.AddCommand(inspectCmd)

carapace.Gen(inspectCmd).PositionalAnyCompletion(
carapace.Batch(
docker.ActionContainers(),
docker.ActionNetworks(),
docker.ActionNodes().Suppress("This node is not a swarm manager"),
docker.ActionRepositoryTags(),
docker.ActionSecrets().Suppress("This node is not a swarm manager"),
docker.ActionServices().Suppress("This node is not a swarm manager"),
docker.ActionVolumes(),
).ToA(),
)
}
55 changes: 48 additions & 7 deletions completers/docker_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"os"
"regexp"
"slices"
"strings"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -29,6 +31,7 @@ func init() {
&cobra.Group{ID: "common", Title: "Common Commands"},
&cobra.Group{ID: "management", Title: "Management Commands"},
&cobra.Group{ID: "swarm", Title: "Swarm Commands"},
&cobra.Group{ID: "legacy", Title: "Legacy Commands"},
)

rootCmd.Flags().String("config", "/home/rsteube/.docker", "Location of client config files")
Expand Down Expand Up @@ -81,22 +84,60 @@ func init() {
}
}

addRootAlias("run", container_runCmd.Short, "common", []string{"docker", "container", "run"})
addRootAlias("run", "common", false, container_runCmd)

hideLegacyCommands := os.Getenv("DOCKER_HIDE_LEGACY_COMMANDS") == "1"
addRootAlias("attach", "legacy", hideLegacyCommands, container_attachCmd)
addRootAlias("commit", "legacy", hideLegacyCommands, container_commitCmd)
addRootAlias("cp", "legacy", hideLegacyCommands, container_cpCmd)
addRootAlias("create", "legacy", hideLegacyCommands, container_createCmd)
addRootAlias("diff", "legacy", hideLegacyCommands, container_diffCmd)
addRootAlias("events", "legacy", hideLegacyCommands, system_eventsCmd)
addRootAlias("export", "legacy", hideLegacyCommands, container_exportCmd)
addRootAlias("history", "legacy", hideLegacyCommands, image_historyCmd)
addRootAlias("import", "legacy", hideLegacyCommands, image_importCmd)
addRootAlias("kill", "legacy", hideLegacyCommands, container_killCmd)
addRootAlias("load", "legacy", hideLegacyCommands, image_loadCmd)
addRootAlias("logs", "legacy", hideLegacyCommands, container_logsCmd)
addRootAlias("pause", "legacy", hideLegacyCommands, container_pauseCmd)
addRootAlias("port", "legacy", hideLegacyCommands, container_portCmd)
addRootAlias("rename", "legacy", hideLegacyCommands, container_renameCmd)
addRootAlias("restart", "legacy", hideLegacyCommands, container_restartCmd)
addRootAlias("rm", "legacy", hideLegacyCommands, container_rmCmd)
addRootAlias("rmi", "legacy", hideLegacyCommands, image_rmCmd)
addRootAlias("save", "legacy", hideLegacyCommands, image_saveCmd)
addRootAlias("start", "legacy", hideLegacyCommands, container_startCmd)
addRootAlias("stats", "legacy", hideLegacyCommands, container_statsCmd)
addRootAlias("stop", "legacy", hideLegacyCommands, container_stopCmd)
addRootAlias("tag", "legacy", hideLegacyCommands, image_tagCmd)
addRootAlias("top", "legacy", hideLegacyCommands, container_topCmd)
addRootAlias("unpause", "legacy", hideLegacyCommands, container_unpauseCmd)
addRootAlias("update", "legacy", hideLegacyCommands, container_updateCmd)
addRootAlias("wait", "legacy", hideLegacyCommands, container_waitCmd)
})
}

func addRootAlias(use string, short, groupID string, command []string) {
cmd := &cobra.Command{
func addRootAlias(use string, groupID string, hidden bool, cmd *cobra.Command) {
aliasCmd := &cobra.Command{
Use: use,
Short: short,
Short: cmd.Short,
GroupID: groupID,
Hidden: hidden,
Run: func(cmd *cobra.Command, args []string) {},
DisableFlagParsing: true,
}

carapace.Gen(cmd).PositionalAnyCompletion(
bridge.ActionCarapaceBin(command...),
carapace.Gen(aliasCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
command := []string{cmd.Name()}
for cmd.HasParent() {
cmd = cmd.Parent()
command = append(command, cmd.Name())
}
slices.Reverse(command)
return bridge.ActionCarapaceBin(command...)
}),
)

rootCmd.AddCommand(cmd)
rootCmd.AddCommand(aliasCmd)
}

0 comments on commit 4942d0b

Please sign in to comment.