Added failsafe values to dolt CLI config, only in sql context for now

This commit is contained in:
Zach Musgrave
2021-11-08 12:26:52 -08:00
parent c76f4bde26
commit 576753d38c
11 changed files with 41 additions and 29 deletions

View File

@@ -245,7 +245,7 @@ func CheckEnvIsValid(dEnv *env.DoltEnv) bool {
return true
}
// CheckUserNameAndEmail returns true if the user name and email are set for this environment, or prints and error and
// CheckUserNameAndEmail returns true if the user name and email are set for this environment, or prints an error and
// returns false if not.
func CheckUserNameAndEmail(dEnv *env.DoltEnv) bool {
ok := true

View File

@@ -212,9 +212,8 @@ func (cmd SqlCmd) Exec(ctx context.Context, commandStr string, args []string, dE
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}
if !cli.CheckUserNameAndEmail(dEnv) {
return 1
}
// We need a username and password for many SQL commands, so set defaults if they don't exist
dEnv.Config.SetFailsafes(env.DefaultFailsafeConfig)
mrEnv, verr := getMultiRepoEnv(ctx, apr, dEnv, cmd)
if verr != nil {

View File

@@ -49,7 +49,7 @@ func Serve(ctx context.Context, version string, serverConfig ServerConfig, serve
// Code is easier to work through if we assume that serverController is never nil
if serverController == nil {
serverController = CreateServerController()
serverController = NewServerController()
}
var mySQLServer *server.Server

View File

@@ -52,7 +52,7 @@ var (
)
func TestServerArgs(t *testing.T) {
serverController := CreateServerController()
serverController := NewServerController()
go func() {
startServer(context.Background(), "test", "dolt sql-server", []string{
"-H", "localhost",
@@ -92,7 +92,7 @@ listener:
read_timeout_millis: 5000
write_timeout_millis: 5000
`
serverController := CreateServerController()
serverController := NewServerController()
go func() {
dEnv := dtestutils.CreateEnvWithSeedData(t)
dEnv.FS.WriteFile("config.yaml", []byte(yamlConfig))
@@ -125,7 +125,7 @@ func TestServerBadArgs(t *testing.T) {
for _, test := range tests {
t.Run(strings.Join(test, " "), func(t *testing.T) {
serverController := CreateServerController()
serverController := NewServerController()
go func(serverController *ServerController) {
startServer(context.Background(), "test", "dolt sql-server", test, env, serverController)
}(serverController)
@@ -160,7 +160,7 @@ func TestServerGoodParams(t *testing.T) {
for _, test := range tests {
t.Run(ConfigInfo(test), func(t *testing.T) {
sc := CreateServerController()
sc := NewServerController()
go func(config ServerConfig, sc *ServerController) {
_, _ = Serve(context.Background(), "", config, sc, env)
}(test, sc)
@@ -181,7 +181,7 @@ func TestServerSelect(t *testing.T) {
env := dtestutils.CreateEnvWithSeedData(t)
serverConfig := DefaultServerConfig().withLogLevel(LogLevel_Fatal).withPort(15300)
sc := CreateServerController()
sc := NewServerController()
defer sc.StopServer()
go func() {
_, _ = Serve(context.Background(), "", serverConfig, sc, env)
@@ -230,7 +230,7 @@ func TestServerSelect(t *testing.T) {
// If a port is already in use, throw error "Port XXXX already in use."
func TestServerFailsIfPortInUse(t *testing.T) {
serverController := CreateServerController()
serverController := NewServerController()
server := &http.Server{
Addr: ":15200",
Handler: http.DefaultServeMux,
@@ -256,7 +256,7 @@ func TestServerSetDefaultBranch(t *testing.T) {
dEnv := dtestutils.CreateEnvWithSeedData(t)
serverConfig := DefaultServerConfig().withLogLevel(LogLevel_Fatal).withPort(15302)
sc := CreateServerController()
sc := NewServerController()
defer sc.StopServer()
go func() {
_, _ = Serve(context.Background(), "", serverConfig, sc, dEnv)
@@ -403,7 +403,7 @@ func TestReadReplica(t *testing.T) {
dsess.InitPersistedSystemVars(multiSetup.MrEnv.GetEnv(readReplicaDbName))
// start server as read replica
sc := CreateServerController()
sc := NewServerController()
serverConfig := DefaultServerConfig().withLogLevel(LogLevel_Fatal).withPort(15303)
func() {

View File

@@ -19,8 +19,6 @@ import (
)
type ServerController struct {
//serverClosed *sync.WaitGroup
//serverStarted *sync.WaitGroup
startCh chan struct{}
closeCh chan struct{}
closeCalled *sync.Once
@@ -31,16 +29,15 @@ type ServerController struct {
closeError error
}
// CreateServerController creates a `ServerController` for use with synchronizing on `Serve`.
func CreateServerController() *ServerController {
sc := &ServerController{
// NewServerController creates a `ServerController` for use with synchronizing on `Serve`.
func NewServerController() *ServerController {
return &ServerController{
startCh: make(chan struct{}),
closeCh: make(chan struct{}),
closeCalled: &sync.Once{},
closeRegistered: &sync.Once{},
stopRegistered: &sync.Once{},
}
return sc
}
// registerCloseFunction is called within `Serve` to associate the close function with a future `StopServer` call.

View File

@@ -113,7 +113,7 @@ func (cmd SqlClientCmd) Exec(ctx context.Context, commandStr string, args []stri
}
cli.PrintErrf("Starting server with Config %v\n", ConfigInfo(serverConfig))
serverController = CreateServerController()
serverController = NewServerController()
go func() {
_, _ = Serve(ctx, SqlServerCmd{}.VersionStr, serverConfig, serverController, dEnv)
}()

View File

@@ -162,7 +162,7 @@ func (cmd SqlServerCmd) RequiresRepo() bool {
// Exec executes the command
func (cmd SqlServerCmd) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv) int {
controller := CreateServerController()
controller := NewServerController()
newCtx, cancelF := context.WithCancel(context.Background())
go func() {
<-ctx.Done()
@@ -176,6 +176,9 @@ func startServer(ctx context.Context, versionStr, commandStr string, args []stri
ap := SqlServerCmd{}.CreateArgParser()
help, _ := cli.HelpAndUsagePrinters(cli.GetCommandDocumentation(commandStr, sqlServerDocs, ap))
// We need a username and password for many SQL commands, so set defaults if they don't exist
dEnv.Config.SetFailsafes(env.DefaultFailsafeConfig)
apr := cli.ParseArgsOrDie(ap, args, help)
serverConfig, err := GetServerConfig(dEnv, apr)

View File

@@ -234,11 +234,24 @@ func (dcc *DoltCliConfig) WriteableConfig() config.ReadWriteConfig {
return writeableLocalDoltCliConfig{dcc}
}
// Returns a copy of this config with the config given as failsafes, i.e. values that will be returned as a last
// resort if they are not found elsewhere in the config hierarchy.
// func (dcc *DoltCliConfig) WithFailsafes(cfg config.ReadableConfig) *DoltCliConfig {
// dcc.ch.
// }
// SetFailsafes sets the config values given as failsafes, i.e. values that will be returned as a last resort if they
// are not found elsewhere in the config hierarchy. The "failsafe" config can be written to in order to conform to the
// interface of ConfigHierarchy, but values will not persist beyond this session.
// Calling SetFailsafes more than once will overwrite any previous values.
// Should only be called after primary configuration of the config hierarchy has been completed.
func (dcc DoltCliConfig) SetFailsafes(cfg map[string]string) {
existing, ok := dcc.ch.GetConfig("failsafe")
if !ok {
dcc.ch.AddConfig("failsafe", config.NewEmptyMapConfig())
}
_ = existing.SetStrings(cfg)
}
var DefaultFailsafeConfig = map[string]string {
UserEmailKey: "doltuser@dolthub.com",
UserNameKey: "Dolt System Account",
}
func (w writeableLocalDoltCliConfig) SetStrings(updates map[string]string) error {
localCfg, ok := w.GetConfig(LocalConfig)

View File

@@ -50,7 +50,7 @@ func (mc *MapConfig) GetStringOrDefault(key, defStr string) string {
return defStr
}
// SetString sets the values for a map of updates.
// SetStrings sets the values for a map of updates.
func (mc *MapConfig) SetStrings(updates map[string]string) error {
for k, v := range updates {
mc.properties[k] = v

View File

@@ -163,7 +163,7 @@ func getProfFile(b *testing.B) *os.File {
}
func executeServerQueries(ctx context.Context, b *testing.B, dEnv *env.DoltEnv, cfg srv.ServerConfig, queries []query) {
serverController := srv.CreateServerController()
serverController := srv.NewServerController()
eg, ctx := errgroup.WithContext(ctx)

View File

@@ -175,7 +175,7 @@ func getProfFile(b *testing.B) *os.File {
}
func executeServerQueries(ctx context.Context, b *testing.B, dEnv *env.DoltEnv, cfg srv.ServerConfig, queries []query) {
serverController := srv.CreateServerController()
serverController := srv.NewServerController()
eg, ctx := errgroup.WithContext(ctx)