github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/node/cmd.go (about)

     1  package node
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/docker/api/types"
    10  	apiclient "github.com/docker/docker/client"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  // NewNodeCommand returns a cobra command for `node` subcommands
    15  func NewNodeCommand(dockerCli command.Cli) *cobra.Command {
    16  	cmd := &cobra.Command{
    17  		Use:   "node",
    18  		Short: "Manage Swarm nodes",
    19  		Args:  cli.NoArgs,
    20  		RunE:  command.ShowHelp(dockerCli.Err()),
    21  		Annotations: map[string]string{
    22  			"version": "1.24",
    23  			"swarm":   "",
    24  		},
    25  	}
    26  	cmd.AddCommand(
    27  		newDemoteCommand(dockerCli),
    28  		newInspectCommand(dockerCli),
    29  		newListCommand(dockerCli),
    30  		newPromoteCommand(dockerCli),
    31  		newRemoveCommand(dockerCli),
    32  		newPsCommand(dockerCli),
    33  		newUpdateCommand(dockerCli),
    34  	)
    35  	return cmd
    36  }
    37  
    38  // Reference returns the reference of a node. The special value "self" for a node
    39  // reference is mapped to the current node, hence the node ID is retrieved using
    40  // the `/info` endpoint.
    41  func Reference(ctx context.Context, client apiclient.APIClient, ref string) (string, error) {
    42  	if ref == "self" {
    43  		info, err := client.Info(ctx)
    44  		if err != nil {
    45  			return "", err
    46  		}
    47  		if info.Swarm.NodeID == "" {
    48  			// If there's no node ID in /info, the node probably
    49  			// isn't a manager. Call a swarm-specific endpoint to
    50  			// get a more specific error message.
    51  			_, err = client.NodeList(ctx, types.NodeListOptions{})
    52  			if err != nil {
    53  				return "", err
    54  			}
    55  			return "", errors.New("node ID not found in /info")
    56  		}
    57  		return info.Swarm.NodeID, nil
    58  	}
    59  	return ref, nil
    60  }