-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
70 lines (56 loc) · 1.22 KB
/
config.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
package tui
import (
"strings"
"github.com/csams/doit/pkg/auth"
"github.com/csams/doit/pkg/tui/client"
"github.com/go-logr/logr"
)
type Config struct {
Options *Options
Auth *auth.Config
Common
}
type completedConfig struct {
Options *Options
Auth auth.CompletedConfig
Common
}
type Common struct {
Client client.Client
Log logr.Logger
}
// CompletedConfig can be constructed only from Config.Complete
type CompletedConfig struct {
*completedConfig
}
func NewConfig(o *Options, log logr.Logger) *Config {
return &Config{
Options: o,
Auth: auth.NewConfig(o.Auth),
Common: Common{
Log: log,
},
}
}
func (c *Config) Complete() (CompletedConfig, error) {
completeAuth := c.Auth.Complete()
if c.Client.Tokens == nil {
var err error
if c.Client.Tokens, err = auth.NewTokenProvider(completeAuth); err != nil {
return CompletedConfig{}, err
}
}
if c.Client.Http == nil {
c.Client.Http = auth.NewClient(c.Options.InsecureClient)
}
var baseUrl = c.Options.Address
if !strings.HasSuffix(c.Options.Address, "/") {
baseUrl = baseUrl + "/"
}
c.Client.BaseUrl = baseUrl
return CompletedConfig{&completedConfig{
Options: c.Options,
Auth: completeAuth,
Common: c.Common,
}}, nil
}