Exemplo n.º 1
0
// initConfig reads in config file and ENV variables if set.
func initConfig() {
	viper.SetEnvPrefix("hyperclair")
	viper.SetConfigName(".hyperclair") // name of config file (without extension)
	viper.AddConfigPath(".")           // adding home directory as first search path
	viper.AutomaticEnv()               // read in environment variables that match
	if cfgFile == "" {                 // enable ability to specify config file via flag
		cfgFile = viper.GetString("config")
	}
	viper.SetConfigFile(cfgFile)
	err := viper.ReadInConfig()
	if err != nil {
		fmt.Println("hyperclair: config file not found")
		os.Exit(1)
	}
	logrus.Infof("Using config file: %v", viper.ConfigFileUsed())

	if viper.Get("clair.uri") == nil {
		viper.Set("clair.uri", "http://localhost")
	}
	if viper.Get("clair.port") == nil {
		viper.Set("clair.port", "6060")
	}
	if viper.Get("clair.healthPort") == nil {
		viper.Set("clair.healthPort", "6061")
	}
	if viper.Get("clair.priority") == nil {
		viper.Set("clair.priority", "Low")
	}
	if viper.Get("clair.report.path") == nil {
		viper.Set("clair.report.path", "reports")
	}
	if viper.Get("clair.report.format") == nil {
		viper.Set("clair.report.format", "html")
	}
	if viper.Get("auth.insecureSkipVerify") == nil {
		viper.Set("auth.insecureSkipVerify", "false")
	}
	if viper.Get("hyperclair.uri") == nil {
		viper.Set("hyperclair.uri", "http://localhost")
	}
	if viper.Get("hyperclair.port") == nil {
		viper.Set("hyperclair.port", "9999")
	}
	if viper.Get("hyperclair.local.ip") == nil {
		viper.Set("hyperclair.local.ip", "")
	}
	if viper.Get("hyperclair.local.port") == nil {
		viper.Set("hyperclair.local.port", 60000)
	}
	if viper.Get("hyperclair.local.tempFolder") == nil {
		viper.Set("hyperclair.local.tempFolder", "/tmp/hyperclair")
	}

	lvl := logrus.WarnLevel
	if logLevel != "" {
		var err error
		lvl, err = logrus.ParseLevel(logLevel)
		if err != nil {
			logrus.Warningf("Wrong Log level %v, defaults to [Warning]", logLevel)
			lvl = logrus.WarnLevel
		}
	}
	logrus.SetLevel(lvl)
	clair.Config()

	HyperclairURI = viper.GetString("hyperclair.uri") + ":" + strconv.Itoa(viper.GetInt("hyperclair.port")) + "/v1"
}
Exemplo n.º 2
0
// Init reads in config file and ENV variables if set.
func Init(cfgFile string, logLevel string) {
	lvl := logrus.WarnLevel
	if logLevel != "" {
		var err error
		lvl, err = logrus.ParseLevel(logLevel)
		if err != nil {
			logrus.Warningf("Wrong Log level %v, defaults to [Warning]", logLevel)
			lvl = logrus.WarnLevel
		}
	}
	logrus.SetLevel(lvl)

	viper.SetEnvPrefix("hyperclair")
	viper.SetConfigName("hyperclair")        // name of config file (without extension)
	viper.AddConfigPath("$HOME/.hyperclair") // adding home directory as first search path
	viper.AddConfigPath(".")                 // adding home directory as first search path
	viper.AutomaticEnv()                     // read in environment variables that match
	if cfgFile != "" {
		viper.SetConfigFile(cfgFile)
	}
	err := viper.ReadInConfig()
	if err != nil {
		logrus.Debugf("No config file used")
	} else {
		logrus.Debugf("Using config file: %v", viper.ConfigFileUsed())
	}

	if viper.Get("clair.uri") == nil {
		viper.Set("clair.uri", "http://localhost")
	}
	if viper.Get("clair.port") == nil {
		viper.Set("clair.port", "6060")
	}
	if viper.Get("clair.healthPort") == nil {
		viper.Set("clair.healthPort", "6061")
	}
	if viper.Get("clair.priority") == nil {
		viper.Set("clair.priority", "Low")
	}
	if viper.Get("clair.report.path") == nil {
		viper.Set("clair.report.path", "reports")
	}
	if viper.Get("clair.report.format") == nil {
		viper.Set("clair.report.format", "html")
	}
	if viper.Get("auth.insecureSkipVerify") == nil {
		viper.Set("auth.insecureSkipVerify", "true")
	}
	if viper.Get("hyperclair.ip") == nil {
		viper.Set("hyperclair.ip", "")
	}
	if viper.Get("hyperclair.port") == nil {
		viper.Set("hyperclair.port", 0)
	}
	if viper.Get("hyperclair.tempFolder") == nil {
		viper.Set("hyperclair.tempFolder", "/tmp/hyperclair")
	}
	if viper.Get("hyperclair.interface") == nil {
		viper.Set("hyperclair.interface", "native")
	}
	clair.Config()
}