Skip to content

Commit

Permalink
fix: Config file not loaded properly
Browse files Browse the repository at this point in the history
Ref: #21
  • Loading branch information
caffeine-addictt committed Apr 9, 2024
1 parent 5999a14 commit 2fdceb0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
)

Expand All @@ -21,7 +22,6 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions src/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/caffeine-addictt/video-manager/src/utils"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -93,6 +94,18 @@ func initConfig() {
os.Exit(1)
}
}

// Update cobra flags with viper environment
viper.AutomaticEnv()
rootCommand.Flags().VisitAll(func(f *pflag.Flag) {
if viper.IsSet(f.Name) {
if err := rootCommand.Flags().Set(f.Name, viper.GetString(f.Name)); err != nil {
fmt.Printf("Failed to set flag '%s' to value '%s'\n", f.Name, viper.GetString(f.Name))
os.Exit(1)
}
}
})

Info("Loaded configuration from " + viper.ConfigFileUsed())
}

Expand Down

0 comments on commit 2fdceb0

Please sign in to comment.