unlock.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package swarm
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "github.com/spf13/cobra"
  8. "golang.org/x/crypto/ssh/terminal"
  9. "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/cli/command"
  12. "golang.org/x/net/context"
  13. )
  14. func newUnlockCommand(dockerCli *command.DockerCli) *cobra.Command {
  15. cmd := &cobra.Command{
  16. Use: "unlock",
  17. Short: "Unlock swarm",
  18. Args: cli.ExactArgs(0),
  19. RunE: func(cmd *cobra.Command, args []string) error {
  20. client := dockerCli.Client()
  21. ctx := context.Background()
  22. key, err := readKey(dockerCli.In(), "Please enter unlock key: ")
  23. if err != nil {
  24. return err
  25. }
  26. req := swarm.UnlockRequest{
  27. UnlockKey: key,
  28. }
  29. return client.SwarmUnlock(ctx, req)
  30. },
  31. }
  32. return cmd
  33. }
  34. func readKey(in *command.InStream, prompt string) (string, error) {
  35. if in.IsTerminal() {
  36. fmt.Print(prompt)
  37. dt, err := terminal.ReadPassword(int(in.FD()))
  38. fmt.Println()
  39. return string(dt), err
  40. }
  41. key, err := bufio.NewReader(in).ReadString('\n')
  42. if err == io.EOF {
  43. err = nil
  44. }
  45. return strings.TrimSpace(key), err
  46. }