2020-02-24 19:14:46 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2022-11-07 17:33:26 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/util"
|
2022-11-07 17:33:26 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewActionsCmd returns the actions command
|
|
|
|
func NewActionsCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
|
|
v := viper.New()
|
|
|
|
actionsCmd := &cobra.Command{
|
2023-01-09 07:25:53 +03:00
|
|
|
Use: "actions",
|
|
|
|
Short: "Manage Hasura Actions",
|
|
|
|
Long: `Running this command enables the use of additional sub-commands to create, modify, and export code related to a project's Actions.
|
|
|
|
|
|
|
|
Further Reading:
|
|
|
|
- https://hasura.io/docs/latest/actions/index/
|
|
|
|
- https://hasura.io/docs/latest/actions/create/
|
|
|
|
- https://hasura.io/docs/latest/actions/derive/
|
|
|
|
`,
|
2020-02-24 19:14:46 +03:00
|
|
|
SilenceUsage: true,
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2022-11-07 17:33:26 +03:00
|
|
|
op := genOpName(cmd, "PersistentPreRunE")
|
2020-03-26 06:08:54 +03:00
|
|
|
cmd.Root().PersistentPreRun(cmd, args)
|
2020-03-03 10:06:59 +03:00
|
|
|
ec.Viper = v
|
2020-02-24 19:14:46 +03:00
|
|
|
err := ec.Prepare()
|
|
|
|
if err != nil {
|
2022-11-07 17:33:26 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
err = ec.Validate()
|
|
|
|
if err != nil {
|
2022-11-07 17:33:26 +03:00
|
|
|
return errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
if ec.Config.Version < cli.V2 {
|
2022-11-07 17:33:26 +03:00
|
|
|
return errors.E(op, "actions commands can be executed only when config version is greater than 1")
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2021-11-10 12:21:06 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
if ec.MetadataDir == "" {
|
2022-11-07 17:33:26 +03:00
|
|
|
return errors.E(op, "actions commands can be executed only when metadata_dir is set in config")
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actionsCmd.AddCommand(
|
|
|
|
newActionsCreateCmd(ec, v),
|
|
|
|
newActionsCodegenCmd(ec),
|
|
|
|
newActionsUseCodegenCmd(ec),
|
|
|
|
)
|
|
|
|
|
2020-04-08 14:55:42 +03:00
|
|
|
f := actionsCmd.PersistentFlags()
|
2020-02-24 19:14:46 +03:00
|
|
|
|
2023-01-11 11:37:47 +03:00
|
|
|
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
|
|
|
|
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
|
|
|
|
f.String("access-key", "", "access key for Hasura GraphQL Engine")
|
2021-10-13 17:38:07 +03:00
|
|
|
if err := f.MarkDeprecated("access-key", "use --admin-secret instead"); err != nil {
|
|
|
|
ec.Logger.WithError(err).Errorf("error while using a dependency library")
|
|
|
|
}
|
2020-04-28 14:59:57 +03:00
|
|
|
f.Bool("insecure-skip-tls-verify", false, "skip TLS verification and disable cert checking (default: false)")
|
|
|
|
f.String("certificate-authority", "", "path to a cert file for the certificate authority")
|
2020-04-08 14:55:42 +03:00
|
|
|
|
|
|
|
util.BindPFlag(v, "endpoint", f.Lookup("endpoint"))
|
|
|
|
util.BindPFlag(v, "admin_secret", f.Lookup("admin-secret"))
|
|
|
|
util.BindPFlag(v, "access_key", f.Lookup("access-key"))
|
2020-04-28 14:59:57 +03:00
|
|
|
util.BindPFlag(v, "insecure_skip_tls_verify", f.Lookup("insecure-skip-tls-verify"))
|
|
|
|
util.BindPFlag(v, "certificate_authority", f.Lookup("certificate-authority"))
|
2020-02-24 19:14:46 +03:00
|
|
|
|
|
|
|
return actionsCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCodegenFrameworks() (allFrameworks []codegenFramework, err error) {
|
2022-11-07 17:33:26 +03:00
|
|
|
var op errors.Op = "commands.getCodegenFrameworks"
|
2020-02-24 19:14:46 +03:00
|
|
|
frameworkFileBytes, err := ioutil.ReadFile(filepath.Join(ec.GlobalConfigDir, util.ActionsCodegenDirName, "frameworks.json"))
|
|
|
|
if err != nil {
|
2022-11-07 17:33:26 +03:00
|
|
|
return allFrameworks, errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(frameworkFileBytes, &allFrameworks)
|
|
|
|
if err != nil {
|
2022-11-07 17:33:26 +03:00
|
|
|
return allFrameworks, errors.E(op, err)
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|
2022-11-07 17:33:26 +03:00
|
|
|
return allFrameworks, nil
|
2020-02-24 19:14:46 +03:00
|
|
|
}
|