From 1d3ffc0254f00db5fa5813d00175b76428499604 Mon Sep 17 00:00:00 2001 From: Christopher Crone Date: Mon, 11 May 2020 17:57:34 +0200 Subject: [PATCH] Add context rm command Signed-off-by: Christopher Crone --- cli/cmd/context.go | 26 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/cli/cmd/context.go b/cli/cmd/context.go index f8b32c28e..6207e0698 100644 --- a/cli/cmd/context.go +++ b/cli/cmd/context.go @@ -33,6 +33,7 @@ import ( "os" "text/tabwriter" + "github.com/hashicorp/go-multierror" "github.com/spf13/cobra" "github.com/docker/api/context/store" @@ -48,6 +49,7 @@ func ContextCommand() *cobra.Command { cmd.AddCommand( createCommand(), listCommand(), + removeCommand(), ) return cmd @@ -91,6 +93,17 @@ func listCommand() *cobra.Command { return cmd } +func removeCommand() *cobra.Command { + return &cobra.Command{ + Use: "rm", + Aliases: []string{"remove"}, + Args: cobra.MinimumNArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runRemove(cmd.Context(), args) + }, + } +} + func runCreate(ctx context.Context, opts createOpts, name string, contextType string) error { switch contextType { case "aci": @@ -134,3 +147,16 @@ func runList(ctx context.Context) error { return w.Flush() } + +func runRemove(ctx context.Context, args []string) error { + s := store.ContextStore(ctx) + var errs *multierror.Error + for _, n := range args { + if err := s.Remove(n); err != nil { + errs = multierror.Append(errs, err) + } else { + fmt.Println(n) + } + } + return errs.ErrorOrNil() +} diff --git a/go.mod b/go.mod index 2a9f9cadd..14fc964f4 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/gobwas/ws v1.0.3 github.com/golang/protobuf v1.4.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 + github.com/hashicorp/go-multierror v1.1.0 github.com/mitchellh/go-homedir v1.1.0 github.com/onsi/gomega v1.9.0 github.com/opencontainers/go-digest v1.0.0-rc1 diff --git a/go.sum b/go.sum index 90e97510b..0b6d21ec2 100644 --- a/go.sum +++ b/go.sum @@ -119,6 +119,10 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= +github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=