Fail early when running CliContext-using-commands outside of a repo, with an exception for specific commands.

This commit is contained in:
Nick Tobey
2023-06-02 12:21:53 -07:00
parent bef50ffebd
commit d5e3d075be
+9 -2
View File
@@ -529,7 +529,7 @@ The sql subcommand is currently the only command that uses these flags. All othe
var cliCtx cli.CliContext = nil
if initCliContext(subcommandName) {
lateBind, err := buildLateBinder(ctx, dEnv.FS, mrEnv, apr, verboseEngineSetup)
lateBind, err := buildLateBinder(ctx, dEnv.FS, mrEnv, apr, subcommandName, verboseEngineSetup)
if err != nil {
cli.PrintErrln(color.RedString("Failure to Load SQL Engine: %v", err))
return 1
@@ -562,7 +562,7 @@ The sql subcommand is currently the only command that uses these flags. All othe
return res
}
func buildLateBinder(ctx context.Context, cwdFS filesys.Filesys, mrEnv *env.MultiRepoEnv, apr *argparser.ArgParseResults, verbose bool) (cli.LateBindQueryist, error) {
func buildLateBinder(ctx context.Context, cwdFS filesys.Filesys, mrEnv *env.MultiRepoEnv, apr *argparser.ArgParseResults, subcommandName string, verbose bool) (cli.LateBindQueryist, error) {
var targetEnv *env.DoltEnv = nil
@@ -580,6 +580,13 @@ func buildLateBinder(ctx context.Context, cwdFS filesys.Filesys, mrEnv *env.Mult
targetEnv = mrEnv.GetEnv(useDb)
}
// There is no target environment detected. This is allowed only for a small number of commands.
// We don't expect that number to grow, so we list them here.
isDoltEnvironmentRequired := subcommandName != "init" && subcommandName != "sql" && subcommandName != "sql-server"
if targetEnv == nil && isDoltEnvironmentRequired {
return nil, fmt.Errorf("The current directory is not a valid dolt repository.")
}
// nil targetEnv will happen if the user ran a command in an empty directory - which we support in some cases.
if targetEnv != nil {
isLocked, lock, err := targetEnv.GetLock()