Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
macneale4 committed Dec 17, 2024
1 parent 4f30148 commit 6d9d278
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
33 changes: 33 additions & 0 deletions go/cmd/dolt/commands/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,39 @@ func StartServer(ctx context.Context, versionStr, commandStr string, args []stri
return nil
}

// GetDataDirPreStart returns the data dir to use for the process. This is called early in the bootstrapping of the process
// to ensure that we load the database once and only once. Later in sql-server start up we validate that the datadir
// hasn't changed for unexpected reasons.
func GetDataDirPreStart(fs filesys.Filesys, args []string) (string, error) {
ap := SqlServerCmd{}.ArgParser()
apr, err := cli.ParseArgs(ap, args, nil)
if err != nil {
// Parse failure at this stage is ignored. We'll handle it during command execution.
return "", nil
}

dataDir, hasDataDirArg := apr.GetValue(commands.DataDirFlag)
if hasDataDirArg {
// NM4 - ensure that dd is not specified in the config file before returning???
return dataDir, nil
}

confArg, hasConfArg := apr.GetValue(configFileFlag)
if hasConfArg {
reader := DoltServerConfigReader{}
cfg, err := reader.ReadConfigFile(fs, confArg)
if err != nil {
return "", err
}

if cfg.DataDir() != "" {
return cfg.DataDir(), nil
}
}

return fs.Abs("")
}

// ServerConfigFromArgs returns a ServerConfig from the given args
func ServerConfigFromArgs(ap *argparser.ArgParser, help cli.UsagePrinter, args []string, dEnv *env.DoltEnv) (servercfg.ServerConfig, error) {
return ServerConfigFromArgsWithReader(ap, help, args, dEnv, DoltServerConfigReader{})
Expand Down
28 changes: 20 additions & 8 deletions go/cmd/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,19 @@ func runMain() int {
var fs filesys.Filesys
fs = filesys.LocalFS
tmpEnv := env.LoadWithoutDB(ctx, env.GetCurrentUserHomeDir, fs, doltversion.Version)
globalConfig, ok := tmpEnv.Config.GetConfig(env.GlobalConfig)
if !ok {
cli.PrintErrln(color.RedString("Failed to get global config"))
return 1
var globalConfig config.ReadWriteConfig
if tmpEnv.Config != nil {
// NM4 - Not sure if this is common or not. Going to see if test bark on this.
var ok bool
globalConfig, ok = tmpEnv.Config.GetConfig(env.GlobalConfig)
if !ok {
cli.PrintErrln(color.RedString("Failed to get global config"))
return 1
}
} else {
panic("tmpEnv.Config is nil")
}

cfg, err := parseGlobalArgsAndSubCommandName(globalConfig, fs, args)
if err != nil {
return 0 // NM4
Expand Down Expand Up @@ -629,7 +637,7 @@ or check the docs for questions about usage.`)
return res
}

func resolveDataDir(gArgs *argparser.ArgParseResults, subCmd string, fs filesys.Filesys) (string, error) {
func resolveDataDir(gArgs *argparser.ArgParseResults, subCmd string, remainingArgs []string, fs filesys.Filesys) (string, error) {
// global config is the dolt --data-dir <foo> sub-command version. Applies to most CLI commands.
globalDir, hasGlobalDataDir := gArgs.GetValue(commands.DataDirFlag)
if hasGlobalDataDir {
Expand All @@ -644,8 +652,12 @@ func resolveDataDir(gArgs *argparser.ArgParseResults, subCmd string, fs filesys.
globalDir = dataDir
}

if subCmd == "sql-server" {
// Load the server config??
if subCmd == "sql-server" { // NM4 - const.
dd, err := sqlserver.GetDataDirPreStart(fs, remainingArgs)
if err != nil {
return "", err
}
return dd, nil
}

return globalDir, nil
Expand Down Expand Up @@ -888,7 +900,7 @@ func parseGlobalArgsAndSubCommandName(globalConfig config.ReadWriteConfig, fs fi
// relative to this directory. The root environment's FS will be updated to be the --data-dir path if the user
// specified one.
cwdFS := fs
dataDir, err := resolveDataDir(apr, subCommand, fs)
dataDir, err := resolveDataDir(apr, subCommand, remainingArgs[1:], fs)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6d9d278

Please sign in to comment.