-
Notifications
You must be signed in to change notification settings - Fork 255
/
current.go
68 lines (60 loc) · 1.41 KB
/
current.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
package context
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/smallstep/cli/flags"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"
"go.step.sm/cli-utils/step"
)
func currentCommand() cli.Command {
return cli.Command{
Name: "current",
Usage: "current returns the name of the current context",
UsageText: "**step context current** [**--json**]",
Description: `**step context current** returns the name of the current context.
## EXAMPLES
List all certificate authority contexts:
'''
$ step context current
test-ca
'''
'''
$ step context current --json
{"name":"test-ca","authority":"internal.ca.smallstep.com","profile":"test-ca"}
'''`,
Action: command.ActionFunc(currentAction),
Flags: []cli.Flag{
flags.HiddenNoContext,
cli.BoolFlag{
Name: "json",
Usage: `Return stringified JSON containing the main attributes of a context.`,
},
},
}
}
func currentAction(ctx *cli.Context) error {
cur := step.Contexts().GetCurrent()
if cur == nil {
return errors.New("no context selected")
}
if ctx.Bool("json") {
b, err := json.Marshal(struct {
Name string `json:"name"`
Authority string `json:"authority"`
Profile string `json:"profile"`
}{
Name: cur.Name,
Authority: cur.Authority,
Profile: cur.Profile,
})
if err != nil {
return err
}
fmt.Printf("%s\n", b)
} else {
fmt.Printf("%s\n", cur.Name)
}
return nil
}