Ejemplo n.º 1
0
func initUsageTemplates() {

	var ut string
	utPath := fmt.Sprintf("%s/.rexray/usage.template", util.HomeDir())
	log.WithField("path", utPath).Debug("usage template path")

	if util.FileExists(utPath) {
		dat, err := ioutil.ReadFile(utPath)
		if err != nil {
			panic(err)
		}
		log.WithField("source", utPath).Debug("loaded usage template")
		ut = string(dat)
	} else {
		log.WithField("source", "UsageTemplate").Debug("loaded usage template")
		ut = UsageTemplate
	}

	RexrayCmd.SetUsageTemplate(ut)
	RexrayCmd.SetHelpTemplate(ut)

	cobra.AddTemplateFuncs(template.FuncMap{
		"af":    additionalFlags,
		"hf":    hasFlags,
		"lf":    localFlags,
		"gf":    globalFlags,
		"ihf":   isHelpFlag,
		"ivf":   isVerboseFlag,
		"saf":   sansAdditionalFlags,
		"cmds":  commands,
		"rtrim": rtrim,
	})
}
Ejemplo n.º 2
0
func TestNew(t *testing.T) {

	usrRexRayDir := fmt.Sprintf("%s/.rexray", util.HomeDir())
	os.MkdirAll(usrRexRayDir, 0755)
	usrRexRayFile := fmt.Sprintf("%s/%s.%s", usrRexRayDir, "config", "yml")
	usrRexRayFileBak := fmt.Sprintf("%s.bak", usrRexRayFile)

	os.Remove(usrRexRayFileBak)
	os.Rename(usrRexRayFile, usrRexRayFileBak)
	defer func() {
		os.Remove(usrRexRayFile)
		os.Rename(usrRexRayFileBak, usrRexRayFile)
	}()

	util.WriteStringToFile(string(yamlConfig1), usrRexRayFile)

	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)
}
Ejemplo n.º 3
0
func initGlobalFlags() {
	RexrayCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c",
		fmt.Sprintf("%s/.rexray/config.yml", util.HomeDir()),
		"The REX-Ray configuration file")
	RexrayCmd.PersistentFlags().BoolP(
		"verbose", "v", false, "Print verbose help information")

	RexrayCmd.PersistentFlags().AddFlagSet(c.GlobalFlags)
	RexrayCmd.PersistentFlags().AddFlagSet(c.AdditionalFlags)
}
Ejemplo n.º 4
0
func New() *Config {
	log.Debug("initializing configuration")

	c := &Config{
		Viper: viper.New(),
	}

	cfgName := "config"
	cfgType := "yaml"
	etcRexRay := "/etc/rexray"
	usrRexRay := fmt.Sprintf("%s/.rexray", util.HomeDir())
	etcRexRayFile := fmt.Sprintf("%s/%s.%s", etcRexRay, cfgName, cfgType)
	usrRexRayFile := fmt.Sprintf("%s/%s.%s", usrRexRay, cfgName, cfgType)

	c.Viper.SetConfigName(cfgName)
	c.Viper.SetConfigType(cfgType)
	c.Viper.SetTypeByDefaultValue(true)

	log.WithFields(log.Fields{
		"name": cfgName,
		"type": cfgType}).Debug("set config name and type")

	c.Viper.AddConfigPath(etcRexRay)
	c.Viper.AddConfigPath(usrRexRay)

	log.WithFields(log.Fields{
		"global": etcRexRay,
		"user":   usrRexRay}).Debug("added config paths")

	if util.FileExists(etcRexRayFile) || util.FileExists(usrRexRayFile) {

		log.WithFields(log.Fields{
			"global": etcRexRayFile,
			"user":   usrRexRayFile}).Debug(
			"reading configuration file(s) from default path(s)")

		if readCfgErr := c.Viper.ReadInConfig(); readCfgErr != nil {
			log.WithFields(log.Fields{
				"global": etcRexRayFile,
				"user":   usrRexRayFile,
				"error":  readCfgErr}).Error(
				"error reading configuration file(s) from default path(s)")
		}
	}

	c.initConfigKeys()

	return c
}
Ejemplo n.º 5
0
func NewConfig(
	loadGlobalConfig, loadUserConfig bool,
	configName, configType string) *Config {

	log.Debug("initializing configuration")

	c := &Config{
		plainTextConfig: plainTextConfig{
			secureConfig: secureConfig{},
		},
		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 := fmt.Sprintf("%s/%s", util.EtcDirPath(), 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

}
Ejemplo n.º 6
0
func (c *CLI) initOtherFlags() {
	cobra.HelpFlagShorthand = "?"
	cobra.HelpFlagUsageFormatString = "Help for %s"

	c.c.PersistentFlags().StringVarP(&c.cfgFile, "config", "c",
		fmt.Sprintf("%s/.rexray/config.yml", util.HomeDir()),
		"The REX-Ray configuration file")
	c.c.PersistentFlags().BoolP(
		"verbose", "v", false, "Print verbose help information")

	c.c.PersistentFlags().AddFlagSet(c.r.Config.GlobalFlags)
	c.c.PersistentFlags().AddFlagSet(c.r.Config.AdditionalFlags)

	c.uninstallCmd.Flags().Bool("package", false,
		"A flag indicating a package manager is performing the uninstallation")
}
Ejemplo n.º 7
0
func TestMain(m *testing.M) {
	usrRexRayDir := fmt.Sprintf("%s/.rexray", util.HomeDir())
	os.MkdirAll(usrRexRayDir, 0755)
	usrRexRayFile = fmt.Sprintf("%s/%s.%s", usrRexRayDir, "config", "yml")
	usrRexRayFileBak := fmt.Sprintf("%s.bak", usrRexRayFile)

	os.Remove(usrRexRayFileBak)
	os.Rename(usrRexRayFile, usrRexRayFileBak)

	exitCode := m.Run()
	for _, d := range tmpPrefixDirs {
		os.RemoveAll(d)
	}

	os.Remove(usrRexRayFile)
	os.Rename(usrRexRayFileBak, usrRexRayFile)
	os.Exit(exitCode)
}
Ejemplo n.º 8
0
// 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
}