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