Skip to content

Commit

Permalink
refactor: Move home dir to use init
Browse files Browse the repository at this point in the history
  • Loading branch information
caffeine-addictt committed Apr 13, 2024
1 parent c8ef0f5 commit 64d3ec0
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import (

// Global variables
var (
cfgFile string
verbose bool
debug bool
// Global
home string

// Flags
cfgFile string
cacheFile string
verbose bool
debug bool

// Working Directory
workingDir string
Expand All @@ -33,11 +37,23 @@ var rootCommand = &cobra.Command{
),
}

func init() {
home_, err := homedir.Dir()
if err != nil {
fmt.Println("Failed to get home directory")
Debug(err.Error())
os.Exit(1)
}

home = home_
}

// Configuration
func init() {
cobra.OnInitialize(initConfig)
rootCommand.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.video-manager)")
rootCommand.PersistentFlags().StringVarP(&cacheFile, "cache", "C", "", "cache file (default is $HOME/.video-manager_history)")
viper.SetDefault("cache", filepath.Clean(filepath.Join(home, ".video-manager_history")))

// Verbosity
rootCommand.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
Expand All @@ -56,13 +72,6 @@ func init() {
func initConfig() {
viper.SetConfigType("yaml")

home, err := homedir.Dir()
if err != nil {
fmt.Println("Failed to get home directory")
Debug(err.Error())
os.Exit(1)
}

if cfgFile != "" {
// Reading provided configuration file
viper.SetConfigFile(cfgFile)
Expand Down

0 comments on commit 64d3ec0

Please sign in to comment.