Allow setting source in config file (fixes #267)

This commit is contained in:
Anatoli Babenia 2020-02-22 06:50:13 +03:00
parent 4026276660
commit aea85a1f40
2 changed files with 7 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/wagoodman/dive/runtime" "github.com/wagoodman/dive/runtime"
) )
@ -45,13 +46,13 @@ func doAnalyzeCmd(cmd *cobra.Command, args []string) {
sourceType, imageStr = dive.DeriveImageSource(userImage) sourceType, imageStr = dive.DeriveImageSource(userImage)
if sourceType == dive.SourceUnknown { if sourceType == dive.SourceUnknown {
sourceStr, err := cmd.PersistentFlags().GetString("source") sourceStr := viper.GetString("source")
if err != nil { sourceType = dive.ParseImageSource(sourceStr)
fmt.Printf("unable to determine image source: %v\n", err) if sourceType == dive.SourceUnknown {
fmt.Printf("unable to determine image source: %v\n", sourceStr)
os.Exit(1) os.Exit(1)
} }
sourceType = dive.ParseImageSource(sourceStr)
imageStr = userImage imageStr = userImage
} }

View File

@ -97,6 +97,8 @@ func initConfig() {
viper.SetDefault("container-engine", "docker") viper.SetDefault("container-engine", "docker")
viper.BindPFlag("source", rootCmd.PersistentFlags().Lookup("source"))
viper.SetEnvPrefix("DIVE") viper.SetEnvPrefix("DIVE")
// replace all - with _ when looking for matching environment variables // replace all - with _ when looking for matching environment variables
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))