-
Notifications
You must be signed in to change notification settings - Fork 32
/
status.go
107 lines (90 loc) · 2.75 KB
/
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
package cmd
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/arigatomachine/cli/api"
"github.com/arigatomachine/cli/config"
"github.com/arigatomachine/cli/errs"
"github.com/arigatomachine/cli/prefs"
"github.com/urfave/cli"
)
func init() {
status := cli.Command{
Name: "status",
Usage: "Show the current Torus status associated with your account and project",
Category: "CONTEXT",
Flags: []cli.Flag{
stdEnvFlag,
serviceFlag("Use this service.", "default", true),
// These flags are hidden so we can still parse out the values
// from the prefs files and env vars, but we don't display
// them to the users in help.
// A user could still set the flag on the command line though :(
placeHolderStringFlag{
StringFlag: cli.StringFlag{Name: "org", EnvVar: "TORUS_ORG", Hidden: true},
Required: true,
},
placeHolderStringFlag{
StringFlag: cli.StringFlag{Name: "project", EnvVar: "TORUS_PROJECT", Hidden: true},
Required: true,
},
placeHolderStringFlag{
StringFlag: cli.StringFlag{Name: "instance", EnvVar: "TORUS_INSTANCE", Value: "1", Hidden: true},
Required: true,
},
},
Action: chain(
ensureDaemon, ensureSession, loadDirPrefs, loadPrefDefaults,
setUserEnv, statusCmd,
),
}
Cmds = append(Cmds, status)
}
func statusCmd(ctx *cli.Context) error {
preferences, err := prefs.NewPreferences(true)
if err != nil {
return err
}
if !preferences.Core.Context {
msg := fmt.Sprintf("Context is disabled. Use '%s prefs' to enable it.", ctx.App.Name)
return errs.NewExitError(msg)
}
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
c := context.Background()
self, err := client.Users.Self(c)
if err != nil {
return errs.NewErrorExitError("Error fetching user details", err)
}
w := tabwriter.NewWriter(os.Stdout, 2, 0, 1, ' ', 0)
fmt.Fprintf(w, "Identity:\t%s <%s>\n", self.Body.Name, self.Body.Email)
fmt.Fprintf(w, "Username:\t%s\n\n", self.Body.Username)
w.Flush()
err = checkRequiredFlags(ctx)
if err != nil {
fmt.Printf("You are not inside a linked working directory. "+
"Use '%s link' to link your project.\n", ctx.App.Name)
return nil
}
org := ctx.String("org")
project := ctx.String("project")
env := ctx.String("environment")
service := ctx.String("service")
instance := ctx.String("instance")
fmt.Fprintf(w, "Org:\t%s\n", org)
fmt.Fprintf(w, "Project:\t%s\n", project)
fmt.Fprintf(w, "Environment:\t%s\n", env)
fmt.Fprintf(w, "Service:\t%s\n", service)
fmt.Fprintf(w, "Instance:\t%s\n", instance)
w.Flush()
parts := []string{"", org, project, env, service, self.Body.Username, instance}
credPath := strings.Join(parts, "/")
fmt.Printf("\nCredential path: %s\n", credPath)
return nil
}