add option to ignore image parsing errors; closes #258

This commit is contained in:
Alex Goodman 2020-02-09 22:30:21 -05:00
parent 4026276660
commit a9c2e48d10
No known key found for this signature in database
GPG Key ID: 150587AB82D3C4E6
5 changed files with 31 additions and 14 deletions

View File

@ -207,7 +207,8 @@ No configuration is necessary, however, you can create a config file and overrid
```yaml ```yaml
# supported options are "docker" and "podman" # supported options are "docker" and "podman"
container-engine: docker container-engine: docker
# continue with analysis even if there are errors parsing the image archive
ignore-errors: false
log: log:
enabled: true enabled: true
path: ./dive.log path: ./dive.log

View File

@ -2,6 +2,8 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/wagoodman/dive/dive" "github.com/wagoodman/dive/dive"
"os" "os"
@ -55,11 +57,17 @@ func doAnalyzeCmd(cmd *cobra.Command, args []string) {
imageStr = userImage imageStr = userImage
} }
ignoreErrors, err := cmd.PersistentFlags().GetBool("ignore-errors")
if err != nil {
logrus.Error("unable to get 'ignore-errors' option:", err)
}
runtime.Run(runtime.Options{ runtime.Run(runtime.Options{
Ci: isCi, Ci: isCi,
Source: sourceType, Source: sourceType,
Image: imageStr, Image: imageStr,
ExportFile: exportFile, ExportFile: exportFile,
CiConfig: ciConfig, CiConfig: ciConfig,
IgnoreErrors: viper.GetBool("ignore-errors") || ignoreErrors,
}) })
} }

View File

@ -48,6 +48,7 @@ func initCli() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml, ~/.config/dive/*.yaml, or $XDG_CONFIG_HOME/dive.yaml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml, ~/.config/dive/*.yaml, or $XDG_CONFIG_HOME/dive.yaml)")
rootCmd.PersistentFlags().String("source", "docker", "The container engine to fetch the image from. Allowed values: "+strings.Join(dive.ImageSources, ", ")) rootCmd.PersistentFlags().String("source", "docker", "The container engine to fetch the image from. Allowed values: "+strings.Join(dive.ImageSources, ", "))
rootCmd.PersistentFlags().BoolP("version", "v", false, "display version number") rootCmd.PersistentFlags().BoolP("version", "v", false, "display version number")
rootCmd.PersistentFlags().BoolP("ignore-errors", "i", false, "ignore image parsing errors and run the analysis anyway")
rootCmd.Flags().BoolVar(&isCi, "ci", false, "Skip the interactive TUI and validate against CI rules (same as env var CI=true)") rootCmd.Flags().BoolVar(&isCi, "ci", false, "Skip the interactive TUI and validate against CI rules (same as env var CI=true)")
rootCmd.Flags().StringVarP(&exportFile, "json", "j", "", "Skip the interactive TUI and write the layer analysis statistics to a given file.") rootCmd.Flags().StringVarP(&exportFile, "json", "j", "", "Skip the interactive TUI and write the layer analysis statistics to a given file.")
rootCmd.Flags().StringVar(&ciConfigFile, "ci-config", ".dive-ci", "If CI=true in the environment, use the given yaml to drive validation rules.") rootCmd.Flags().StringVar(&ciConfigFile, "ci-config", ".dive-ci", "If CI=true in the environment, use the given yaml to drive validation rules.")
@ -62,6 +63,9 @@ func initCli() {
} }
} }
if err := ciConfig.BindPFlag("ignore-errors", rootCmd.PersistentFlags().Lookup("ignore-errors")); err != nil {
log.Fatalf("Unable to bind 'ignore-errors' flag: %v", err)
}
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.
@ -96,6 +100,7 @@ func initConfig() {
viper.SetDefault("filetree.show-attributes", true) viper.SetDefault("filetree.show-attributes", true)
viper.SetDefault("container-engine", "docker") viper.SetDefault("container-engine", "docker")
viper.SetDefault("ignore-errors", false)
viper.SetEnvPrefix("DIVE") viper.SetEnvPrefix("DIVE")
// replace all - with _ when looking for matching environment variables // replace all - with _ when looking for matching environment variables

View File

@ -9,6 +9,7 @@ type Options struct {
Ci bool Ci bool
Image string Image string
Source dive.ImageSource Source dive.ImageSource
IgnoreErrors bool
ExportFile string ExportFile string
CiConfig *viper.Viper CiConfig *viper.Viper
BuildArgs []string BuildArgs []string

View File

@ -93,9 +93,11 @@ func run(enableUi bool, options Options, imageResolver image.Resolver, events ev
for _, err := range errors { for _, err := range errors {
events.message(" " + err.Error()) events.message(" " + err.Error())
} }
events.exitWithError(fmt.Errorf("file tree has path errors")) if !options.IgnoreErrors {
events.exitWithError(fmt.Errorf("file tree has path errors (use '--ignore-errors' to attempt to continue)"))
return return
} }
}
if enableUi { if enableUi {
// it appears there is a race condition where termbox.Init() will // it appears there is a race condition where termbox.Init() will