-
Notifications
You must be signed in to change notification settings - Fork 7
/
get.manual.go
90 lines (71 loc) · 2.05 KB
/
get.manual.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
package get
import (
"context"
"encoding/json"
"github.com/MakeNowJust/heredoc/v2"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/subcommand"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/spf13/cobra"
)
type CmdGetSession struct {
*subcommand.SubCommand
factory *cmdutil.Factory
}
func NewCmdGetSession(f *cmdutil.Factory) *CmdGetSession {
ccmd := &CmdGetSession{
factory: f,
}
cmd := &cobra.Command{
Use: "get",
Short: "Get session",
Long: heredoc.Doc(`
Get session information and settings
The session is either loaded by the session file or from environment variables.
`),
Example: heredoc.Doc(`
$ c8y sessions get
Get the details about the current session
$ c8y sessions get --session mycustomsession
Get the details about the current session which is specified via the --session argument
$ c8y sessions get --select host,tenant
Show the host and tenant name of the current session
`),
RunE: ccmd.RunE,
}
cmd.SilenceUsage = true
ccmd.SubCommand = subcommand.NewSubCommand(cmd)
return ccmd
}
func (n *CmdGetSession) RunE(cmd *cobra.Command, args []string) error {
cfg, err := n.factory.Config()
if err != nil {
return err
}
sessionPath := cfg.GetSessionFile()
cfg.Persistent.Set("path", sessionPath)
client, err := n.factory.Client()
if err != nil {
return err
}
// Support looking up a session which is only controlled via env variables
if sessionPath == "" && client != nil {
cfg.Persistent.Set("host", client.GetHostname())
cfg.Persistent.Set("tenant", client.TenantName)
cfg.Persistent.Set("username", client.GetUsername())
version := client.Version
if version == "" {
serverVersion, err := client.TenantOptions.GetVersion(context.Background())
if err != nil {
return err
}
version = serverVersion
}
cfg.Persistent.Set("version", version)
}
sessionFileContents := cfg.Persistent.AllSettings()
b, err := json.Marshal(sessionFileContents)
if err != nil {
return err
}
return n.factory.WriteOutputWithoutPropertyGuess(b, cmdutil.OutputContext{})
}