func initConfig() {
	if cfgFile != "" {
		viper.SetConfigFile(cfgFile)
	} else {
		home, err := homedir.Dir()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		viper.AddConfigPath(home)
		ex, err := os.Executable()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		exPath := filepath.Dir(ex)
		viper.AddConfigPath(exPath)
		viper.SetConfigName(".yummy")
	}
	viper.AutomaticEnv()  
	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	}
	repoPath := viper.GetString("yum.repopath")
	createrepoBinary := viper.GetString("yum.createrepoBinary")
	rpmBinary := viper.GetString("yum.rpmBinary")
	helpFile := viper.GetString("yum.helpFile")
	info, err := os.Stat(repoPath)
	if err != nil {
		fmt.Printf("configured repo path '%s' does not exists\n", repoPath)
		os.Exit(1)
	}
	if !info.IsDir() {
		fmt.Printf("configured repo path '%s' is not a directory\n", repoPath)
		os.Exit(1)
	}
	if unix.Access(repoPath, unix.W_OK) != nil {
		fmt.Printf("configured repo path '%s' is not writeable\n", repoPath)
		os.Exit(1)
	}
	info, err = os.Stat(createrepoBinary)
	if err != nil {
		fmt.Printf("configured createrepo binary '%s' does not exists\n", createrepoBinary)
		os.Exit(1)
	}
	if unix.Access(createrepoBinary, unix.X_OK) != nil {
		fmt.Printf("configured createrepo binary '%s' is not executable\n", createrepoBinary)
		os.Exit(1)
	}
	info, err = os.Stat(rpmBinary)
	if err != nil {
		fmt.Printf("configured rpm binary '%s' does not exists\n", rpmBinary)
		os.Exit(1)
	}
	if unix.Access(rpmBinary, unix.X_OK) != nil {
		fmt.Printf("configured rpm binary '%s' is not executable\n", rpmBinary)
		os.Exit(1)
	}
	info, err = os.Stat(helpFile)
	if err != nil {
		fmt.Printf("configured help file '%s' does not exists\n", helpFile)
		os.Exit(1)
	}
	_, err = os.Stat(repoPath + "/repodata")
	if err != nil {
		fmt.Printf("initialise empty repository in %s\n", repoPath)
		var cmdOut []byte
		cmdOut, err = exec.Command(createrepoBinary, repoPath).CombinedOutput()
		if err != nil {
			log.Println(err, string(cmdOut))
			os.Exit(1)
		}
	} else {
		fmt.Printf("Using existing repository: %s\n", repoPath)
	}
}
initConfig reads in config file and ENV variables if set.
package cmd
import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	homedir "github.com/mitchellh/go-homedir"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"golang.org/x/sys/unix"
)
var cfgFile string
var RootCmd = &cobra.Command{
	Use:   "yummy",
	Short: "a simple yum repository server",
	Long: `yummy provides a simple yum repository webserver where you can
upload your rpm packages and download from.`,
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Usage()
	},
}
func Execute() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
func init() {
	cobra.OnInitialize(initConfig)
	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.yummy.yaml)")
}