forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_config.go
206 lines (175 loc) · 5.78 KB
/
global_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cli
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
"github.com/gofrs/uuid"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// GlobalConfig is the configuration object stored in the GlobalConfigFile.
type GlobalConfig struct {
// UUID used for telemetry, generated on first run.
UUID string `json:"uuid"`
// Indicate if telemetry is enabled or not
EnableTelemetry bool `json:"enable_telemetry"`
// Indicates whether update notifications should be shown or not
ShowUpdateNotification bool `json:"show_update_notification"`
}
type rawGlobalConfig struct {
UUID *string `json:"uuid"`
EnableTelemetry *bool `json:"enable_telemetry"`
ShowUpdateNotification *bool `json:"show_update_notification"`
logger *logrus.Logger
shoudlWrite bool
}
func (c *rawGlobalConfig) read(filename string) error {
b, err := ioutil.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "read file")
}
err = json.Unmarshal(b, c)
if err != nil {
return errors.Wrap(err, "parse file")
}
return nil
}
func (c *rawGlobalConfig) validateKeys() error {
// check prescence of uuid, create if doesn't exist
if c.UUID == nil {
u, err := uuid.NewV4()
if err != nil {
errors.Wrap(err, "failed generating uuid")
}
uid := u.String()
c.UUID = &uid
c.shoudlWrite = true
}
// check enabletelemetry
if c.EnableTelemetry == nil {
trueVal := true
c.EnableTelemetry = &trueVal
c.shoudlWrite = true
}
// check showupdatenotification
if c.ShowUpdateNotification == nil {
trueVal := true
c.ShowUpdateNotification = &trueVal
c.shoudlWrite = true
}
return nil
}
func (c *rawGlobalConfig) write(filename string) error {
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return errors.Wrap(err, "marshal file")
}
err = ioutil.WriteFile(filename, b, 0644)
if err != nil {
return errors.Wrap(err, "write file")
}
return nil
}
// setupGlobConfig ensures that global config directory and file exists and
// reads it into the GlobalConfig object.
func (ec *ExecutionContext) setupGlobalConfig() error {
// check if the directory name is set, else default
if len(ec.GlobalConfigDir) == 0 {
ec.Logger.Debug("global config directory is not pre-set, defaulting")
home, err := homedir.Dir()
if err != nil {
return errors.Wrap(err, "cannot get home directory")
}
globalConfigDir := filepath.Join(home, GlobalConfigDirName)
ec.GlobalConfigDir = globalConfigDir
ec.Logger.Debugf("global config directory set as '%s'", ec.GlobalConfigDir)
}
// create the config directory
err := os.MkdirAll(ec.GlobalConfigDir, os.ModePerm)
if err != nil {
return errors.Wrap(err, "cannot create global config directory")
}
// check if the filename is set, else default
if len(ec.GlobalConfigFile) == 0 {
ec.GlobalConfigFile = filepath.Join(ec.GlobalConfigDir, GlobalConfigFileName)
ec.Logger.Debugf("global config file set as '%s'", ec.GlobalConfigFile)
}
// check if the global config file exist
_, err = os.Stat(ec.GlobalConfigFile)
if os.IsNotExist(err) {
// file does not exist, teat as first run and create it
ec.Logger.Debug("global config file does not exist, this could be the first run, creating it...")
// create an empty config object
gc := &rawGlobalConfig{}
// populate the keys
err := gc.validateKeys()
if err != nil {
return errors.Wrap(err, "setup global config object")
}
// write the file
err = gc.write(ec.GlobalConfigFile)
if err != nil {
return errors.Wrap(err, "write global config file")
}
ec.Logger.Debugf("global config file written at '%s' with content '%v'", ec.GlobalConfigFile, gc)
// also show a notice about telemetry
ec.Logger.Info(StrTelemetryNotice)
} else if os.IsExist(err) || err == nil {
// file exists, verify contents
ec.Logger.Debug("global config file exisits, verifying contents")
// initialize the config object
gc := rawGlobalConfig{}
err := gc.read(ec.GlobalConfigFile)
if err != nil {
return errors.Wrap(err, "reading global config file failed")
}
// validate keys
err = gc.validateKeys()
if err != nil {
return errors.Wrap(err, "validating global config file failed")
}
// write the file if there are any changes
if gc.shoudlWrite {
err := gc.write(ec.GlobalConfigFile)
if err != nil {
return errors.Wrap(err, "writing global config file failed")
}
ec.Logger.Debugf("global config file written at '%s' with content '%+#v'", ec.GlobalConfigFile, gc)
}
}
return ec.readGlobalConfig()
}
// readGlobalConfig reads the configuration from global config file env vars,
// through viper.
func (ec *ExecutionContext) readGlobalConfig() error {
// need to get existing viper because https://github.com/spf13/viper/issues/233
v := viper.New()
v.SetEnvPrefix("HASURA_GRAPHQL")
v.AutomaticEnv()
v.SetConfigName("config")
v.AddConfigPath(ec.GlobalConfigDir)
err := v.ReadInConfig()
if err != nil {
return errors.Wrap(err, "cannot read global config from file/env")
}
if ec.GlobalConfig == nil {
ec.Logger.Debugf("global config is not pre-set, reading from current env")
ec.GlobalConfig = &GlobalConfig{
UUID: v.GetString("uuid"),
EnableTelemetry: v.GetBool("enable_telemetry"),
ShowUpdateNotification: v.GetBool("show_update_notification"),
}
} else {
ec.Logger.Debugf("global config is pre-set to %#v", ec.GlobalConfig)
}
ec.Logger.Debugf("global config: uuid: %v", ec.GlobalConfig.UUID)
ec.Logger.Debugf("global config: enableTelemetry: %v", ec.GlobalConfig.EnableTelemetry)
ec.Logger.Debugf("global config: showUpdateNotification: %v", ec.GlobalConfig.ShowUpdateNotification)
// set if telemetry can be beamed or not
ec.Telemetry.CanBeam = ec.GlobalConfig.EnableTelemetry
ec.Telemetry.UUID = ec.GlobalConfig.UUID
return nil
}