cmd.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package node
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/cli"
  5. "github.com/docker/docker/cli/command"
  6. apiclient "github.com/docker/docker/client"
  7. "github.com/spf13/cobra"
  8. "golang.org/x/net/context"
  9. )
  10. // NewNodeCommand returns a cobra command for `node` subcommands
  11. func NewNodeCommand(dockerCli *command.DockerCli) *cobra.Command {
  12. cmd := &cobra.Command{
  13. Use: "node",
  14. Short: "Manage Swarm nodes",
  15. Args: cli.NoArgs,
  16. Run: func(cmd *cobra.Command, args []string) {
  17. fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
  18. },
  19. }
  20. cmd.AddCommand(
  21. newDemoteCommand(dockerCli),
  22. newInspectCommand(dockerCli),
  23. newListCommand(dockerCli),
  24. newPromoteCommand(dockerCli),
  25. newRemoveCommand(dockerCli),
  26. newPsCommand(dockerCli),
  27. newUpdateCommand(dockerCli),
  28. )
  29. return cmd
  30. }
  31. // Reference returns the reference of a node. The special value "self" for a node
  32. // reference is mapped to the current node, hence the node ID is retrieved using
  33. // the `/info` endpoint.
  34. func Reference(ctx context.Context, client apiclient.APIClient, ref string) (string, error) {
  35. if ref == "self" {
  36. info, err := client.Info(ctx)
  37. if err != nil {
  38. return "", err
  39. }
  40. return info.Swarm.NodeID, nil
  41. }
  42. return ref, nil
  43. }