func initConfig() {
	if cfgFile != "" {
		viper.SetConfigFile(cfgFile)
	} else {
		home, err := homedir.Dir()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		viper.AddConfigPath(home)
		viper.SetConfigName(".disk_usage_exporter")
	}
	viper.AutomaticEnv()  
	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	}
}
initConfig reads in config file and ENV variables if set.
package cmd
import (
	"fmt"
	"os"
	"runtime"
	"github.com/dundee/disk_usage_exporter/build"
	"github.com/dundee/disk_usage_exporter/exporter"
	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	homedir "github.com/mitchellh/go-homedir"
	"github.com/spf13/viper"
)
var (
	cfgFile string
)
var rootCmd = &cobra.Command{
	Use:   "disk_usage_exporter",
	Short: "Prometheus exporter for detailed disk usage info",
	Long: `Prometheus exporter analysing disk usage of the filesystem
and reporting which directories consume what space.`,
	Run: func(cmd *cobra.Command, args []string) {
		printHeader()
		e := exporter.NewExporter(
			viper.GetInt("dir-level"),
			viper.GetString("analyzed-path"),
		)
		e.SetIgnoreDirPaths(viper.GetStringSlice("ignore-dirs"))
		e.RunServer(viper.GetString("bind-address"))
	},
}
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
func init() {
	cobra.OnInitialize(initConfig)
	flags := rootCmd.PersistentFlags()
	flags.StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.disk_usage_exporter.yaml)")
	flags.StringP("bind-address", "b", "0.0.0.0:9995", "Address to bind to")
	flags.StringP("analyzed-path", "p", "/", "Path where to analyze disk usage")
	flags.IntP("dir-level", "l", 2, "Directory nesting level to show (0 = only selected dir)")
	flags.StringSliceP("ignore-dirs", "i", []string{"/proc", "/dev", "/sys", "/run", "/var/cache/rsnapshot"}, "Absolute paths to ignore (separated by comma)")
	viper.BindPFlags(flags)
}