Skip to content

Commit

Permalink
added docker context subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 4, 2020
1 parent ada9145 commit c8aafa5
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 0 deletions.
11 changes: 11 additions & 0 deletions completers/docker_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,14 @@ func ActionDetachKeys() carapace.Action {
// "detach-keys": carapace.ActionValues("{a-z}", `ctrl-\`, "ctrl-@", "ctrl-[", "ctrl-]", "ctrl-^", "ctrl-_", "ctrl-{a-z}"),
return carapace.ActionValues()
}

func ActionContexts() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("docker", "context", "ls", "--format", "{{.Name}}\n{{.Description}}").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
vals := strings.Split(string(output), "\n")
return carapace.ActionValuesDescribed(vals[:len(vals)-1]...)
}
})
}
18 changes: 18 additions & 0 deletions completers/docker_completer/cmd/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var contextCmd = &cobra.Command{
Use: "context",
Short: "Manage contexts",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(contextCmd).Standalone()

rootCmd.AddCommand(contextCmd)
}
23 changes: 23 additions & 0 deletions completers/docker_completer/cmd/context_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var context_createCmd = &cobra.Command{
Use: "create",
Short: "Create a context",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_createCmd).Standalone()

context_createCmd.Flags().String("default-stack-orchestrator", "", "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
context_createCmd.Flags().String("description", "", "Description of the context")
context_createCmd.Flags().String("docker", "", "set the docker endpoint (default [])")
context_createCmd.Flags().String("from", "", "create context from a named context")
context_createCmd.Flags().String("kubernetes", "", "set the kubernetes endpoint (default [])")
contextCmd.AddCommand(context_createCmd)
}
25 changes: 25 additions & 0 deletions completers/docker_completer/cmd/context_export.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/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)

var context_exportCmd = &cobra.Command{
Use: "export",
Short: "Export a context to a tar or kubeconfig file",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_exportCmd).Standalone()

context_exportCmd.Flags().Bool("kubeconfig", false, "Export as a kubeconfig file")
contextCmd.AddCommand(context_exportCmd)

carapace.Gen(context_exportCmd).PositionalCompletion(
action.ActionContexts(),
carapace.ActionFiles(""),
)
}
24 changes: 24 additions & 0 deletions completers/docker_completer/cmd/context_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)

var context_importCmd = &cobra.Command{
Use: "import",
Short: "Import a context from a tar or zip file",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_importCmd).Standalone()

contextCmd.AddCommand(context_importCmd)

carapace.Gen(context_importCmd).PositionalCompletion(
action.ActionContexts(),
carapace.ActionFiles(""),
)
}
22 changes: 22 additions & 0 deletions completers/docker_completer/cmd/context_inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)

var context_inspectCmd = &cobra.Command{
Use: "inspect",
Short: "Display detailed information on one or more contexts",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_inspectCmd).Standalone()

context_inspectCmd.Flags().StringP("format", "f", "", "Format the output using the given Go template")
contextCmd.AddCommand(context_inspectCmd)

carapace.Gen(context_inspectCmd).PositionalAnyCompletion(action.ActionContexts())
}
20 changes: 20 additions & 0 deletions completers/docker_completer/cmd/context_ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var context_lsCmd = &cobra.Command{
Use: "ls",
Short: "List context",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_lsCmd).Standalone()

context_lsCmd.Flags().String("format", "", "Pretty-print contexts using a Go template")
context_lsCmd.Flags().BoolP("quiet", "q", false, "Only show context names")
contextCmd.AddCommand(context_lsCmd)
}
22 changes: 22 additions & 0 deletions completers/docker_completer/cmd/context_rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)

var context_rmCmd = &cobra.Command{
Use: "rm",
Short: "Remove one or more contexts",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_rmCmd).Standalone()

context_rmCmd.Flags().BoolP("force", "f", false, "Force the removal of a context in use")
contextCmd.AddCommand(context_rmCmd)

carapace.Gen(context_rmCmd).PositionalAnyCompletion(action.ActionContexts())
}
27 changes: 27 additions & 0 deletions completers/docker_completer/cmd/context_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)

var context_updateCmd = &cobra.Command{
Use: "update",
Short: "Update a context",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_updateCmd).Standalone()

context_updateCmd.Flags().String("default-stack-orchestrator", "", "Default orchestrator for stack operations to use")
context_updateCmd.Flags().String("description", "", "Description of the context")
context_updateCmd.Flags().String("docker", "", "set the docker endpoint (default [])")
context_updateCmd.Flags().String("kubernetes", "", "set the kubernetes endpoint (default [])")
contextCmd.AddCommand(context_updateCmd)

carapace.Gen(context_updateCmd).PositionalCompletion(
action.ActionContexts(),
)
}
23 changes: 23 additions & 0 deletions completers/docker_completer/cmd/context_use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/docker_completer/cmd/action"
"github.com/spf13/cobra"
)

var context_useCmd = &cobra.Command{
Use: "use",
Short: "Set the current docker context",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(context_useCmd).Standalone()

contextCmd.AddCommand(context_useCmd)

carapace.Gen(context_useCmd).PositionalCompletion(
action.ActionContexts(),
)
}

0 comments on commit c8aafa5

Please sign in to comment.