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 (
|
2020-02-24 19:14:46 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/update"
|
|
|
|
"github.com/hasura/graphql-engine/cli/v2/version"
|
2019-02-04 13:51:29 +03:00
|
|
|
"github.com/pkg/errors"
|
2018-06-24 16:47:01 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
const hasuraASCIIText = `
|
2019-02-05 15:51:21 +03:00
|
|
|
__
|
|
|
|
/ /_ ____ _ _____ __ __ _____ ____ _
|
|
|
|
/ __ \ / __ ` + "`" + `// ___// / / // ___// __ ` + "`" + `/
|
|
|
|
/ / / // /_/ /(__ )/ /_/ // / / /_/ /
|
|
|
|
/_/ /_/ \__,_//____/ \__,_//_/ \__,_/
|
|
|
|
|
|
|
|
`
|
|
|
|
|
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",
|
2021-03-15 18:40:52 +03:00
|
|
|
Short: "Hasura GraphQL engine command line tool",
|
2019-03-20 08:10:06 +03:00
|
|
|
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) {
|
2019-02-04 13:51:29 +03:00
|
|
|
if cmd.Use != updateCLICmdUse {
|
2019-02-14 09:16:36 +03:00
|
|
|
if update.ShouldRunCheck(ec.LastUpdateCheckFile) && ec.GlobalConfig.ShowUpdateNotification && !ec.SkipUpdateCheck {
|
2019-02-04 13:51:29 +03:00
|
|
|
u := &updateOptions{
|
|
|
|
EC: ec,
|
|
|
|
}
|
|
|
|
err := u.run(true)
|
2019-12-25 11:31:29 +03:00
|
|
|
if err != nil && u.EC.Version.GetCLIVersion() != version.DevVersion {
|
|
|
|
ec.Logger.WithError(err).Warn("auto-update failed, run 'hasura update-cli' to update manually")
|
2019-02-04 13:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 16:55:28 +03:00
|
|
|
},
|
2019-02-05 15:51:21 +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),
|
2020-06-16 15:15:04 +03:00
|
|
|
NewSeedCmd(ec),
|
2020-02-24 19:14:46 +03:00
|
|
|
NewActionsCmd(ec),
|
|
|
|
NewPluginsCmd(ec),
|
2018-06-28 08:40:18 +03:00
|
|
|
NewVersionCmd(ec),
|
2020-02-24 19:14:46 +03:00
|
|
|
NewScriptsCmd(ec),
|
2018-06-28 08:40:18 +03:00
|
|
|
NewDocsCmd(ec),
|
2019-02-05 15:51:21 +03:00
|
|
|
NewCompletionCmd(ec),
|
2019-02-04 13:51:29 +03:00
|
|
|
NewUpdateCLICmd(ec),
|
2018-06-24 16:47:01 +03:00
|
|
|
)
|
2019-02-05 15:51:21 +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)")
|
2019-02-14 09:16:36 +03:00
|
|
|
f.StringVar(&ec.ExecutionDirectory, "project", "", "directory where commands are executed (default: current dir)")
|
2021-03-15 18:40:52 +03:00
|
|
|
f.BoolVar(&ec.SkipUpdateCheck, "skip-update-check", false, "skip automatic update check on command execution")
|
2019-07-30 09:55:38 +03:00
|
|
|
f.BoolVar(&ec.NoColor, "no-color", false, "do not colorize output (default: false)")
|
2020-04-23 05:47:51 +03:00
|
|
|
f.StringVar(&ec.Envfile, "envfile", ".env", ".env filename to load ENV vars from")
|
2021-06-27 16:49:31 +03:00
|
|
|
f.StringVar(&ec.CliExtSourceBinPath, "cli-ext-path", "", "path to cli-ext binary")
|
|
|
|
f.MarkHidden("cli-ext-path")
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
// NewDefaultHasuraCommand creates the `hasura` command with default arguments
|
|
|
|
func NewDefaultHasuraCommand() *cobra.Command {
|
|
|
|
return NewDefaultHasuraCommandWithArgs(NewDefaultPluginHandler(validPluginFilenamePrefixes), os.Args, os.Stdin, os.Stdout, os.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultHasuraCommandWithArgs creates the `hasura` command with arguments
|
|
|
|
func NewDefaultHasuraCommandWithArgs(pluginHandler PluginHandler, args []string, in io.Reader, out, errout io.Writer) *cobra.Command {
|
|
|
|
cmd := rootCmd
|
|
|
|
|
|
|
|
if pluginHandler == nil {
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
cmdPathPieces := args[1:]
|
|
|
|
|
|
|
|
// only look for suitable extension executables if
|
|
|
|
// the specified command does not already exist
|
|
|
|
if _, _, err := cmd.Find(cmdPathPieces); err != nil {
|
|
|
|
if err := HandlePluginCommand(pluginHandler, cmdPathPieces); err != nil {
|
|
|
|
fmt.Fprintf(errout, "%v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-02-04 13:51:29 +03:00
|
|
|
err := ec.Prepare()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "preparing execution context failed")
|
|
|
|
}
|
2020-03-26 06:08:54 +03:00
|
|
|
execCmd, err := NewDefaultHasuraCommand().ExecuteC()
|
2019-01-28 16:55:28 +03:00
|
|
|
if err != nil {
|
|
|
|
ec.Telemetry.IsError = true
|
|
|
|
}
|
2020-03-26 06:08:54 +03:00
|
|
|
ec.Telemetry.Command = execCmd.CommandPath()
|
2019-01-28 16:55:28 +03:00
|
|
|
ec.Telemetry.Beam()
|
|
|
|
if ec.Spinner != nil {
|
|
|
|
ec.Spinner.Stop()
|
|
|
|
}
|
|
|
|
return err
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|