From a759fd1544c967f947be58a048d54bc760cabfa6 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 5 Jul 2023 17:06:19 -0700 Subject: [PATCH 1/2] cmd/vet: Add --no-db option --- internal/cmd/cmd.go | 4 ++++ internal/cmd/vet.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 762e42a677..ce32648d9e 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -37,6 +37,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int rootCmd.PersistentFlags().StringP("file", "f", "", "specify an alternate config file (default: sqlc.yaml)") rootCmd.PersistentFlags().BoolP("experimental", "x", false, "DEPRECATED: enable experimental features (default: false)") rootCmd.PersistentFlags().Bool("no-remote", false, "disable remote execution (default: false)") + rootCmd.PersistentFlags().Bool("no-db", false, "disable database connections (default: false)") rootCmd.AddCommand(checkCmd) rootCmd.AddCommand(diffCmd) @@ -136,15 +137,18 @@ type Env struct { DryRun bool Debug opts.Debug NoRemote bool + NoDB bool } func ParseEnv(c *cobra.Command) Env { dr := c.Flag("dry-run") nr := c.Flag("no-remote") + nodb := c.Flag("no-db") return Env{ DryRun: dr != nil && dr.Changed, Debug: opts.DebugFromEnv(), NoRemote: nr != nil && nr.Value.String() == "true", + NoDB: nodb != nil && nodb.Value.String() == "true", } } diff --git a/internal/cmd/vet.go b/internal/cmd/vet.go index 19040cf8ce..67cb32d009 100644 --- a/internal/cmd/vet.go +++ b/internal/cmd/vet.go @@ -132,6 +132,7 @@ func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) err Envmap: map[string]string{}, Msgs: msgs, Stderr: stderr, + NoDB: e.NoDB, } errored := false for _, sql := range conf.SQL { @@ -207,6 +208,7 @@ type checker struct { Envmap map[string]string Msgs map[string]string Stderr io.Writer + NoDB bool } func (c *checker) DSN(dsn string) (string, error) { @@ -250,6 +252,9 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error { // TODO: Add MySQL support var prep preparer if s.Database != nil { + if c.NoDB { + return fmt.Errorf("database: refusing to connect since the --no-db flag was specified") + } dburl, err := c.DSN(s.Database.URL) if err != nil { return err From fe6782d772243b7545c31ae3165fe2b65008c2c3 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 5 Jul 2023 17:12:40 -0700 Subject: [PATCH 2/2] docs: Update cli flag --- internal/cmd/cmd.go | 20 ++++++++++---------- internal/cmd/vet.go | 36 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index ce32648d9e..aec6eb1214 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -37,7 +37,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int rootCmd.PersistentFlags().StringP("file", "f", "", "specify an alternate config file (default: sqlc.yaml)") rootCmd.PersistentFlags().BoolP("experimental", "x", false, "DEPRECATED: enable experimental features (default: false)") rootCmd.PersistentFlags().Bool("no-remote", false, "disable remote execution (default: false)") - rootCmd.PersistentFlags().Bool("no-db", false, "disable database connections (default: false)") + rootCmd.PersistentFlags().Bool("no-database", false, "disable database connections (default: false)") rootCmd.AddCommand(checkCmd) rootCmd.AddCommand(diffCmd) @@ -134,21 +134,21 @@ var initCmd = &cobra.Command{ } type Env struct { - DryRun bool - Debug opts.Debug - NoRemote bool - NoDB bool + DryRun bool + Debug opts.Debug + NoRemote bool + NoDatabase bool } func ParseEnv(c *cobra.Command) Env { dr := c.Flag("dry-run") nr := c.Flag("no-remote") - nodb := c.Flag("no-db") + nodb := c.Flag("no-database") return Env{ - DryRun: dr != nil && dr.Changed, - Debug: opts.DebugFromEnv(), - NoRemote: nr != nil && nr.Value.String() == "true", - NoDB: nodb != nil && nodb.Value.String() == "true", + DryRun: dr != nil && dr.Changed, + Debug: opts.DebugFromEnv(), + NoRemote: nr != nil && nr.Value.String() == "true", + NoDatabase: nodb != nil && nodb.Value.String() == "true", } } diff --git a/internal/cmd/vet.go b/internal/cmd/vet.go index 67cb32d009..d4b2efbff0 100644 --- a/internal/cmd/vet.go +++ b/internal/cmd/vet.go @@ -125,14 +125,14 @@ func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) err } c := checker{ - Checks: checks, - Conf: conf, - Dir: dir, - Env: env, - Envmap: map[string]string{}, - Msgs: msgs, - Stderr: stderr, - NoDB: e.NoDB, + Checks: checks, + Conf: conf, + Dir: dir, + Env: env, + Envmap: map[string]string{}, + Msgs: msgs, + Stderr: stderr, + NoDatabase: e.NoDatabase, } errored := false for _, sql := range conf.SQL { @@ -201,14 +201,14 @@ func (p *dbPreparer) Prepare(ctx context.Context, name, query string) error { } type checker struct { - Checks map[string]cel.Program - Conf *config.Config - Dir string - Env *cel.Env - Envmap map[string]string - Msgs map[string]string - Stderr io.Writer - NoDB bool + Checks map[string]cel.Program + Conf *config.Config + Dir string + Env *cel.Env + Envmap map[string]string + Msgs map[string]string + Stderr io.Writer + NoDatabase bool } func (c *checker) DSN(dsn string) (string, error) { @@ -252,8 +252,8 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error { // TODO: Add MySQL support var prep preparer if s.Database != nil { - if c.NoDB { - return fmt.Errorf("database: refusing to connect since the --no-db flag was specified") + if c.NoDatabase { + return fmt.Errorf("database: connections disabled via command line flag") } dburl, err := c.DSN(s.Database.URL) if err != nil {