forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
495 lines (423 loc) · 14.9 KB
/
Copy pathcli.go
File metadata and controls
495 lines (423 loc) · 14.9 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Package cli and it's sub packages implements the command line tool for Hasura
// GraphQL Engine. The CLI operates on a directory, denoted by
// "ExecutionDirectory" in the "ExecutionContext" struct.
//
// The ExecutionContext is passed to all the subcommands so that a singleton
// context is available for the execution. Logger and Spinner comes from the same
// context.
package cli
import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v2"
"github.com/briandowns/spinner"
"github.com/gofrs/uuid"
"github.com/hasura/graphql-engine/cli/metadata/actions/types"
"github.com/hasura/graphql-engine/cli/plugins"
"github.com/hasura/graphql-engine/cli/telemetry"
"github.com/hasura/graphql-engine/cli/util"
"github.com/hasura/graphql-engine/cli/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
)
// Other constants used in the package
const (
// Name of the global configuration directory
GlobalConfigDirName = ".hasura"
// Name of the global configuration file
GlobalConfigFileName = "config.json"
// Name of the file to store last update check time
LastUpdateCheckFileName = "last_update_check_at"
)
// String constants
const (
StrTelemetryNotice = `Help us improve Hasura! The cli collects anonymized usage stats which
allow us to keep improving Hasura at warp speed. To opt-out or read more,
visit https://hasura.io/docs/1.0/graphql/manual/guides/telemetry.html
`
)
// ConfigVersion defines the version of the Config.
type ConfigVersion int
const (
// V1 represents config version 1
V1 ConfigVersion = iota + 1
// V2 represents config version 2
V2
)
// ServerConfig has the config values required to contact the server
type ServerConfig struct {
// Endpoint for the GraphQL Engine
Endpoint string `yaml:"endpoint"`
// AccessKey (deprecated) (optional) Admin secret key required to query the endpoint
AccessKey string `yaml:"access_key,omitempty"`
// AdminSecret (optional) Admin secret required to query the endpoint
AdminSecret string `yaml:"admin_secret,omitempty"`
ParsedEndpoint *url.URL `yaml:"-"`
}
// ParseEndpoint ensures the endpoint is valid.
func (s *ServerConfig) ParseEndpoint() error {
nurl, err := url.Parse(s.Endpoint)
if err != nil {
return err
}
s.ParsedEndpoint = nurl
return nil
}
// Config represents configuration required for the CLI to function
type Config struct {
// Version of the config.
Version ConfigVersion `yaml:"version"`
// ServerConfig to be used by CLI to contact server.
ServerConfig `yaml:",inline"`
// MetadataDirectory defines the directory where the metadata files were stored.
MetadataDirectory string `yaml:"metadata_directory"`
// MigrationsDirectory defines the directory where the migration files were stored.
MigrationsDirectory string `yaml:"migrations_directory,omitempty"`
// ActionConfig defines the config required to create or generate codegen for an action.
ActionConfig types.ActionExecutionConfig `yaml:"actions"`
}
// ExecutionContext contains various contextual information required by the cli
// at various points of it's execution. Values are filled in by the
// initializers and passed on to each command. Commands can also fill in values
// to be used further down the line.
type ExecutionContext struct {
// CMDName is the name of CMD (os.Args[0]). To be filled in later to
// correctly render example strings etc.
CMDName string
// ID is a unique ID for this Execution
ID string
// ServerUUID is the unique ID for the server this execution is contacting.
ServerUUID string
// Spinner is the global spinner object used to show progress across the cli.
Spinner *spinner.Spinner
// Logger is the global logger object to print logs.
Logger *logrus.Logger
// ExecutionDirectory is the directory in which command is being executed.
ExecutionDirectory string
// MigrationDir is the name of directory where migrations are stored.
MigrationDir string
// MetadataDir is the name of directory where metadata files are stored.
MetadataDir string
// ConfigFile is the file where endpoint etc. are stored.
ConfigFile string
// Config is the configuration object storing the endpoint and admin secret
// information after reading from config file or env var.
Config *Config
// GlobalConfigDir is the ~/.hasura-graphql directory to store configuration
// globally.
GlobalConfigDir string
// GlobalConfigFile is the file inside GlobalConfigDir where values are
// stored.
GlobalConfigFile string
// GlobalConfig holds all the configuration options.
GlobalConfig *GlobalConfig
// IsStableRelease indicates if the CLI release is stable or not.
IsStableRelease bool
// Version indicates the version object
Version *version.Version
// Viper indicates the viper object for the execution
Viper *viper.Viper
// LogLevel indicates the logrus default logging level
LogLevel string
// NoColor indicates if the outputs shouldn't be colorized
NoColor bool
// Telemetry collects the telemetry data throughout the execution
Telemetry *telemetry.Data
// LastUpdateCheckFile is the file where the timestamp of last update check is stored
LastUpdateCheckFile string
// SkipUpdateCheck will skip the auto update check if set to true
SkipUpdateCheck bool
// PluginsConfig defines the config for plugins
PluginsConfig *plugins.Config
// CodegenAssetsRepo defines the config to handle codegen-assets repo
CodegenAssetsRepo *util.GitUtil
// InitTemplatesRepo defines the config to handle init-templates repo
InitTemplatesRepo *util.GitUtil
// IsTerminal indicates whether the current session is a terminal or not
IsTerminal bool
}
// NewExecutionContext returns a new instance of execution context
func NewExecutionContext() *ExecutionContext {
ec := &ExecutionContext{}
ec.Telemetry = telemetry.BuildEvent()
ec.Telemetry.Version = version.BuildVersion
return ec
}
// Prepare as the name suggests, prepares the ExecutionContext ec by
// initializing most of the variables to sensible defaults, if it is not already
// set.
func (ec *ExecutionContext) Prepare() error {
// set the command name
cmdName := os.Args[0]
if len(cmdName) == 0 {
cmdName = "hasura"
}
ec.CMDName = cmdName
ec.IsTerminal = terminal.IsTerminal(int(os.Stdout.Fd()))
// set spinner
ec.setupSpinner()
// set logger
ec.setupLogger()
// populate version
ec.setVersion()
// setup global config
err := ec.setupGlobalConfig()
if err != nil {
return errors.Wrap(err, "setting up global config failed")
}
// setup plugins path
err = ec.setupPlugins()
if err != nil {
return errors.Wrap(err, "setting up plugins path failed")
}
err = ec.setupCodegenAssetsRepo()
if err != nil {
return errors.Wrap(err, "setting up codegen-assets repo failed")
}
err = ec.setupInitTemplatesRepo()
if err != nil {
return errors.Wrap(err, "setting up init-templates repo failed")
}
ec.LastUpdateCheckFile = filepath.Join(ec.GlobalConfigDir, LastUpdateCheckFileName)
// initialize a blank server config
if ec.Config == nil {
ec.Config = &Config{}
}
// generate an execution id
if ec.ID == "" {
id := "00000000-0000-0000-0000-000000000000"
u, err := uuid.NewV4()
if err == nil {
id = u.String()
} else {
ec.Logger.Debugf("generating uuid for execution ID failed, %v", err)
}
ec.ID = id
ec.Logger.Debugf("execution id: %v", ec.ID)
}
ec.Telemetry.ExecutionID = ec.ID
return nil
}
// setupPlugins create and returns the inferred paths for hasura. By default, it assumes
// $HOME/.hasura as the base path
func (ec *ExecutionContext) setupPlugins() error {
base := filepath.Join(ec.GlobalConfigDir, "plugins")
base, err := filepath.Abs(base)
if err != nil {
return errors.Wrap(err, "cannot get absolute path")
}
ec.PluginsConfig = plugins.New(base)
ec.PluginsConfig.Logger = ec.Logger
return ec.PluginsConfig.Prepare()
}
func (ec *ExecutionContext) setupCodegenAssetsRepo() error {
base := filepath.Join(ec.GlobalConfigDir, util.ActionsCodegenDirName)
base, err := filepath.Abs(base)
if err != nil {
return errors.Wrap(err, "cannot get absolute path")
}
ec.CodegenAssetsRepo = util.NewGitUtil(util.ActionsCodegenRepoURI, base, "")
return nil
}
func (ec *ExecutionContext) setupInitTemplatesRepo() error {
base := filepath.Join(ec.GlobalConfigDir, util.InitTemplatesDirName)
base, err := filepath.Abs(base)
if err != nil {
return errors.Wrap(err, "cannot get absolute path")
}
ec.InitTemplatesRepo = util.NewGitUtil(util.InitTemplatesRepoURI, base, "")
return nil
}
// Validate prepares the ExecutionContext ec and then validates the
// ExecutionDirectory to see if all the required files and directories are in
// place.
func (ec *ExecutionContext) Validate() error {
// ensure plugins index exists
err := ec.PluginsConfig.Repo.EnsureCloned()
if err != nil {
return errors.Wrap(err, "ensuring plugins index failed")
}
// ensure codegen-assets repo exists
err = ec.CodegenAssetsRepo.EnsureCloned()
if err != nil {
return errors.Wrap(err, "ensuring codegen-assets repo failed")
}
// validate execution directory
err = ec.validateDirectory()
if err != nil {
return errors.Wrap(err, "validating current directory failed")
}
// set names of config file
ec.ConfigFile = filepath.Join(ec.ExecutionDirectory, "config.yaml")
// read config and parse the values into Config
err = ec.readConfig()
if err != nil {
return errors.Wrap(err, "cannot read config")
}
// set name of migration directory
ec.MigrationDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MigrationsDirectory)
if _, err := os.Stat(ec.MigrationDir); os.IsNotExist(err) {
err = os.MkdirAll(ec.MigrationDir, os.ModePerm)
if err != nil {
return errors.Wrap(err, "cannot create migrations directory")
}
}
if ec.Config.Version == V2 && ec.Config.MetadataDirectory != "" {
// set name of metadata directory
ec.MetadataDir = filepath.Join(ec.ExecutionDirectory, ec.Config.MetadataDirectory)
if _, err := os.Stat(ec.MetadataDir); os.IsNotExist(err) {
err = os.MkdirAll(ec.MetadataDir, os.ModePerm)
if err != nil {
return errors.Wrap(err, "cannot create metadata directory")
}
}
}
ec.Logger.Debug("graphql engine endpoint: ", ec.Config.ServerConfig.Endpoint)
ec.Logger.Debug("graphql engine admin_secret: ", ec.Config.ServerConfig.AdminSecret)
// get version from the server and match with the cli version
err = ec.checkServerVersion()
if err != nil {
return errors.Wrap(err, "version check")
}
// get the server feature flags
err = ec.Version.GetServerFeatureFlags()
if err != nil {
return errors.Wrap(err, "error in getting server feature flags")
}
state := util.GetServerState(ec.Config.ServerConfig.Endpoint, ec.Config.ServerConfig.AdminSecret, ec.Version.ServerSemver, ec.Logger)
ec.ServerUUID = state.UUID
ec.Telemetry.ServerUUID = ec.ServerUUID
ec.Logger.Debugf("server: uuid: %s", ec.ServerUUID)
return nil
}
func (ec *ExecutionContext) checkServerVersion() error {
v, err := version.FetchServerVersion(ec.Config.ServerConfig.Endpoint)
if err != nil {
return errors.Wrap(err, "failed to get version from server")
}
ec.Version.SetServerVersion(v)
ec.Telemetry.ServerVersion = ec.Version.GetServerVersion()
isCompatible, reason := ec.Version.CheckCLIServerCompatibility()
ec.Logger.Debugf("versions: cli: [%s] server: [%s]", ec.Version.GetCLIVersion(), ec.Version.GetServerVersion())
ec.Logger.Debugf("compatibility check: [%v] %v", isCompatible, reason)
if !isCompatible {
ec.Logger.Warnf("[cli: %s] [server: %s] version mismatch: %s", ec.Version.GetCLIVersion(), ec.Version.GetServerVersion(), reason)
}
return nil
}
// WriteConfig writes the configuration from ec.Config or input config
func (ec *ExecutionContext) WriteConfig(config *Config) error {
var cfg *Config
if config != nil {
cfg = config
} else {
cfg = ec.Config
}
y, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return ioutil.WriteFile(ec.ConfigFile, y, 0644)
}
// readConfig reads the configuration from config file, flags and env vars,
// through viper.
func (ec *ExecutionContext) readConfig() error {
// need to get existing viper because https://github.com/spf13/viper/issues/233
v := ec.Viper
v.SetEnvPrefix("HASURA_GRAPHQL")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
v.SetConfigName("config")
v.SetDefault("version", "1")
v.SetDefault("endpoint", "http://localhost:8080")
v.SetDefault("admin_secret", "")
v.SetDefault("access_key", "")
v.SetDefault("metadata_directory", "")
v.SetDefault("migrations_directory", "migrations")
v.SetDefault("actions.kind", "synchronous")
v.SetDefault("actions.handler_webhook_baseurl", "http://localhost:3000")
v.SetDefault("actions.codegen.framework", "")
v.SetDefault("actions.codegen.output_dir", "")
v.SetDefault("actions.codegen.uri", "")
v.AddConfigPath(ec.ExecutionDirectory)
err := v.ReadInConfig()
if err != nil {
return errors.Wrap(err, "cannot read config from file/env")
}
adminSecret := v.GetString("admin_secret")
if adminSecret == "" {
adminSecret = v.GetString("access_key")
}
ec.Config = &Config{
Version: ConfigVersion(v.GetInt("version")),
ServerConfig: ServerConfig{
Endpoint: v.GetString("endpoint"),
AdminSecret: adminSecret,
},
MetadataDirectory: v.GetString("metadata_directory"),
MigrationsDirectory: v.GetString("migrations_directory"),
ActionConfig: types.ActionExecutionConfig{
Kind: v.GetString("actions.kind"),
HandlerWebhookBaseURL: v.GetString("actions.handler_webhook_baseurl"),
Codegen: &types.CodegenExecutionConfig{
Framework: v.GetString("actions.codegen.framework"),
OutputDir: v.GetString("actions.codegen.output_dir"),
URI: v.GetString("actions.codegen.uri"),
},
},
}
return ec.Config.ServerConfig.ParseEndpoint()
}
// setupSpinner creates a default spinner if the context does not already have
// one.
func (ec *ExecutionContext) setupSpinner() {
if ec.Spinner == nil {
spnr := spinner.New(spinner.CharSets[7], 100*time.Millisecond)
spnr.Writer = os.Stderr
ec.Spinner = spnr
}
}
// Spin stops any existing spinner and starts a new one with the given message.
func (ec *ExecutionContext) Spin(message string) {
if ec.IsTerminal {
ec.Spinner.Stop()
ec.Spinner.Prefix = message
ec.Spinner.Start()
} else {
ec.Logger.Println(message)
}
}
// setupLogger creates a default logger if context does not have one set.
func (ec *ExecutionContext) setupLogger() {
if ec.Logger == nil {
logger := logrus.New()
ec.Logger = logger
}
if ec.LogLevel != "" {
level, err := logrus.ParseLevel(ec.LogLevel)
if err != nil {
ec.Logger.WithError(err).Error("error parsing log-level flag")
return
}
ec.Logger.SetLevel(level)
}
ec.Logger.Hooks = make(logrus.LevelHooks)
ec.Logger.AddHook(newSpinnerHandlerHook(ec.Logger, ec.Spinner, ec.IsTerminal, ec.NoColor))
// set the logger for telemetry
if ec.Telemetry.Logger == nil {
ec.Telemetry.Logger = ec.Logger
}
}
// SetVersion sets the version inside context, according to the variable
// 'version' set during build context.
func (ec *ExecutionContext) setVersion() {
if ec.Version == nil {
ec.Version = version.New()
}
}