github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/user/user.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package user 5 6 import ( 7 "fmt" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 "github.com/juju/loggo" 12 "github.com/juju/utils" 13 "github.com/juju/utils/readpass" 14 15 "github.com/juju/juju/api/usermanager" 16 "github.com/juju/juju/cmd/envcmd" 17 ) 18 19 var logger = loggo.GetLogger("juju.cmd.juju.user") 20 21 const userCommandDoc = ` 22 "juju user" is used to manage the user accounts and access control in 23 the Juju environment. 24 ` 25 26 const userCommandPurpose = "manage user accounts and access control" 27 28 // NewSuperCommand creates the user supercommand and registers the subcommands 29 // that it supports. 30 func NewSuperCommand() cmd.Command { 31 usercmd := cmd.NewSuperCommand(cmd.SuperCommandParams{ 32 Name: "user", 33 Doc: userCommandDoc, 34 UsagePrefix: "juju", 35 Purpose: userCommandPurpose, 36 }) 37 usercmd.Register(envcmd.Wrap(&AddCommand{})) 38 usercmd.Register(envcmd.Wrap(&ChangePasswordCommand{})) 39 usercmd.Register(envcmd.Wrap(&InfoCommand{})) 40 usercmd.Register(envcmd.Wrap(&DisableCommand{})) 41 usercmd.Register(envcmd.Wrap(&EnableCommand{})) 42 usercmd.Register(envcmd.Wrap(&ListCommand{})) 43 return usercmd 44 } 45 46 // UserCommandBase is a helper base structure that has a method to get the 47 // user manager client. 48 type UserCommandBase struct { 49 envcmd.EnvCommandBase 50 } 51 52 // NewUserManagerClient returns a usermanager client for the root api endpoint 53 // that the environment command returns. 54 func (c *UserCommandBase) NewUserManagerClient() (*usermanager.Client, error) { 55 root, err := c.NewAPIRoot() 56 if err != nil { 57 return nil, err 58 } 59 return usermanager.NewClient(root), nil 60 } 61 62 var readPassword = readpass.ReadPassword 63 64 func (*UserCommandBase) generateOrReadPassword(ctx *cmd.Context, generate bool) (string, error) { 65 if generate { 66 password, err := utils.RandomPassword() 67 if err != nil { 68 return "", errors.Annotate(err, "failed to generate random password") 69 } 70 return password, nil 71 } 72 73 fmt.Fprintln(ctx.Stdout, "password:") 74 password, err := readPassword() 75 if err != nil { 76 return "", errors.Trace(err) 77 } 78 fmt.Fprintln(ctx.Stdout, "type password again:") 79 verify, err := readPassword() 80 if err != nil { 81 return "", errors.Trace(err) 82 } 83 if password != verify { 84 return "", errors.New("Passwords do not match") 85 } 86 return password, nil 87 }