func createUnitFile(exeFile string) { data := struct { RexrayBin string EnvFile string }{ exeFile, util.EtcFilePath(util.EnvFileName), } tmpl, err := template.New("UnitFile").Parse(unitFileTemplate) if err != nil { panic(err) } var buf bytes.Buffer err = tmpl.Execute(&buf, data) if err != nil { panic(err) } text := buf.String() f, err := os.OpenFile(util.UnitFilePath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { panic(err) } defer f.Close() f.WriteString(text) }
func TestCopy(t *testing.T) { newPrefixDir("TestCopy", t) etcRexRayCfg := util.EtcFilePath("config.yml") t.Logf("etcRexRayCfg=%s", etcRexRayCfg) util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg) c := New() assertLogLevel(t, c, "error") assertStorageDrivers(t, c) assertOsDrivers1(t, c) cc, _ := c.Copy() assertLogLevel(t, cc, "error") assertStorageDrivers(t, cc) assertOsDrivers1(t, cc) cJSON, _ := c.ToJSON() ccJSON, _ := cc.ToJSON() cMap := map[string]interface{}{} ccMap := map[string]interface{}{} json.Unmarshal([]byte(cJSON), cMap) json.Unmarshal([]byte(ccJSON), ccJSON) if !reflect.DeepEqual(cMap, ccMap) { t.Fail() } }
func TestNewWithGlobalConfigFileWithErrors(t *testing.T) { newPrefixDir("TestNewWithGlobalConfigFileWithErrors", t) etcRexRayCfg := util.EtcFilePath("config.yml") t.Logf("etcRexRayCfg=%s", etcRexRayCfg) util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg) os.Chmod(etcRexRayCfg, 0000) New() }
func createEnvFile() { f, err := os.OpenFile( util.EtcFilePath(util.EnvFileName), os.O_CREATE|os.O_WRONLY, 0644) if err != nil { panic(err) } defer f.Close() if util.IsPrefixed() { f.WriteString("REXRAY_HOME=") f.WriteString(util.GetPrefix()) } }
// NewConfig initialies a new instance of a Config object with the specified // options. func NewConfig( loadGlobalConfig, loadUserConfig bool, configName, configType string) *Config { log.Debug("initializing configuration") c := &Config{ secureConfig: secureConfig{}, plainTextConfig: plainTextConfig{}, Viper: viper.New(), GlobalFlags: &flag.FlagSet{}, AdditionalFlags: &flag.FlagSet{}, jsonMarshalStrategy: JSONMarshalSecure, } c.Viper.SetTypeByDefaultValue(true) c.Viper.SetConfigName(configName) c.Viper.SetConfigType(configType) cfgFile := fmt.Sprintf("%s.%s", configName, configType) etcRexRayFile := util.EtcFilePath(cfgFile) usrRexRayFile := fmt.Sprintf("%s/.rexray/%s", util.HomeDir(), cfgFile) if loadGlobalConfig && util.FileExists(etcRexRayFile) { log.WithField("path", etcRexRayFile).Debug("loading global config file") if err := c.ReadConfigFile(etcRexRayFile); err != nil { log.WithFields(log.Fields{ "path": etcRexRayFile, "error": err}).Error( "error reading global config file") } } if loadUserConfig && util.FileExists(usrRexRayFile) { log.WithField("path", usrRexRayFile).Debug("loading user config file") if err := c.ReadConfigFile(usrRexRayFile); err != nil { log.WithFields(log.Fields{ "path": usrRexRayFile, "error": err}).Error( "error reading user config file") } } c.initConfigKeys() return c }
// NewConfig initialies a new instance of a Config object with the specified // options. func NewConfig( loadGlobalConfig, loadUserConfig bool, configName, configType string) *Config { log.Debug("initializing configuration") c := &Config{ v: viper.New(), FlagSets: map[string]*flag.FlagSet{}, } c.v.SetTypeByDefaultValue(false) c.v.SetConfigName(configName) c.v.SetConfigType(configType) c.processRegistrations() cfgFile := fmt.Sprintf("%s.%s", configName, configType) etcRexRayFile := util.EtcFilePath(cfgFile) usrRexRayFile := fmt.Sprintf("%s/.rexray/%s", util.HomeDir(), cfgFile) if loadGlobalConfig && util.FileExists(etcRexRayFile) { log.WithField("path", etcRexRayFile).Debug("loading global config file") if err := c.ReadConfigFile(etcRexRayFile); err != nil { log.WithFields(log.Fields{ "path": etcRexRayFile, "error": err}).Error( "error reading global config file") } } if loadUserConfig && util.FileExists(usrRexRayFile) { log.WithField("path", usrRexRayFile).Debug("loading user config file") if err := c.ReadConfigFile(usrRexRayFile); err != nil { log.WithFields(log.Fields{ "path": usrRexRayFile, "error": err}).Error( "error reading user config file") } } return c }
func TestNewWithGlobalConfigFile(t *testing.T) { newPrefixDir("TestNewWithGlobalConfigFile", t) etcRexRayCfg := util.EtcFilePath("config.yml") t.Logf("etcRexRayCfg=%s", etcRexRayCfg) util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg) c := New() assertLogLevel(t, c, "error") assertStorageDrivers(t, c) assertOsDrivers1(t, c) if err := c.ReadConfig(bytes.NewReader(yamlConfig2)); err != nil { t.Fatal(err) } assertLogLevel(t, c, "debug") assertStorageDrivers(t, c) assertOsDrivers2(t, c) }
func TestCopy(t *testing.T) { newPrefixDir("TestCopy", t) wipeEnv() Register(testRegistration()) etcRexRayCfg := util.EtcFilePath("config.yml") t.Logf("etcRexRayCfg=%s", etcRexRayCfg) util.WriteStringToFile(string(yamlConfig1), etcRexRayCfg) c := New() assertString(t, c, "logLevel", "error") assertStorageDrivers(t, c) assertOsDrivers1(t, c) cc, _ := c.Copy() assertString(t, cc, "logLevel", "error") assertStorageDrivers(t, cc) assertOsDrivers1(t, cc) cJSON, _ := c.ToJSON() ccJSON, _ := cc.ToJSON() cMap := map[string]interface{}{} ccMap := map[string]interface{}{} if err := json.Unmarshal([]byte(cJSON), &cMap); err != nil { t.Fatal(err) } if err := json.Unmarshal([]byte(ccJSON), &ccMap); err != nil { t.Fatal(err) } if !reflect.DeepEqual(cMap, ccMap) { t.Fail() } }