github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/environment/share.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package environment 5 6 import ( 7 "strings" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 "github.com/juju/names" 12 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/cmd/juju/block" 15 ) 16 17 const shareEnvHelpDoc = ` 18 Share the current environment with another user. 19 20 Examples: 21 juju environment share joe 22 Give local user "joe" access to the current environment 23 24 juju environment share user1 user2 user3@ubuntuone 25 Give two local users and one remote user access to the current environment 26 27 juju environment share sam --environment myenv 28 Give local user "sam" access to the environment named "myenv" 29 ` 30 31 // ShareCommand represents the command to share an environment with a user(s). 32 type ShareCommand struct { 33 envcmd.EnvCommandBase 34 envName string 35 api ShareEnvironmentAPI 36 37 // Users to share the environment with. 38 Users []names.UserTag 39 } 40 41 // Info implements Command.Info. 42 func (c *ShareCommand) Info() *cmd.Info { 43 return &cmd.Info{ 44 Name: "share", 45 Args: "<user> ...", 46 Purpose: "share the current environment with another user", 47 Doc: strings.TrimSpace(shareEnvHelpDoc), 48 } 49 } 50 51 func (c *ShareCommand) Init(args []string) (err error) { 52 if len(args) == 0 { 53 return errors.New("no users specified") 54 } 55 56 for _, arg := range args { 57 if !names.IsValidUser(arg) { 58 return errors.Errorf("invalid username: %q", arg) 59 } 60 c.Users = append(c.Users, names.NewUserTag(arg)) 61 } 62 63 return nil 64 } 65 66 func (c *ShareCommand) getAPI() (ShareEnvironmentAPI, error) { 67 if c.api != nil { 68 return c.api, nil 69 } 70 return c.NewAPIClient() 71 } 72 73 // ShareEnvironmentAPI defines the API functions used by the environment share command. 74 type ShareEnvironmentAPI interface { 75 Close() error 76 ShareEnvironment(...names.UserTag) error 77 } 78 79 func (c *ShareCommand) Run(ctx *cmd.Context) error { 80 client, err := c.getAPI() 81 if err != nil { 82 return err 83 } 84 defer client.Close() 85 86 return block.ProcessBlockedError(client.ShareEnvironment(c.Users...), block.BlockChange) 87 }