forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_context.go
86 lines (65 loc) · 1.66 KB
/
session_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
package cmd
import (
boshsys "github.com/cloudfoundry/bosh-utils/system"
cmdconf "github.com/cloudfoundry/bosh-cli/cmd/config"
)
// SessionContextImpl prefers options over config values
type SessionContextImpl struct {
opts BoshOpts
config cmdconf.Config
fs boshsys.FileSystem
}
func NewSessionContextImpl(
opts BoshOpts,
config cmdconf.Config,
fs boshsys.FileSystem,
) *SessionContextImpl {
return &SessionContextImpl{opts: opts, config: config, fs: fs}
}
func (c SessionContextImpl) Environment() string {
if len(c.opts.EnvironmentOpt) > 0 {
return c.config.ResolveEnvironment(c.opts.EnvironmentOpt)
}
return c.config.Environment()
}
func (c SessionContextImpl) Credentials() cmdconf.Creds {
creds := c.config.Credentials(c.Environment())
if len(c.opts.UsernameOpt) > 0 {
creds.Username = c.opts.UsernameOpt
}
if len(c.opts.PasswordOpt) > 0 {
creds.Password = c.opts.PasswordOpt
}
if len(c.opts.UAAClientOpt) > 0 {
creds.Client = c.opts.UAAClientOpt
creds.ClientSecret = c.opts.UAAClientSecretOpt
}
return creds
}
func (c SessionContextImpl) CACert() string {
caCert := c.opts.CACertOpt
if len(caCert) > 0 {
if c.isFilePath(caCert) {
readCACert, err := c.fs.ReadFileString(caCert)
if err != nil {
return ""
}
return readCACert
}
return caCert
}
return c.config.CACert(c.Environment())
}
func (c SessionContextImpl) Deployment() string {
if len(c.opts.DeploymentOpt) > 0 {
return c.opts.DeploymentOpt
}
return c.config.Deployment(c.Environment())
}
func (c SessionContextImpl) isFilePath(path string) bool {
fullPath, err := c.fs.ExpandPath(path)
if err != nil {
return false
}
return c.fs.FileExists(fullPath)
}