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 // Check the version number in the upgraded config file
expectedVersion := globalMinioConfigVersion expectedVersion := v14
if serverConfig.Version != expectedVersion { if serverConfig.Version != expectedVersion {
t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version) t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version)
} }

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

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

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

Loading…
Cancel
Save