From 685cf6256d6265b744812e3bb306343fbbbbe272 Mon Sep 17 00:00:00 2001 From: Aravind Shankar Date: Fri, 6 Mar 2020 04:14:39 +0530 Subject: [PATCH 1/5] cli: fix init command to generate correct config.yaml for v1 --- cli/cli.go | 8 ++++---- cli/commands/init.go | 8 ++++---- cli/metadata/actions/actions.go | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index acc34586b60f3..046387e177c58 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -84,17 +84,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 @@ -427,7 +427,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{ diff --git a/cli/commands/init.go b/cli/commands/init.go index 91474fd42b8ad..7b7976367e98e 100644 --- a/cli/commands/init.go +++ b/cli/commands/init.go @@ -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.IntVar(&opts.Version, "version", 2, "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") @@ -83,7 +83,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command { type InitOptions struct { EC *cli.ExecutionContext - Version string + Version int Endpoint string AdminSecret string InitDir string @@ -163,7 +163,7 @@ func (o *InitOptions) createFiles() error { } // set config object var config *cli.Config - if o.Version == "1" { + if cli.ConfigVersion(o.Version) == cli.V1 { config = &cli.Config{ ServerConfig: cli.ServerConfig{ Endpoint: "http://localhost:8080", @@ -176,7 +176,7 @@ func (o *InitOptions) createFiles() error { Endpoint: "http://localhost:8080", }, MetadataDirectory: o.MetadataDir, - ActionConfig: types.ActionExecutionConfig{ + ActionConfig: &types.ActionExecutionConfig{ Kind: o.ActionKind, HandlerWebhookBaseURL: o.ActionHandler, }, diff --git a/cli/metadata/actions/actions.go b/cli/metadata/actions/actions.go index 6cff86b34f29e..1aa9ea5a56c04 100644 --- a/cli/metadata/actions/actions.go +++ b/cli/metadata/actions/actions.go @@ -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 From 709f18eb8c6d413749a135a02e0e93a87cb0be5b Mon Sep 17 00:00:00 2001 From: Aravind Shankar Date: Fri, 6 Mar 2020 04:22:53 +0530 Subject: [PATCH 2/5] fix test --- cli/integration_test/v1/init.go | 2 +- cli/integration_test/v2/init.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/integration_test/v1/init.go b/cli/integration_test/v1/init.go index bdc3fa80127b8..55735a576481c 100644 --- a/cli/integration_test/v1/init.go +++ b/cli/integration_test/v1/init.go @@ -24,7 +24,7 @@ func TestInitCmd(t *testing.T, ec *cli.ExecutionContext, initDir string) { }{ {"only-init-dir", &commands.InitOptions{ EC: ec, - Version: "1", + Version: 1, 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))), diff --git a/cli/integration_test/v2/init.go b/cli/integration_test/v2/init.go index c50c80d1b046e..b5aa77356cff8 100644 --- a/cli/integration_test/v2/init.go +++ b/cli/integration_test/v2/init.go @@ -24,7 +24,7 @@ func TestInitCmd(t *testing.T, ec *cli.ExecutionContext, initDir string) { }{ {"only-init-dir", &commands.InitOptions{ EC: ec, - Version: "2", + Version: 2, 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))), From fa4609111a7b63c92b990615e490d9bfe5843842 Mon Sep 17 00:00:00 2001 From: Aravind Shankar Date: Thu, 12 Mar 2020 19:41:05 +0530 Subject: [PATCH 3/5] implement pflag.Value interface on configVersion type --- cli/cli.go | 41 +++++++++++++++++++++++++++++++++++++++++ cli/commands/init.go | 8 ++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 046387e177c58..4a0344e9de3f8 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -8,10 +8,12 @@ package cli import ( + "fmt" "io/ioutil" "net/url" "os" "path/filepath" + "strconv" "strings" "time" @@ -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 @@ -437,6 +475,9 @@ func (ec *ExecutionContext) readConfig() error { }, }, } + if !ec.Config.Version.IsValid() { + return ErrInvalidConfigVersion + } return ec.Config.ServerConfig.ParseEndpoint() } diff --git a/cli/commands/init.go b/cli/commands/init.go index 7b7976367e98e..e155af340c4d4 100644 --- a/cli/commands/init.go +++ b/cli/commands/init.go @@ -65,7 +65,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command { } f := initCmd.Flags() - f.IntVar(&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") @@ -83,7 +83,7 @@ func NewInitCmd(ec *cli.ExecutionContext) *cobra.Command { type InitOptions struct { EC *cli.ExecutionContext - Version int + Version cli.ConfigVersion Endpoint string AdminSecret string InitDir string @@ -163,7 +163,7 @@ func (o *InitOptions) createFiles() error { } // set config object var config *cli.Config - if cli.ConfigVersion(o.Version) == cli.V1 { + if o.Version == cli.V1 { config = &cli.Config{ ServerConfig: cli.ServerConfig{ Endpoint: "http://localhost:8080", @@ -171,7 +171,7 @@ func (o *InitOptions) createFiles() error { } } else { config = &cli.Config{ - Version: cli.V2, + Version: o.Version, ServerConfig: cli.ServerConfig{ Endpoint: "http://localhost:8080", }, From 23ed13ae9bd8b541b21d4887d7583d0779e87c80 Mon Sep 17 00:00:00 2001 From: Aravind Shankar Date: Thu, 12 Mar 2020 19:42:32 +0530 Subject: [PATCH 4/5] fix test --- cli/integration_test/v1/init.go | 2 +- cli/integration_test/v2/init.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/integration_test/v1/init.go b/cli/integration_test/v1/init.go index 55735a576481c..1401bf544d28b 100644 --- a/cli/integration_test/v1/init.go +++ b/cli/integration_test/v1/init.go @@ -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))), diff --git a/cli/integration_test/v2/init.go b/cli/integration_test/v2/init.go index b5aa77356cff8..55aa7665681ea 100644 --- a/cli/integration_test/v2/init.go +++ b/cli/integration_test/v2/init.go @@ -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))), From a8badcd4765872b9e7a4bbd1a3f3c380c639e672 Mon Sep 17 00:00:00 2001 From: Aravind Shankar Date: Wed, 25 Mar 2020 16:05:18 +0530 Subject: [PATCH 5/5] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index af1298ff6c4c8..ba21394c551ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ ### Other changes +- cli: fix init command to generate correct config.yaml (close #4036) - console and cli-ext: fix parsing of wrapped types in SDL - cli: fix typo in cli example for squash (fix #4047) (#4049) - console: fix run_sql migration modal messaging (close #4020) (#4060)