github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/juju/authorizedkeys_import.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 "github.com/juju/cmd" 11 "launchpad.net/gnuflag" 12 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/juju" 15 ) 16 17 var importKeysDoc = ` 18 Import new authorized ssh keys to allow the holder of those keys to log on to Juju nodes or machines. 19 The keys are imported using ssh-import-id. 20 ` 21 22 // ImportKeysCommand is used to add new authorized ssh keys for a user. 23 type ImportKeysCommand struct { 24 envcmd.EnvCommandBase 25 user string 26 sshKeyIds []string 27 } 28 29 func (c *ImportKeysCommand) Info() *cmd.Info { 30 return &cmd.Info{ 31 Name: "import", 32 Args: "<ssh key id> [...]", 33 Doc: importKeysDoc, 34 Purpose: "using ssh-import-id, import new authorized ssh keys for a Juju user", 35 } 36 } 37 38 func (c *ImportKeysCommand) Init(args []string) error { 39 switch len(args) { 40 case 0: 41 return errors.New("no ssh key id specified") 42 default: 43 c.sshKeyIds = args 44 } 45 return nil 46 } 47 48 func (c *ImportKeysCommand) SetFlags(f *gnuflag.FlagSet) { 49 f.StringVar(&c.user, "user", "admin", "the user for which to import the keys") 50 } 51 52 func (c *ImportKeysCommand) Run(context *cmd.Context) error { 53 client, err := juju.NewKeyManagerClient(c.EnvName) 54 if err != nil { 55 return err 56 } 57 defer client.Close() 58 59 results, err := client.ImportKeys(c.user, c.sshKeyIds...) 60 if err != nil { 61 return err 62 } 63 for i, result := range results { 64 if result.Error != nil { 65 fmt.Fprintf(context.Stderr, "cannot import key id %q: %v\n", c.sshKeyIds[i], result.Error) 66 } 67 } 68 return nil 69 }