launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/juju/authorisedkeys_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 "launchpad.net/juju-core/cmd" 13 "launchpad.net/juju-core/juju" 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 cmd.EnvCommandBase 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 c.EnvCommandBase.SetFlags(f) 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 }