Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting source in config file (fixes #267) #283

Merged
merged 2 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wagoodman/dive/runtime"
)

Expand Down Expand Up @@ -45,13 +46,13 @@ func doAnalyzeCmd(cmd *cobra.Command, args []string) {
sourceType, imageStr = dive.DeriveImageSource(userImage)

if sourceType == dive.SourceUnknown {
sourceStr, err := cmd.PersistentFlags().GetString("source")
if err != nil {
fmt.Printf("unable to determine image source: %v\n", err)
sourceStr := viper.GetString("source")
sourceType = dive.ParseImageSource(sourceStr)
if sourceType == dive.SourceUnknown {
fmt.Printf("unable to determine image source: %v\n", sourceStr)
os.Exit(1)
}

sourceType = dive.ParseImageSource(sourceStr)
imageStr = userImage
}

Expand Down
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func initCli() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
var err error

viper.SetDefault("log.level", log.InfoLevel.String())
viper.SetDefault("log.path", "./dive.log")
viper.SetDefault("log.enabled", false)
Expand Down Expand Up @@ -97,6 +99,12 @@ func initConfig() {

viper.SetDefault("container-engine", "docker")

err = viper.BindPFlag("source", rootCmd.PersistentFlags().Lookup("source"))
if err != nil {
fmt.Println(err)
os.Exit(1)
}

viper.SetEnvPrefix("DIVE")
// replace all - with _ when looking for matching environment variables
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand All @@ -110,7 +118,7 @@ func initConfig() {
} else {
viper.SetConfigFile(cfgFile)
}
err := viper.ReadInConfig()
err = viper.ReadInConfig()
if err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else if cfgFile != "" {
Expand Down