config-old: Use interface to avoid code repetition. (#3769)

master
Bala FA 8 years ago committed by Harshavardhana
parent 0137ff498a
commit d12f3e06b1
  1. 202
      cmd/config-old.go

@ -1,3 +1,19 @@
/*
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd package cmd
import ( import (
@ -8,6 +24,23 @@ import (
"github.com/minio/minio/pkg/quick" "github.com/minio/minio/pkg/quick"
) )
func loadOldConfig(configFile string, config interface{}) (interface{}, error) {
if _, err := os.Stat(configFile); err != nil {
return nil, err
}
qc, err := quick.New(config)
if err != nil {
return nil, err
}
if err = qc.Load(configFile); err != nil {
return nil, err
}
return config, nil
}
/////////////////// Config V1 /////////////////// /////////////////// Config V1 ///////////////////
type configV1 struct { type configV1 struct {
Version string `json:"version"` Version string `json:"version"`
@ -22,19 +55,11 @@ func loadConfigV1() (*configV1, error) {
return nil, err return nil, err
} }
configFile := filepath.Join(configPath, "fsUsers.json") configFile := filepath.Join(configPath, "fsUsers.json")
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &configV1{Version: "1"})
if config == nil {
return nil, err return nil, err
} }
c := &configV1{} return config.(*configV1), err
c.Version = "1"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err
}
return c, nil
} }
/////////////////// Config V2 /////////////////// /////////////////// Config V2 ///////////////////
@ -65,19 +90,11 @@ func loadConfigV2() (*configV2, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &configV2{Version: "2"})
if config == nil {
return nil, err return nil, err
} }
c := &configV2{} return config.(*configV2), err
c.Version = "2"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err
}
return c, nil
} }
/////////////////// Config V3 /////////////////// /////////////////// Config V3 ///////////////////
@ -139,19 +156,11 @@ func loadConfigV3() (*configV3, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &configV3{Version: "3"})
return nil, err if config == nil {
}
c := &configV3{}
c.Version = "3"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err return nil, err
} }
return c, nil return config.(*configV3), err
} }
// logger type representing version '4' logger config. // logger type representing version '4' logger config.
@ -190,19 +199,11 @@ func loadConfigV4() (*configV4, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &configV4{Version: "4"})
if config == nil {
return nil, err return nil, err
} }
c := &configV4{} return config.(*configV4), err
c.Version = "4"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err
}
return c, nil
} }
// logger type representing version '5' logger config. // logger type representing version '5' logger config.
@ -268,19 +269,11 @@ func loadConfigV5() (*configV5, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &configV5{Version: "5"})
if config == nil {
return nil, err return nil, err
} }
c := &configV5{} return config.(*configV5), err
c.Version = "5"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err
}
return c, nil
} }
type loggerV6 struct { type loggerV6 struct {
@ -310,19 +303,11 @@ func loadConfigV6() (*configV6, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &configV6{Version: "6"})
if config == nil {
return nil, err return nil, err
} }
c := &configV6{} return config.(*configV6), err
c.Version = "6"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err
}
return c, nil
} }
// Notifier represents collection of supported notification queues in version // Notifier represents collection of supported notification queues in version
@ -371,19 +356,11 @@ func loadConfigV7() (*serverConfigV7, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &serverConfigV7{Version: "7"})
return nil, err if config == nil {
}
c := &serverConfigV7{}
c.Version = "7"
qc, err := quick.New(c)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err return nil, err
} }
return c, nil return config.(*serverConfigV7), err
} }
// serverConfigV8 server configuration version '8'. Adds NATS notifier // serverConfigV8 server configuration version '8'. Adds NATS notifier
@ -411,19 +388,11 @@ func loadConfigV8() (*serverConfigV8, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &serverConfigV8{Version: "8"})
return nil, err if config == nil {
}
c := &serverConfigV8{}
c.Version = "8"
qc, err := quick.New(c)
if err != nil {
return nil, err return nil, err
} }
if err := qc.Load(configFile); err != nil { return config.(*serverConfigV8), err
return nil, err
}
return c, nil
} }
// serverConfigV9 server configuration version '9'. Adds PostgreSQL // serverConfigV9 server configuration version '9'. Adds PostgreSQL
@ -450,20 +419,11 @@ func loadConfigV9() (*serverConfigV9, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &serverConfigV9{Version: "9"})
return nil, err if config == nil {
}
srvCfg := &serverConfigV9{}
srvCfg.Version = "9"
srvCfg.rwMutex = &sync.RWMutex{}
qc, err := quick.New(srvCfg)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err return nil, err
} }
return srvCfg, nil return config.(*serverConfigV9), err
} }
// serverConfigV10 server configuration version '10' which is like // serverConfigV10 server configuration version '10' which is like
@ -488,19 +448,11 @@ func loadConfigV10() (*serverConfigV10, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &serverConfigV10{Version: "10"})
if config == nil {
return nil, err return nil, err
} }
srvCfg := &serverConfigV10{} return config.(*serverConfigV10), err
srvCfg.Version = "10"
qc, err := quick.New(srvCfg)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err
}
return srvCfg, nil
} }
// natsNotifyV1 - structure was valid until config V 11 // natsNotifyV1 - structure was valid until config V 11
@ -536,19 +488,11 @@ func loadConfigV11() (*serverConfigV11, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &serverConfigV11{Version: "11"})
return nil, err if config == nil {
}
srvCfg := &serverConfigV11{}
srvCfg.Version = "11"
qc, err := quick.New(srvCfg)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err return nil, err
} }
return srvCfg, nil return config.(*serverConfigV11), err
} }
// serverConfigV12 server configuration version '12' which is like // serverConfigV12 server configuration version '12' which is like
@ -572,17 +516,9 @@ func loadConfigV12() (*serverConfigV12, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err = os.Stat(configFile); err != nil { config, err := loadOldConfig(configFile, &serverConfigV12{Version: "12"})
return nil, err if config == nil {
}
srvCfg := &serverConfigV12{}
srvCfg.Version = "12"
qc, err := quick.New(srvCfg)
if err != nil {
return nil, err
}
if err := qc.Load(configFile); err != nil {
return nil, err return nil, err
} }
return srvCfg, nil return config.(*serverConfigV12), err
} }

Loading…
Cancel
Save