Skip to content

Commit

Permalink
Add the cwd to cliCtx and use it in server config resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
macneale4 committed Dec 23, 2024
1 parent 2e97e0a commit e1d400f
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 38 deletions.
16 changes: 14 additions & 2 deletions go/cmd/dolt/cli/cli_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"

"github.com/dolthub/dolt/go/libraries/utils/filesys"
"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/dolt/go/cmd/dolt/errhand"
Expand All @@ -39,17 +40,23 @@ type LateBindQueryist func(ctx context.Context) (Queryist, *sql.Context, func(),
type CliContext interface {
// GlobalArgs returns the arguments passed before the subcommand.
GlobalArgs() *argparser.ArgParseResults
WorkingDir() filesys.Filesys
Config() *env.DoltCliConfig
QueryEngine(ctx context.Context) (Queryist, *sql.Context, func(), error)
}

// NewCliContext creates a new CliContext instance. Arguments must not be nil.
func NewCliContext(args *argparser.ArgParseResults, config *env.DoltCliConfig, latebind LateBindQueryist) (CliContext, errhand.VerboseError) {
func NewCliContext(args *argparser.ArgParseResults, config *env.DoltCliConfig, cwd filesys.Filesys, latebind LateBindQueryist) (CliContext, errhand.VerboseError) {
if args == nil || config == nil || latebind == nil {
return nil, errhand.VerboseErrorFromError(errors.New("Invariant violated. args, config, and latebind must be non nil."))
}

return LateBindCliContext{globalArgs: args, config: config, activeContext: &QueryistContext{}, bind: latebind}, nil
return LateBindCliContext{
globalArgs: args,
config: config,
cwd: cwd,
activeContext: &QueryistContext{},
bind: latebind}, nil
}

type QueryistContext struct {
Expand All @@ -62,6 +69,7 @@ type QueryistContext struct {
// created once.
type LateBindCliContext struct {
globalArgs *argparser.ArgParseResults
cwd filesys.Filesys
config *env.DoltCliConfig
activeContext *QueryistContext

Expand Down Expand Up @@ -92,6 +100,10 @@ func (lbc LateBindCliContext) QueryEngine(ctx context.Context) (Queryist, *sql.C
return qryist, sqlCtx, closer, nil
}

func (lbc LateBindCliContext) WorkingDir() filesys.Filesys {
return lbc.cwd
}

// Config returns the dolt config stored in CliContext
func (lbc LateBindCliContext) Config() *env.DoltCliConfig {
return lbc.config
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestInit(t *testing.T) {
gCfg.SetStrings(test.GlobalConfig)
apr := argparser.ArgParseResults{}
latebind := func(ctx context.Context) (cli.Queryist, *sql.Context, func(), error) { return nil, nil, func() {}, nil }
cliCtx, _ := cli.NewCliContext(&apr, dEnv.Config, latebind)
cliCtx, _ := cli.NewCliContext(&apr, dEnv.Config, dEnv.FS, latebind)

result := InitCmd{}.Exec(context.Background(), "dolt init", test.Args, dEnv, cliCtx)
defer dEnv.DoltDB.Close()
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/signed_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func execCommand(ctx context.Context, wd string, cmd cli.Command, args []string,
return
}

cliCtx, err := cli.NewCliContext(apr, cfg, latebind)
cliCtx, err := cli.NewCliContext(apr, cfg, dEnv.FS, latebind)
if err != nil {
err = fmt.Errorf("error creating cli context: %w", err)
return
Expand Down
22 changes: 11 additions & 11 deletions go/cmd/dolt/commands/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestSqlConsole(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{}
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestSqlBatchMode(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-b", "-q", test.query}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestSqlSelect(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestSqlShow(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestCreateTable(t *testing.T) {
assert.NoError(t, err)
assert.False(t, has, "table exists before creating it")

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestShowTables(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestAlterTable(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestDropTable(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestInsert(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestUpdate(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down Expand Up @@ -582,7 +582,7 @@ func TestDelete(t *testing.T) {
require.NoError(t, err)
defer dEnv.DoltDB.Close()

cliCtx, err := NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

args := []string{"-q", test.query}
Expand Down
8 changes: 4 additions & 4 deletions go/cmd/dolt/commands/sqlserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestServerArgs(t *testing.T) {
"-t", "5",
"-l", "info",
"-r",
}, dEnv, controller)
}, dEnv, dEnv.FS, controller)
}()
err = controller.WaitForStart()
require.NoError(t, err)
Expand Down Expand Up @@ -118,7 +118,7 @@ listener:
dEnv.FS.WriteFile("config.yaml", []byte(yamlConfig), os.ModePerm)
StartServer(context.Background(), "0.0.0", "dolt sql-server", []string{
"--config", "config.yaml",
}, dEnv, controller)
}, dEnv, dEnv.FS, controller)
}()
err = controller.WaitForStart()
require.NoError(t, err)
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestServerBadArgs(t *testing.T) {
t.Run(strings.Join(test, " "), func(t *testing.T) {
controller := svcs.NewController()
go func() {
StartServer(context.Background(), "test", "dolt sql-server", test, env, controller)
StartServer(context.Background(), "test", "dolt sql-server", test, env, env.FS, controller)
}()
if !assert.Error(t, controller.WaitForStart()) {
controller.Stop()
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestServerFailsIfPortInUse(t *testing.T) {
"-t", "5",
"-l", "info",
"-r",
}, dEnv, controller)
}, dEnv, dEnv.FS, controller)
}()

err = controller.WaitForStart()
Expand Down
22 changes: 15 additions & 7 deletions go/cmd/dolt/commands/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (cmd SqlServerCmd) Exec(ctx context.Context, commandStr string, args []stri
}
}()

err := StartServer(newCtx, cmd.VersionStr, commandStr, args, dEnv, controller)
err := StartServer(newCtx, cmd.VersionStr, commandStr, args, dEnv, cliCtx.WorkingDir(), controller)
if err != nil {
cli.Println(color.RedString(err.Error()))
return 1
Expand All @@ -235,10 +235,10 @@ func validateSqlServerArgs(apr *argparser.ArgParseResults) error {
}

// StartServer starts the sql server with the controller provided and blocks until the server is stopped.
func StartServer(ctx context.Context, versionStr, commandStr string, args []string, dEnv *env.DoltEnv, controller *svcs.Controller) error {
func StartServer(ctx context.Context, versionStr, commandStr string, args []string, dEnv *env.DoltEnv, cwd filesys.Filesys, controller *svcs.Controller) error {
ap := SqlServerCmd{}.ArgParser()
help, _ := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, sqlServerDocs, ap))
serverConfig, err := ServerConfigFromArgs(ap, help, args, dEnv)
serverConfig, err := ServerConfigFromArgs(ap, help, args, dEnv, cwd)
if err != nil {
return err
}
Expand Down Expand Up @@ -308,8 +308,8 @@ func GetDataDirPreStart(fs filesys.Filesys, args []string) (string, error) {
}

// 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{})
func ServerConfigFromArgs(ap *argparser.ArgParser, help cli.UsagePrinter, args []string, dEnv *env.DoltEnv, cwd filesys.Filesys) (servercfg.ServerConfig, error) {
return ServerConfigFromArgsWithReader(ap, help, args, dEnv, cwd, DoltServerConfigReader{})
}

// ServerConfigFromArgsWithReader returns a ServerConfig from the given args, using the provided ServerConfigReader
Expand All @@ -318,6 +318,7 @@ func ServerConfigFromArgsWithReader(
help cli.UsagePrinter,
args []string,
dEnv *env.DoltEnv,
cwd filesys.Filesys,
reader ServerConfigReader,
) (servercfg.ServerConfig, error) {
apr := cli.ParseArgsOrDie(ap, args, help)
Expand All @@ -336,7 +337,7 @@ func ServerConfigFromArgsWithReader(
return nil, fmt.Errorf("bad configuration: %w", err)
}

if err = setupDoltConfig(dEnv, apr, serverConfig); err != nil {
if err = setupDoltConfig(dEnv, cwd, apr, serverConfig); err != nil {
return nil, fmt.Errorf("bad configuration: %w", err)
}

Expand Down Expand Up @@ -408,7 +409,7 @@ func GetClientConfig(cwdFS filesys.Filesys, creds *cli.UserPassword, apr *argpar
}

// setupDoltConfig updates the given server config with where to create .doltcfg directory
func setupDoltConfig(dEnv *env.DoltEnv, apr *argparser.ArgParseResults, config servercfg.ServerConfig) error {
func setupDoltConfig(dEnv *env.DoltEnv, cwd filesys.Filesys, apr *argparser.ArgParseResults, config servercfg.ServerConfig) error {
if _, ok := apr.GetValue(configFileFlag); ok {
return nil
}
Expand All @@ -423,6 +424,13 @@ func setupDoltConfig(dEnv *env.DoltEnv, apr *argparser.ArgParseResults, config s
dataDir := serverConfig.DataDir()
cfgDir, cfgDirSpecified := apr.GetValue(commands.CfgDirFlag)
if cfgDirSpecified {
if !filepath.IsAbs(cfgDir) {
var err error
cfgDir, err = cwd.Abs(cfgDir)
if err != nil {
return err
}
}
cfgDirPath = cfgDir
} else if dataDirSpecified {
cfgDirPath = filepath.Join(dataDir, commands.DefaultCfgDirName)
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/dolt/commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func MaybeGetCommitWithVErr(dEnv *env.DoltEnv, maybeCommit string) (*doltdb.Comm
}

// NewArgFreeCliContext creates a new CliContext instance with no arguments using a local SqlEngine. This is useful for testing primarily
func NewArgFreeCliContext(ctx context.Context, dEnv *env.DoltEnv) (cli.CliContext, errhand.VerboseError) {
func NewArgFreeCliContext(ctx context.Context, dEnv *env.DoltEnv, cwd filesys.Filesys) (cli.CliContext, errhand.VerboseError) {
mrEnv, err := env.MultiEnvForSingleEnv(ctx, dEnv)
if err != nil {
return nil, errhand.VerboseErrorFromError(err)
Expand All @@ -119,7 +119,7 @@ func NewArgFreeCliContext(ctx context.Context, dEnv *env.DoltEnv) (cli.CliContex
if err != nil {
return nil, verr
}
return cli.NewCliContext(argparser.NewEmptyResults(), dEnv.Config, lateBind)
return cli.NewCliContext(argparser.NewEmptyResults(), dEnv.Config, cwd, lateBind)
}

// BuildSqlEngineQueryist Utility function to build a local SQLEngine for use interacting with data on disk using
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var doltSubCommands = []cli.Command{

var commandsWithoutCliCtx = []cli.Command{
admin.Commands,
sqlserver.SqlServerCmd{VersionStr: doltversion.Version},
// NM4??? sqlserver.SqlServerCmd{VersionStr: doltversion.Version},
commands.CloneCmd{},
commands.BackupCmd{},
commands.LoginCmd{},
Expand Down Expand Up @@ -564,7 +564,7 @@ func runMain() int {
return 1
}

cliCtx, err = cli.NewCliContext(cfg.apr, dEnv.Config, lateBind)
cliCtx, err = cli.NewCliContext(cfg.apr, dEnv.Config, cfg.cwdFS, lateBind)
if err != nil {
cli.PrintErrln(color.RedString("Unexpected Error: %v", err))
return 1
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/feature_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (cmd fvCommand) exec(ctx context.Context, dEnv *env.DoltEnv) int {
doltdb.DoltFeatureVersion = cmd.user.vers
defer func() { doltdb.DoltFeatureVersion = DoltFeatureVersionCopy }()

cliCtx, _ := commands.NewArgFreeCliContext(ctx, dEnv)
cliCtx, _ := commands.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)

return cmd.cmd.Exec(ctx, cmd.cmd.Name(), cmd.args, dEnv, cliCtx)
}
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/doltdb/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestForeignKeyErrors(t *testing.T) {

ctx := context.Background()
dEnv := dtestutils.CreateTestEnv()
cliCtx, err := commands.NewArgFreeCliContext(ctx, dEnv)
cliCtx, err := commands.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, err)

for _, c := range cmds {
Expand Down Expand Up @@ -154,7 +154,7 @@ func testForeignKeys(t *testing.T, test foreignKeyTest) {
ctx := context.Background()
dEnv := dtestutils.CreateTestEnv()

cliCtx, verr := commands.NewArgFreeCliContext(ctx, dEnv)
cliCtx, verr := commands.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, verr)

for _, c := range fkSetupCommon {
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/dtestutils/testcommands/multienv.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (mr *MultiRepoTestSetup) NewBranch(dbName, branchName string) {

func (mr *MultiRepoTestSetup) CheckoutBranch(dbName, branchName string) {
dEnv := mr.envs[dbName]
cliCtx, _ := cmd.NewArgFreeCliContext(context.Background(), dEnv)
cliCtx, _ := cmd.NewArgFreeCliContext(context.Background(), dEnv, dEnv.FS)
_, sqlCtx, closeFunc, err := cliCtx.QueryEngine(context.Background())
if err != nil {
mr.Errhand(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestDbRevision(t *testing.T) {
dEnv := dtestutils.CreateTestEnv()
defer dEnv.DoltDB.Close()

cliCtx, _ := cmd.NewArgFreeCliContext(ctx, dEnv)
cliCtx, _ := cmd.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)

setup := append(setupCommon, test.setup...)
for _, c := range setup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var INIT = "" // HEAD~4
func setupHistoryTests(t *testing.T) *env.DoltEnv {
ctx := context.Background()
dEnv := dtestutils.CreateTestEnv()
cliCtx, verr := cmd.NewArgFreeCliContext(ctx, dEnv)
cliCtx, verr := cmd.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, verr)

for _, c := range setupCommon {
Expand All @@ -239,7 +239,7 @@ func setupHistoryTests(t *testing.T) *env.DoltEnv {

func testHistoryTable(t *testing.T, test historyTableTest, dEnv *env.DoltEnv) {
ctx := context.Background()
cliCtx, verr := cmd.NewArgFreeCliContext(ctx, dEnv)
cliCtx, verr := cmd.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, verr)

for _, c := range test.setup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestJsonValues(t *testing.T) {
func testJsonValue(t *testing.T, test jsonValueTest, setupCommon []testCommand) {
ctx := context.Background()
dEnv := dtestutils.CreateTestEnv()
cliCtx, verr := cmd.NewArgFreeCliContext(ctx, dEnv)
cliCtx, verr := cmd.NewArgFreeCliContext(ctx, dEnv, dEnv.FS)
require.NoError(t, verr)

setup := append(setupCommon, test.setup...)
Expand Down

0 comments on commit e1d400f

Please sign in to comment.