2019-02-05 15:51:21 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2019-02-05 15:51:21 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type commandGroup struct {
|
|
|
|
Title string
|
|
|
|
Commands []*cobra.Command
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHelpCmd returns the help command
|
|
|
|
func NewHelpCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
|
|
opts := &helpOptions{
|
|
|
|
EC: ec,
|
|
|
|
}
|
|
|
|
var helpCmd = &cobra.Command{
|
|
|
|
Use: "help",
|
|
|
|
Short: "Help about any command",
|
|
|
|
Long: "Help provides help for any command in the CLI",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
opts.Cmd = cmd
|
|
|
|
opts.Args = args
|
|
|
|
opts.run()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return helpCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
type helpOptions struct {
|
|
|
|
EC *cli.ExecutionContext
|
|
|
|
|
|
|
|
Cmd *cobra.Command
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *helpOptions) run() {
|
|
|
|
topLevelCommands := []commandGroup{
|
|
|
|
{
|
|
|
|
Title: "GraphQL commands",
|
|
|
|
Commands: []*cobra.Command{
|
|
|
|
NewInitCmd(o.EC),
|
|
|
|
NewMigrateCmd(o.EC),
|
|
|
|
NewMetadataCmd(o.EC),
|
|
|
|
NewConsoleCmd(o.EC),
|
2020-05-05 06:51:20 +03:00
|
|
|
NewActionsCmd(o.EC),
|
2020-06-16 15:15:04 +03:00
|
|
|
NewSeedCmd(o.EC),
|
2021-09-29 09:14:15 +03:00
|
|
|
NewDeployCmd(o.EC),
|
2019-02-05 15:51:21 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Title: "Other commands",
|
|
|
|
Commands: []*cobra.Command{
|
|
|
|
NewCompletionCmd(o.EC),
|
|
|
|
NewVersionCmd(o.EC),
|
2020-05-05 06:51:20 +03:00
|
|
|
NewPluginsCmd(o.EC),
|
|
|
|
NewScriptsCmd(o.EC),
|
|
|
|
NewUpdateCLICmd(o.EC),
|
2019-02-05 15:51:21 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := o.Cmd
|
|
|
|
args := o.Args
|
|
|
|
cmd, _, e := c.Root().Find(args)
|
|
|
|
if cmd == nil || e != nil {
|
|
|
|
c.Printf("Unknown help topic %#q\n", args)
|
2021-10-13 17:38:07 +03:00
|
|
|
err := c.Root().Usage()
|
|
|
|
if err != nil {
|
|
|
|
ec.Logger.WithError(err).Errorf("error while using a dependency library")
|
|
|
|
}
|
2019-02-05 15:51:21 +03:00
|
|
|
} else {
|
|
|
|
if cmd.Name() == "hasura" {
|
|
|
|
// root command
|
|
|
|
fmt.Println(cmd.Long)
|
2021-07-16 08:26:00 +03:00
|
|
|
w := tabwriter.NewWriter(o.EC.Stdout, 0, 0, 3, ' ', 0)
|
2019-02-05 15:51:21 +03:00
|
|
|
for _, g := range topLevelCommands {
|
|
|
|
fmt.Println(g.Title + ":")
|
|
|
|
for _, gc := range g.Commands {
|
|
|
|
fmt.Fprintf(w, " %s\t%s\n", gc.Name(), gc.Short)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
fmt.Println(`Use "hasura [command] --help" for more information about a command.`)
|
|
|
|
} else {
|
|
|
|
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
|
2021-10-13 17:38:07 +03:00
|
|
|
err := cmd.Help()
|
|
|
|
if err != nil {
|
|
|
|
ec.Logger.WithError(err).Errorf("error while using a dependency library")
|
|
|
|
}
|
2019-02-05 15:51:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|