Skip to content

Commit

Permalink
Merge branch 'master' into issue-3969
Browse files Browse the repository at this point in the history
  • Loading branch information
shahidhk committed Mar 26, 2020
2 parents 89a49f0 + 09e0e2f commit 4ee67bf
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

### Other changes

- cli: fix init command to generate correct config.yaml (close #4036)
- cli: fix command path not being set in telemetry data (close #4127)
- fix deploy script to upload github release assets
- cli: fix parse error returned on console api (close #4126)
- console and cli-ext: fix parsing of wrapped types in SDL
Expand Down
49 changes: 45 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
package cli

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -59,6 +61,42 @@ const (
V2
)

// ErrInvalidConfigVersion - if the config version is not valid
var ErrInvalidConfigVersion error = fmt.Errorf("invalid config version")

// NewConfigVersionValue returns ConfigVersion set with default value
func NewConfigVersionValue(val ConfigVersion, p *ConfigVersion) *ConfigVersion {
*p = val
return p
}

// Set sets the value of the named command-line flag.
func (c *ConfigVersion) Set(s string) error {
v, err := strconv.ParseInt(s, 0, 64)
*c = ConfigVersion(v)
if err != nil {
return err
}
if !c.IsValid() {
return ErrInvalidConfigVersion
}
return nil
}

// Type returns a string that uniquely represents this flag's type.
func (c *ConfigVersion) Type() string {
return "int"
}

func (c *ConfigVersion) String() string {
return strconv.Itoa(int(*c))
}

// IsValid returns if its a valid config version
func (c ConfigVersion) IsValid() bool {
return c != 0 && c <= V2
}

// ServerConfig has the config values required to contact the server
type ServerConfig struct {
// Endpoint for the GraphQL Engine
Expand All @@ -84,17 +122,17 @@ func (s *ServerConfig) ParseEndpoint() error {
// Config represents configuration required for the CLI to function
type Config struct {
// Version of the config.
Version ConfigVersion `yaml:"version"`
Version ConfigVersion `yaml:"version,omitempty"`

// 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"`
MetadataDirectory string `yaml:"metadata_directory,omitempty"`
// 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"`
ActionConfig *types.ActionExecutionConfig `yaml:"actions,omitempty"`
}

// ExecutionContext contains various contextual information required by the cli
Expand Down Expand Up @@ -427,7 +465,7 @@ func (ec *ExecutionContext) readConfig() error {
},
MetadataDirectory: v.GetString("metadata_directory"),
MigrationsDirectory: v.GetString("migrations_directory"),
ActionConfig: types.ActionExecutionConfig{
ActionConfig: &types.ActionExecutionConfig{
Kind: v.GetString("actions.kind"),
HandlerWebhookBaseURL: v.GetString("actions.handler_webhook_baseurl"),
Codegen: &types.CodegenExecutionConfig{
Expand All @@ -437,6 +475,9 @@ func (ec *ExecutionContext) readConfig() error {
},
},
}
if !ec.Config.Version.IsValid() {
return ErrInvalidConfigVersion
}
return ec.Config.ServerConfig.ParseEndpoint()
}

Expand Down
1 change: 1 addition & 0 deletions cli/commands/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewActionsCmd(ec *cli.ExecutionContext) *cobra.Command {
Short: "Manage actions on hasura",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
ec.Viper = v
err := ec.Prepare()
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cli/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command {
}

f := initCmd.Flags()
f.StringVar(&opts.Version, "version", "2", "config version to be used")
f.Var(cli.NewConfigVersionValue(cli.V2, &opts.Version), "version", "config version to be used")
f.StringVar(&opts.InitDir, "directory", "", "name of directory where files will be created")
f.StringVar(&opts.MetadataDir, "metadata-directory", "metadata", "name of directory where metadata files will be created")
f.StringVar(&opts.Endpoint, "endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
Expand All @@ -83,7 +83,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command {
type InitOptions struct {
EC *cli.ExecutionContext

Version string
Version cli.ConfigVersion
Endpoint string
AdminSecret string
InitDir string
Expand Down Expand Up @@ -163,20 +163,20 @@ func (o *InitOptions) createFiles() error {
}
// set config object
var config *cli.Config
if o.Version == "1" {
if o.Version == cli.V1 {
config = &cli.Config{
ServerConfig: cli.ServerConfig{
Endpoint: "http://localhost:8080",
},
}
} else {
config = &cli.Config{
Version: cli.V2,
Version: o.Version,
ServerConfig: cli.ServerConfig{
Endpoint: "http://localhost:8080",
},
MetadataDirectory: o.MetadataDir,
ActionConfig: types.ActionExecutionConfig{
ActionConfig: &types.ActionExecutionConfig{
Kind: o.ActionKind,
HandlerWebhookBaseURL: o.ActionHandler,
},
Expand Down
1 change: 1 addition & 0 deletions cli/commands/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
Aliases: []string{"md"},
Short: "Manage Hasura GraphQL Engine metadata saved in the database",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
ec.Viper = v
err := ec.Prepare()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
Short: "Manage migrations on the database",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
ec.Viper = v
err := ec.Prepare()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cli/commands/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ https://github.com/hasura/cli-plugins-index
Please open pull requests against this repo to add new plugins`,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cmd.Root().PersistentPreRun(cmd, args)
return ec.PluginsConfig.Repo.EnsureCloned()
},
}
Expand Down
5 changes: 2 additions & 3 deletions cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var rootCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
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{
Expand Down Expand Up @@ -116,10 +114,11 @@ func Execute() error {
if err != nil {
return errors.Wrap(err, "preparing execution context failed")
}
err = NewDefaultHasuraCommand().Execute()
execCmd, err := NewDefaultHasuraCommand().ExecuteC()
if err != nil {
ec.Telemetry.IsError = true
}
ec.Telemetry.Command = execCmd.CommandPath()
ec.Telemetry.Beam()
if ec.Spinner != nil {
ec.Spinner.Stop()
Expand Down
2 changes: 1 addition & 1 deletion cli/integration_test/v1/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestInitCmd(t *testing.T, ec *cli.ExecutionContext, initDir string) {
}{
{"only-init-dir", &commands.InitOptions{
EC: ec,
Version: "1",
Version: cli.V1,
Endpoint: os.Getenv("HASURA_GRAPHQL_TEST_ENDPOINT"),
AdminSecret: os.Getenv("HASURA_GRAPHQL_TEST_ADMIN_SECRET"),
InitDir: filepath.Join(os.TempDir(), "hasura-cli-test-"+strconv.Itoa(rand.Intn(1000))),
Expand Down
2 changes: 1 addition & 1 deletion cli/integration_test/v2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestInitCmd(t *testing.T, ec *cli.ExecutionContext, initDir string) {
}{
{"only-init-dir", &commands.InitOptions{
EC: ec,
Version: "2",
Version: cli.V2,
Endpoint: os.Getenv("HASURA_GRAPHQL_TEST_ENDPOINT"),
AdminSecret: os.Getenv("HASURA_GRAPHQL_TEST_ADMIN_SECRET"),
InitDir: filepath.Join(os.TempDir(), "hasura-cli-test-"+strconv.Itoa(rand.Intn(1000))),
Expand Down
2 changes: 1 addition & 1 deletion cli/metadata/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (

type ActionConfig struct {
MetadataDir string
ActionConfig types.ActionExecutionConfig
ActionConfig *types.ActionExecutionConfig
serverFeatureFlags *version.ServerFeatureFlags
pluginsCfg *plugins.Config
cliExtensionConfig *cliextension.Config
Expand Down

0 comments on commit 4ee67bf

Please sign in to comment.