github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/node/demote.go (about)

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