Skip to content

Commit

Permalink
Merge pull request #283 from abitrolly/sourceconfig
Browse files Browse the repository at this point in the history
Allow setting `source` in config file (fixes #267)
  • Loading branch information
wagoodman committed Mar 8, 2020
2 parents fa9c486 + e15a2aa commit f9e29dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit f9e29dc

Please sign in to comment.