github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/node/demote.go (about)

     1  package node
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/docker/api/types/swarm"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  func newDemoteCommand(dockerCli command.Cli) *cobra.Command {
    13  	return &cobra.Command{
    14  		Use:   "demote NODE [NODE...]",
    15  		Short: "Demote one or more nodes from manager in the swarm",
    16  		Args:  cli.RequiresMinArgs(1),
    17  		RunE: func(cmd *cobra.Command, args []string) error {
    18  			return runDemote(dockerCli, args)
    19  		},
    20  	}
    21  }
    22  
    23  func runDemote(dockerCli command.Cli, nodes []string) error {
    24  	demote := func(node *swarm.Node) error {
    25  		if node.Spec.Role == swarm.NodeRoleWorker {
    26  			fmt.Fprintf(dockerCli.Out(), "Node %s is already a worker.\n", node.ID)
    27  			return errNoRoleChange
    28  		}
    29  		node.Spec.Role = swarm.NodeRoleWorker
    30  		return nil
    31  	}
    32  	success := func(nodeID string) {
    33  		fmt.Fprintf(dockerCli.Out(), "Manager %s demoted in the swarm.\n", nodeID)
    34  	}
    35  	return updateNodes(dockerCli, nodes, demote, success)
    36  }