github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/node/remove.go (about) 1 package node 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/docker/api/types" 11 "github.com/pkg/errors" 12 "github.com/spf13/cobra" 13 ) 14 15 type removeOptions struct { 16 force bool 17 } 18 19 func newRemoveCommand(dockerCli command.Cli) *cobra.Command { 20 opts := removeOptions{} 21 22 cmd := &cobra.Command{ 23 Use: "rm [OPTIONS] NODE [NODE...]", 24 Aliases: []string{"remove"}, 25 Short: "Remove one or more nodes from the swarm", 26 Args: cli.RequiresMinArgs(1), 27 RunE: func(cmd *cobra.Command, args []string) error { 28 return runRemove(dockerCli, args, opts) 29 }, 30 } 31 flags := cmd.Flags() 32 flags.BoolVarP(&opts.force, "force", "f", false, "Force remove a node from the swarm") 33 return cmd 34 } 35 36 func runRemove(dockerCli command.Cli, args []string, opts removeOptions) error { 37 client := dockerCli.Client() 38 ctx := context.Background() 39 40 var errs []string 41 42 for _, nodeID := range args { 43 err := client.NodeRemove(ctx, nodeID, types.NodeRemoveOptions{Force: opts.force}) 44 if err != nil { 45 errs = append(errs, err.Error()) 46 continue 47 } 48 fmt.Fprintf(dockerCli.Out(), "%s\n", nodeID) 49 } 50 51 if len(errs) > 0 { 52 return errors.Errorf("%s", strings.Join(errs, "\n")) 53 } 54 55 return nil 56 }