/
whoami.go
135 lines (119 loc) · 3.47 KB
/
whoami.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package user
import (
"fmt"
"io"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v5"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/cmd/output"
"github.com/juju/juju/jujuclient"
)
var whoAmIDetails = `
Display the current controller, model and logged in user name.
`[1:]
const whoAmIExamples = `
juju whoami
`
// NewWhoAmICommand returns a command to print login details.
func NewWhoAmICommand() cmd.Command {
cmd := &whoAmICommand{
store: jujuclient.NewFileClientStore(),
}
return modelcmd.WrapBase(cmd)
}
// Info implements Command.Info
func (c *whoAmICommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "whoami",
Purpose: "Print current login details.",
Doc: whoAmIDetails,
Examples: whoAmIExamples,
SeeAlso: []string{
"controllers",
"login",
"logout",
"models",
"users",
},
})
}
// SetFlags implements Command.SetFlags.
func (c *whoAmICommand) SetFlags(f *gnuflag.FlagSet) {
c.CommandBase.SetFlags(f)
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": formatWhoAmITabular,
})
}
// SetClientStore implements Command.SetClientStore.
func (c *whoAmICommand) SetClientStore(store jujuclient.ClientStore) {
c.store = store
}
type whoAmI struct {
ControllerName string `yaml:"controller" json:"controller"`
ModelName string `yaml:"model,omitempty" json:"model,omitempty"`
UserName string `yaml:"user" json:"user"`
}
func formatWhoAmITabular(writer io.Writer, value interface{}) error {
details, ok := value.(whoAmI)
if !ok {
return errors.Errorf("expected value of type %T, got %T", details, value)
}
tw := output.TabWriter(writer)
fmt.Fprintf(tw, "Controller:\t%s\n", details.ControllerName)
modelName := details.ModelName
if modelName == "" {
modelName = "<no-current-model>"
}
fmt.Fprintf(tw, "Model:\t%s\n", modelName)
fmt.Fprintf(tw, "User:\t%s\n", details.UserName)
return tw.Flush()
}
// Run implements Command.Run
func (c *whoAmICommand) Run(ctx *cmd.Context) error {
controllerName, err := modelcmd.DetermineCurrentController(c.store)
if err != nil && !errors.IsNotFound(err) {
return err
}
if err != nil {
fmt.Fprintln(ctx.Stderr, "There is no current controller.\nRun juju controllers to see available controllers.")
return nil
}
modelName, err := c.store.CurrentModel(controllerName)
if err != nil && !errors.IsNotFound(err) {
return err
}
userDetails, err := c.store.AccountDetails(controllerName)
if err != nil && !errors.IsNotFound(err) {
return err
}
if err != nil {
fmt.Fprintf(ctx.Stderr, "You are not logged in to controller %q and model %q.\nRun juju login if you want to login.\n", controllerName, modelName)
return nil
}
// Only qualify model name if there is a current model.
if modelName != "" {
if unqualifiedModelName, owner, err := jujuclient.SplitModelName(modelName); err == nil {
user := names.NewUserTag(userDetails.User)
modelName = common.OwnerQualifiedModelName(unqualifiedModelName, owner, user)
}
}
result := whoAmI{
ControllerName: controllerName,
ModelName: modelName,
UserName: userDetails.User,
}
return c.out.Write(ctx, result)
}
type whoAmICommand struct {
modelcmd.CommandBase
out cmd.Output
store jujuclient.ClientStore
}