config: Fix creating new config with wrong version (#3821)

Simplify a little config code to avoid making mistake
next time.
master
Anis Elleuch 8 years ago committed by Harshavardhana
parent 91cf54f895
commit 77c1998a38
  1. 2
      cmd/config-migrate_test.go
  2. 54
      cmd/config-v14.go
  3. 4
      cmd/config-v14_test.go
  4. 1
      cmd/globals.go

@ -151,7 +151,7 @@ func TestServerConfigMigrateV2toV14(t *testing.T) {
}
// Check the version number in the upgraded config file
expectedVersion := globalMinioConfigVersion
expectedVersion := v14
if serverConfig.Version != expectedVersion {
t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version)
}

@ -17,6 +17,7 @@
package cmd
import (
"errors"
"os"
"strings"
"sync"
@ -27,6 +28,9 @@ import (
// Read Write mutex for safe access to ServerConfig.
var serverConfigMu sync.RWMutex
// Config version
var v14 = "14"
// serverConfigV14 server configuration version '14' which is like
// version '13' except it adds support of browser param.
type serverConfigV14 struct {
@ -44,30 +48,15 @@ type serverConfigV14 struct {
Notify *notifier `json:"notify"`
}
// newConfig - initialize a new server config, saves env parameters if
// found, otherwise use default parameters
func newConfig(envParams envParams) error {
// Initialize server config.
func newServerConfigV14() *serverConfigV14 {
srvCfg := &serverConfigV14{
Version: v14,
Region: globalMinioDefaultRegion,
Logger: &logger{},
Notify: &notifier{},
}
srvCfg.Version = globalMinioConfigVersion
srvCfg.Region = globalMinioDefaultRegion
// If env is set for a fresh start, save them to config file.
if globalIsEnvCreds {
srvCfg.SetCredential(envParams.creds)
} else {
srvCfg.SetCredential(newCredential())
}
if globalIsEnvBrowser {
srvCfg.SetBrowser(envParams.browser)
} else {
srvCfg.SetBrowser("on")
}
// Enable console logger by default on a fresh run.
srvCfg.Logger.Console = consoleLogger{
Enable: true,
@ -90,6 +79,24 @@ func newConfig(envParams envParams) error {
srvCfg.Notify.Webhook = make(map[string]webhookNotify)
srvCfg.Notify.Webhook["1"] = webhookNotify{}
return srvCfg
}
// newConfig - initialize a new server config, saves env parameters if
// found, otherwise use default parameters
func newConfig(envParams envParams) error {
// Initialize server config.
srvCfg := newServerConfigV14()
// If env is set for a fresh start, save them to config file.
if globalIsEnvCreds {
srvCfg.SetCredential(envParams.creds)
}
if globalIsEnvBrowser {
srvCfg.SetBrowser(envParams.browser)
}
// Create config path.
if err := createConfigPath(); err != nil {
return err
@ -117,8 +124,9 @@ func loadConfig(envParams envParams) error {
if _, err = os.Stat(configFile); err != nil {
return err
}
srvCfg := &serverConfigV14{}
srvCfg.Version = globalMinioConfigVersion
qc, err := quick.New(srvCfg)
if err != nil {
return err
@ -131,8 +139,6 @@ func loadConfig(envParams envParams) error {
// If env is set override the credentials from config file.
if globalIsEnvCreds {
srvCfg.SetCredential(envParams.creds)
} else {
srvCfg.SetCredential(srvCfg.Credential)
}
if globalIsEnvBrowser {
@ -149,8 +155,10 @@ func loadConfig(envParams envParams) error {
serverConfig = srvCfg
serverConfigMu.Unlock()
// Set the version properly after the unmarshalled json is loaded.
serverConfig.Version = globalMinioConfigVersion
if serverConfig.Version != v14 {
return errors.New("Unsupported config version `" + serverConfig.Version + "`.")
}
return nil
}

@ -102,8 +102,8 @@ func TestServerConfig(t *testing.T) {
})
// Match version.
if serverConfig.GetVersion() != globalMinioConfigVersion {
t.Errorf("Expecting version %s found %s", serverConfig.GetVersion(), globalMinioConfigVersion)
if serverConfig.GetVersion() != v14 {
t.Errorf("Expecting version %s found %s", serverConfig.GetVersion(), v14)
}
// Attempt to save.

@ -31,7 +31,6 @@ import (
// minio configuration related constants.
const (
globalMinioConfigVersion = "13"
globalMinioConfigDir = ".minio"
globalMinioCertsDir = "certs"
globalMinioCertsCADir = "CAs"

Loading…
Cancel
Save