diff --git a/cmd/schema.go b/cmd/schema.go index 36565eb792..ea23c02b9b 100644 --- a/cmd/schema.go +++ b/cmd/schema.go @@ -37,6 +37,7 @@ type SchemaCmd struct { target string targetProfile string filePrefix string // TODO: move filePrefix to global flags + dry_run bool } // Name returns the name of operation. @@ -67,6 +68,7 @@ func (cmd *SchemaCmd) SetFlags(f *flag.FlagSet) { f.StringVar(&cmd.target, "target", "Spanner", "Specifies the target DB, defaults to Spanner (accepted values: `Spanner`)") f.StringVar(&cmd.targetProfile, "target-profile", "", "Flag for specifying connection profile for target database e.g., \"dialect=postgresql\"") f.StringVar(&cmd.filePrefix, "prefix", "", "File prefix for generated files") + f.BoolVar(&cmd.dry_run, "dry-run", false, "Flag for specifying dry run mode for a command") } func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { @@ -78,7 +80,8 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa fmt.Printf("FATAL error: %v\n", err) } }() - + internal.DryRunInit(cmd.dry_run) + fmt.Printf("Dry run mode: %v\n", cmd.dry_run) sourceProfile, err := profiles.NewSourceProfile(cmd.sourceProfile, cmd.source) if err != nil { return subcommands.ExitUsageError @@ -98,7 +101,14 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa if sourceProfile.Ty == profiles.SourceProfileTypeFile && (sourceProfile.File.Format == "" || sourceProfile.File.Format == "dump") { dumpFilePath = sourceProfile.File.Path } + + if internal.DryRun() { + fmt.Println("Command successfully validated.") + return subcommands.ExitSuccess + } + ioHelper := utils.NewIOStreams(sourceProfile.Driver, dumpFilePath) + if ioHelper.SeekableIn != nil { defer ioHelper.In.Close() } @@ -112,7 +122,6 @@ func (cmd *SchemaCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interfa } cmd.filePrefix = dbName + "." } - schemaConversionStartTime := time.Now() var conv *internal.Conv conv, err = conversion.SchemaConv(sourceProfile, targetProfile, &ioHelper) diff --git a/internal/dry_run.go b/internal/dry_run.go new file mode 100644 index 0000000000..3fdaa021c2 --- /dev/null +++ b/internal/dry_run.go @@ -0,0 +1,27 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package internal + +var enabled = false + +// DryRun returns true if dry run mode is enabled. +func DryRun() bool { + return enabled +} + +// DryRunInit determines whether dry run mode is enabled. +// Generally there should be one call to DryRunInit at startup. +func DryRunInit(b bool) { + enabled = b +}