/
who_am_i.go
44 lines (33 loc) · 950 Bytes
/
who_am_i.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
func registerWhoAmICommand() *cobra.Command {
var raw bool
whoCmd := &cobra.Command{
Use: "who-am-i",
Short: "Command to check which user being used by GoCD Command line interface",
Long: `Command leverages GoCD current user api [https://api.gocd.org/current/#current-user] to GET current user from GoCD`,
PreRunE: setCLIClient,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.GetCurrentUser()
if err != nil {
return err
}
if !raw {
fmt.Printf("user: %s\n", response.Name)
return nil
}
return cliRenderer.Render(response)
},
}
whoCmd.SetUsageTemplate(getUsageTemplate())
for _, command := range whoCmd.Commands() {
command.SilenceUsage = true
}
whoCmd.PersistentFlags().BoolVarP(&raw, "raw", "", false,
"enabling this would print the raw response")
return whoCmd
}