/
config.go
173 lines (153 loc) · 3.4 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package config
import (
"encoding/json"
"fmt"
"github.com/mesos/mesos-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io/ioutil"
"net/url"
"os"
"os/user"
)
const (
OperatorAPIPath = "/api/v1"
SchedulerAPIPath = "/api/v1/scheduler"
)
// Options are functional profile options
type Option func(*Profile)
// ProfileFn returns a Profile
type ProfileFn func() *Profile
// Profile contains environment specific options
type Profile struct {
Master string `json:"master"`
Debug bool `json:"debug"`
Restart bool `json:"restart"`
log *zap.Logger
}
func (p *Profile) Log() *zap.Logger {
if p.log == nil {
cfg := zapConfig(p.Debug)
if p.Debug {
cfg.Level.SetLevel(zap.DebugLevel)
cfg.EncoderConfig.CallerKey = "caller"
}
logger, err := cfg.Build()
if err != nil {
panic(err)
}
p.log = logger
}
return p.log
}
func (p *Profile) With(opts ...Option) *Profile {
for _, opt := range opts {
opt(p)
}
return p
}
func (p Profile) Framework() *mesos.FrameworkInfo {
return &mesos.FrameworkInfo{
ID: &mesos.FrameworkID{Value: ""},
User: "root",
Name: "mesos-cli",
}
}
func (p Profile) Scheduler() *url.URL {
u, _ := url.Parse(p.Master)
u.Path = SchedulerAPIPath
return u
}
func (p Profile) Endpoint() *url.URL {
u, _ := url.Parse(p.Master)
u.Path = OperatorAPIPath
return u
}
type Config struct {
Profiles map[string]*Profile `json:"profiles"`
}
// LoadProfile loads a user configuration
// from ~/.mesos-cli.json creating a
// JSON file with defaults if it does
// not exist.
func LoadProfile(path, name string) (profile *Profile, err error) {
config := &Config{Profiles: map[string]*Profile{}}
if _, err = os.Stat(path); os.IsNotExist(err) {
config.Profiles["default"] = defaults()
raw, err := json.MarshalIndent(config, "", " ")
if err != nil {
return nil, err
}
err = ioutil.WriteFile(path, raw, os.FileMode(0755))
if err != nil {
return nil, err
}
profile = config.Profiles["default"]
} else {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err = json.Unmarshal(raw, &config); err != nil {
return nil, err
}
if _, ok := config.Profiles[name]; !ok {
return nil, fmt.Errorf("Cannot load profile: %s", name)
}
profile = config.Profiles[name]
}
return profile, nil
}
func zapConfig(debug bool) *zap.Config {
return &zap.Config{
Level: zap.NewAtomicLevel(),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
MessageKey: "msg",
StacktraceKey: "stacktrace",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
}
func defaults() *Profile {
return &Profile{
Master: "http://localhost:5050",
}
}
// Master specifies the Mesos master hostname
func Master(master string) Option {
return func(p *Profile) {
if master != "" {
p.Master = master
}
}
}
func Debug(debug bool) Option {
return func(p *Profile) {
p.Debug = debug
}
}
func Restart(restart bool) Option {
return func(p *Profile) {
p.Restart = restart
}
}
func HomeDir() string {
u, err := user.Current()
if err != nil {
panic(err)
}
return u.HomeDir
}