Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/vet: Add --no-database option #2405

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-database", false, "disable database connections (default: false)")

rootCmd.AddCommand(checkCmd)
rootCmd.AddCommand(diffCmd)
Expand Down Expand Up @@ -133,18 +134,21 @@ var initCmd = &cobra.Command{
}

type Env struct {
DryRun bool
Debug opts.Debug
NoRemote 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-database")
return Env{
DryRun: dr != nil && dr.Changed,
Debug: opts.DebugFromEnv(),
NoRemote: nr != nil && nr.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",
}
}

Expand Down
33 changes: 19 additions & 14 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +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,
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 {
Expand Down Expand Up @@ -200,13 +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
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) {
Expand Down Expand Up @@ -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.NoDatabase {
return fmt.Errorf("database: connections disabled via command line flag")
}
dburl, err := c.DSN(s.Database.URL)
if err != nil {
return err
Expand Down