graphql-engine/cli/commands/root.go

92 lines
2.4 KiB
Go
Raw Normal View History

2018-06-27 15:04:09 +03:00
// Package commands contains the definition for all the commands present in
// Hasura CLI.
2018-06-24 16:40:48 +03:00
package commands
2018-06-24 16:47:01 +03:00
import (
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/update"
"github.com/pkg/errors"
2018-06-24 16:47:01 +03:00
"github.com/spf13/cobra"
)
2018-06-24 16:40:48 +03:00
const hasuraASCIIText = `
__
/ /_ ____ _ _____ __ __ _____ ____ _
/ __ \ / __ ` + "`" + `// ___// / / // ___// __ ` + "`" + `/
/ / / // /_/ /(__ )/ /_/ // / / /_/ /
/_/ /_/ \__,_//____/ \__,_//_/ \__,_/
`
2019-01-28 16:55:28 +03:00
// ec is the Execution Context for the current run.
var ec *cli.ExecutionContext
2018-06-27 15:04:09 +03:00
// rootCmd is the main "hasura" command
2018-06-24 16:40:48 +03:00
var rootCmd = &cobra.Command{
Use: "hasura",
Short: "Hasura GraphQL Engine command line tool",
Long: hasuraASCIIText,
2018-06-24 16:40:48 +03:00
SilenceUsage: true,
SilenceErrors: true,
2019-01-28 16:55:28 +03:00
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ec.Telemetry.Command = cmd.CommandPath()
if cmd.Use != updateCLICmdUse {
if update.ShouldRunCheck(ec.LastUpdateCheckFile) && ec.GlobalConfig.ShowUpdateNotification && !ec.SkipUpdateCheck {
u := &updateOptions{
EC: ec,
}
err := u.run(true)
if err != nil {
ec.Logger.WithError(err).Error("auto-update failed, run 'hasura update-cli' to update manually")
}
}
}
2019-01-28 16:55:28 +03:00
},
Run: func(cmd *cobra.Command, args []string) {
o := helpOptions{
EC: ec,
Cmd: cmd,
Args: args,
}
o.run()
},
2018-06-24 16:40:48 +03:00
}
func init() {
2019-01-28 16:55:28 +03:00
ec = cli.NewExecutionContext()
2018-06-24 16:47:01 +03:00
rootCmd.AddCommand(
NewInitCmd(ec),
NewConsoleCmd(ec),
NewMetadataCmd(ec),
NewMigrateCmd(ec),
NewVersionCmd(ec),
NewDocsCmd(ec),
NewCompletionCmd(ec),
NewUpdateCLICmd(ec),
2018-06-24 16:47:01 +03:00
)
rootCmd.SetHelpCommand(NewHelpCmd(ec))
2018-06-24 16:47:01 +03:00
f := rootCmd.PersistentFlags()
f.StringVar(&ec.LogLevel, "log-level", "INFO", "log level (DEBUG, INFO, WARN, ERROR, FATAL)")
f.StringVar(&ec.ExecutionDirectory, "project", "", "directory where commands are executed (default: current dir)")
f.BoolVar(&ec.SkipUpdateCheck, "skip-update-check", false, "Skip automatic update check on command execution")
f.BoolVar(&ec.NoColor, "no-color", false, "do not colorize output (default: false)")
2018-06-24 16:40:48 +03:00
}
2018-06-27 15:04:09 +03:00
// Execute executes the command and returns the error
2018-06-24 16:40:48 +03:00
func Execute() error {
err := ec.Prepare()
if err != nil {
return errors.Wrap(err, "preparing execution context failed")
}
err = rootCmd.Execute()
2019-01-28 16:55:28 +03:00
if err != nil {
ec.Telemetry.IsError = true
}
ec.Telemetry.Beam()
if ec.Spinner != nil {
ec.Spinner.Stop()
}
return err
2018-06-24 16:40:48 +03:00
}