-
Notifications
You must be signed in to change notification settings - Fork 11
/
auth_status.go
151 lines (121 loc) · 3.43 KB
/
auth_status.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package auth
import (
"context"
"fmt"
"strings"
"sync"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"github.com/axiomhq/cli/internal/client"
"github.com/axiomhq/cli/internal/cmdutil"
"github.com/axiomhq/cli/pkg/utils"
)
type statusOptions struct {
*cmdutil.Factory
// Alias of the deployment to check the authentication status for.
Alias string
}
func newStatusCmd(f *cmdutil.Factory) *cobra.Command {
opts := &statusOptions{
Factory: f,
}
cmd := &cobra.Command{
Use: "status [<alias>]",
Short: "View authentication status",
DisableFlagsInUseLine: true,
Args: cmdutil.PopulateFromArgs(f, &opts.Alias),
ValidArgsFunction: deploymentCompletionFunc(f.Config),
Example: heredoc.Doc(`
# Check authentication status of all configured deployments:
$ axiom auth status
# Check authentication status of a specified deployment:
$ axiom auth status axiom-eu-west-1
`),
PreRunE: cmdutil.ChainRunFuncs(
cmdutil.AsksForSetup(f, NewLoginCmd(f)),
cmdutil.NeedsDeployments(f),
cmdutil.NeedsValidDeployment(f, &opts.Alias),
),
RunE: func(cmd *cobra.Command, _ []string) error {
return runStatus(cmd.Context(), opts)
},
}
return cmd
}
func runStatus(ctx context.Context, opts *statusOptions) error {
deploymentAliases := opts.Config.DeploymentAliases()
if opts.Alias != "" {
deploymentAliases = []string{opts.Alias}
}
stop := opts.IO.StartActivityIndicator()
defer stop()
var (
cs = opts.IO.ColorScheme()
statusInfo = map[string][]string{}
statusInfoMu = new(sync.Mutex)
// We don't care about the context here. If an errors occurs, still try
// to get the status of the other deployments.
eg, _ = errgroup.WithContext(ctx)
)
for _, deploymentAlias := range deploymentAliases {
deployment, ok := opts.Config.Deployments[deploymentAlias]
if !ok {
continue
}
eg.Go(func() error {
var info string
defer func() {
statusInfoMu.Lock()
defer statusInfoMu.Unlock()
statusInfo[deploymentAlias] = append(statusInfo[deploymentAlias], info)
}()
client, err := client.New(ctx, deployment.URL, deployment.Token, deployment.OrganizationID, opts.Config.Insecure)
if err != nil {
info = fmt.Sprintf("%s %s", cs.ErrorIcon(), err)
return err
}
user, err := client.Users.Current(ctx)
if err != nil {
info = fmt.Sprintf("%s %s", cs.ErrorIcon(), err)
return err
}
if deployment.OrganizationID == "" {
info = fmt.Sprintf("%s Logged in as %s", cs.SuccessIcon(),
cs.Bold(user.Name))
return nil
}
organization, err := client.Organizations.Get(ctx, deployment.OrganizationID)
if err != nil {
info = fmt.Sprintf("%s %s", cs.ErrorIcon(), err)
return err
}
info = fmt.Sprintf("%s Logged in to %s as %s", cs.SuccessIcon(),
cs.Bold(organization.Name), cs.Bold(user.Name))
return nil
})
}
failed := false
if err := eg.Wait(); err != nil {
failed = true
}
stop()
if opts.IO.IsStderrTTY() {
var buf strings.Builder
for _, alias := range deploymentAliases {
if alias == opts.Config.ActiveDeployment {
fmt.Fprintf(&buf, "%s %s\n", cs.Yellow("➜"), cs.Bold(alias))
} else {
fmt.Fprintf(&buf, " %s\n", cs.Bold(alias))
}
for _, line := range statusInfo[alias] {
fmt.Fprintf(&buf, " %s\n", line)
}
}
fmt.Fprint(opts.IO.ErrOut(), utils.Dedent(buf.String()))
}
if failed {
return cmdutil.ErrSilent
}
return nil
}