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