github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/authorizedkeys_add.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "errors" 8 "fmt" 9 10 "github.com/juju/cmd" 11 "launchpad.net/gnuflag" 12 13 "github.com/juju/juju/cmd/juju/block" 14 ) 15 16 var addKeysDoc = ` 17 Add new authorised ssh keys to allow the holder of those keys to log on to Juju nodes or machines. 18 ` 19 20 // AddKeysCommand is used to add a new authorized ssh key for a user. 21 type AddKeysCommand struct { 22 AuthorizedKeysBase 23 user string 24 sshKeys []string 25 } 26 27 func (c *AddKeysCommand) Info() *cmd.Info { 28 return &cmd.Info{ 29 Name: "add", 30 Args: "<ssh key> [...]", 31 Doc: addKeysDoc, 32 Purpose: "add new authorized ssh keys for a Juju user", 33 } 34 } 35 36 func (c *AddKeysCommand) Init(args []string) error { 37 switch len(args) { 38 case 0: 39 return errors.New("no ssh key specified") 40 default: 41 c.sshKeys = args 42 } 43 return nil 44 } 45 46 func (c *AddKeysCommand) SetFlags(f *gnuflag.FlagSet) { 47 f.StringVar(&c.user, "user", "admin", "the user for which to add the keys") 48 } 49 50 func (c *AddKeysCommand) Run(context *cmd.Context) error { 51 client, err := c.NewKeyManagerClient() 52 if err != nil { 53 return err 54 } 55 defer client.Close() 56 57 results, err := client.AddKeys(c.user, c.sshKeys...) 58 if err != nil { 59 return block.ProcessBlockedError(err, block.BlockChange) 60 } 61 for i, result := range results { 62 if result.Error != nil { 63 fmt.Fprintf(context.Stderr, "cannot add key %q: %v\n", c.sshKeys[i], result.Error) 64 } 65 } 66 return nil 67 }