Skip to content

Commit

Permalink
Fix several breaking profile tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macneale4 committed Dec 18, 2024
1 parent bb904f9 commit 4b428c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
70 changes: 34 additions & 36 deletions go/cmd/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func runMain() int {
return status
}

// NM4 - can we use the tmpEnv here??
dEnv := env.Load(ctx, env.GetCurrentUserHomeDir, cfg.dataDirFS, doltdb.LocalDirDoltDB, doltversion.Version)
if dEnv.CfgLoadErr != nil {
cli.PrintErrln(color.RedString("Failed to load the global config. %v", dEnv.CfgLoadErr))
Expand Down Expand Up @@ -627,6 +628,10 @@ func resolveDataDir(gArgs *argparser.ArgParseResults, subCmd string, remainingAr
return dd, nil
}

if globalDir == "" {
globalDir, _ = fs.Abs("")
}

return globalDir, nil
}

Expand Down Expand Up @@ -849,6 +854,8 @@ type TheConfig struct {
homeDir string
}

// NM4 - This name needs to be updated.

// parseGlobalArgsAndSubCommandName parses the global arguments, including a profile if given or a default profile if exists. Also returns the subcommand name.
func parseGlobalArgsAndSubCommandName(ctx context.Context, args []string) (cfg *TheConfig, terminate bool, status int) {
var fs filesys.Filesys
Expand Down Expand Up @@ -900,24 +907,8 @@ func parseGlobalArgsAndSubCommandName(ctx context.Context, args []string) (cfg *
}
subCommand := remainingArgs[0]

// Current working directory is preserved to ensure that user provided path arguments are always calculated
// 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, remainingArgs[1:], fs)
if err != nil {
cli.PrintErrln(color.RedString("Failed to resolve the data directory: %v", err))
return nil, true, 1
}

dataDirFS, err := fs.WithWorkingDir(dataDir)
if err != nil {
cli.PrintErrln(color.RedString("Failed to set the data directory to: %s. %v", dataDir, err))
return nil, true, 1
}

// If there is a profile flag, we want to load the profile and inject it's args into the global args.
useDefaultProfile := false

profileName, hasProfile := apr.GetValue(commands.ProfileFlag)
encodedProfiles, err := globalConfig.GetString(commands.GlobalCfgProfileKey)
if err != nil {
Expand All @@ -933,39 +924,44 @@ func parseGlobalArgsAndSubCommandName(ctx context.Context, args []string) (cfg *
return nil, true, 1
}
}

profiles, err := commands.DecodeProfile(encodedProfiles)
profilesJson, err := commands.DecodeProfile(encodedProfiles)
if err != nil {
cli.PrintErrln(color.RedString("Failed to decode profiles: %v", err))
return nil, true, 1
}

if !hasProfile && supportsGlobalArgs(subCommand) {
defaultProfile := gjson.Get(profiles, commands.DefaultProfileName)
defaultProfile := gjson.Get(profilesJson, commands.DefaultProfileName)
if defaultProfile.Exists() {
/*
/// NM4 - don't do this.
args = append([]string{"--profile", commands.DefaultProfileName}, args...)
apr, remaining, err = globalArgParser.ParseGlobalArgs(args)
if err != nil {
return nil, nil, "", err
}
profileName, _ = apr.GetValue(commands.ProfileFlag)
useDefaultProfile = true
*/
panic("TODO: implement this")
profileName = commands.DefaultProfileName
useDefaultProfile = true
}
}

if hasProfile || useDefaultProfile {
apr, err = injectProfileArgs(apr, profileName, profiles)
apr, err = injectProfileArgs(apr, profileName, profilesJson)
if err != nil {
cli.PrintErrln(color.RedString("Failed to inject profile arguments: %v", err))
return nil, true, 1
}
}

// Current working directory is preserved to ensure that user provided path arguments are always calculated
// 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, remainingArgs[1:], fs)
if err != nil {
cli.PrintErrln(color.RedString("Failed to resolve the data directory: %v", err))
return nil, true, 1
}

dataDirFS, err := fs.WithWorkingDir(dataDir)
if err != nil {
cli.PrintErrln(color.RedString("Failed to set the data directory to: %s. %v", dataDir, err))
return nil, true, 1
}

cfg = &TheConfig{
apr: apr,
remainingArgs: remainingArgs,
Expand All @@ -979,10 +975,12 @@ func parseGlobalArgsAndSubCommandName(ctx context.Context, args []string) (cfg *
return cfg, false, 0
}

// NM4 -Update DOCs
// getProfile retrieves the given profile from the provided list of profiles and returns the args (as flags) and values
// for that profile in a []string. If the profile is not found, an error is returned.
func injectProfileArgs(apr *argparser.ArgParseResults, profileName, profiles string) (aprUpdated *argparser.ArgParseResults, err error) {
prof := gjson.Get(profiles, profileName)
aprUpdated = apr
if prof.Exists() {
hasPassword := false
password := ""
Expand All @@ -995,17 +993,17 @@ func injectProfileArgs(apr *argparser.ArgParseResults, profileName, profiles str
} else if flag == cli.NoTLSFlag {
if value.Bool() {
// NM4 - I don't think this is right. Test it, or make another accessor.
aprUpdated = apr.InsertArgument(flag, "true")
aprUpdated = aprUpdated.InsertArgument(flag, "true")
}
} else {
if value.Str != "" {
aprUpdated = apr.InsertArgument(flag, value.Str)
aprUpdated = aprUpdated.InsertArgument(flag, value.Str)
}
}
}
}
if !apr.Contains(cli.PasswordFlag) && hasPassword {
aprUpdated = apr.InsertArgument(cli.PasswordFlag, password)
aprUpdated = aprUpdated.InsertArgument(cli.PasswordFlag, password)
}
return aprUpdated, nil
} else {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/bats/profile.bats
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ teardown() {

run dolt --profile nonExistentProfile sql -q "select * from altDB_tbl"
[ "$status" -eq 1 ]
[[ "$output" =~ "Failure to parse arguments: profile nonExistentProfile not found" ]] || false
[[ "$output" =~ "Failed to inject profile arguments: profile nonExistentProfile not found" ]] || false
}

@test "profile: additional flag gets used" {
Expand Down

0 comments on commit 4b428c4

Please sign in to comment.