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