/
context.go
106 lines (93 loc) · 2.34 KB
/
context.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
package main
import (
"context"
"fmt"
"os"
"github.com/juju/ansiterm"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/color"
"github.com/manifoldco/manifold-cli/config"
"github.com/manifoldco/manifold-cli/middleware"
"github.com/manifoldco/manifold-cli/session"
)
func init() {
versionCmd := cli.Command{
Name: "context",
Usage: "Display current context of this tool",
Category: "UTILITY",
Flags: append(teamFlags, []cli.Flag{
cli.BoolFlag{
Name: "short, s",
Usage: "Display only the label of the current context",
},
}...),
Action: middleware.Chain(middleware.LoadDirPrefs, middleware.EnsureSession,
middleware.LoadTeamPrefs, contextLookup),
}
cmds = append(cmds, versionCmd)
}
func contextLookup(cliCtx *cli.Context) error {
ctx := context.Background()
cfg, err := config.Load()
if err != nil {
return cli.NewExitError("Could not load config: "+err.Error(), -1)
}
teamID, err := validateTeamID(cliCtx)
if err != nil {
return err
}
var me bool
var teamName string
s, err := session.Retrieve(ctx, cfg)
if err != nil {
return err
}
if teamID != nil {
teamName = cfg.TeamName
if teamName == "" {
teamName = "unknown"
}
} else {
me = true
if !s.IsUser() {
return errUserActionAsTeam
}
}
usr := s.User()
switch cliCtx.Bool("short") {
case true:
if me {
fmt.Println(s.User().Body.Email)
} else {
fmt.Println(teamName)
}
default:
fmt.Println("Use `manifold switch` to change contexts")
fmt.Println("")
var ctxValue, ctxType string
if me {
ctxType = "user"
ctxValue = fmt.Sprintf("%s (%s)", usr.Body.Name, usr.Body.Email)
} else {
ctxType = "team"
ctxValue = fmt.Sprintf("%s (%s)", cfg.TeamName, cfg.TeamTitle)
}
faint := func(i interface{}) string {
return color.Color(ansiterm.Gray, i)
}
if s.IsUser() {
fmt.Println(color.Bold("Account"))
w := ansiterm.NewTabWriter(os.Stdout, 0, 0, 8, ' ', 0)
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", faint("Name"), usr.Body.Name))
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", faint("Email"), usr.Body.Email))
w.Flush()
fmt.Println("")
}
fmt.Println(color.Bold("Context"))
w := ansiterm.NewTabWriter(os.Stdout, 0, 0, 8, ' ', 0)
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", faint("Type"), ctxType))
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", faint("Value"), ctxValue))
w.Flush()
}
return nil
}