Ensure config dir

Closes #5
This commit is contained in:
James Walker 2021-01-05 03:06:05 +00:00
parent 4d6b96b79a
commit d313e46181
Signed by: walkah
GPG Key ID: 3C127179D6086E93

View File

@ -25,6 +25,7 @@ import (
"errors"
"fmt"
"os"
"path"
"github.com/spf13/cobra"
@ -61,10 +62,24 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)
ensureConfigDir()
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.workon.yaml)")
}
// ensureConfigDir ensures ~/.workon/ exists
func ensureConfigDir() {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configDir := path.Join(home, ".workon")
if _, err = os.Stat(configDir); os.IsNotExist(err) {
os.Mkdir(configDir, 0755)
}
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {