diff --git a/main.go b/main.go index f7d58652..c9254702 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/rancher/charts-build-scripts/pkg/util" "io/ioutil" "os" "strings" @@ -54,6 +55,8 @@ const ( defaultPRNumberEnvironmentVariable = "PR_NUMBER" // defaultSkipEnvironmentVariable is the default environment variable that indicates whether to skip execution defaultSkipEnvironmentVariable = "SKIP" + // softErrorsEnvironmentVariable is the default environment variable that indicates if soft error mode is enabled + softErrorsEnvironmentVariable = "SOFT_ERRORS" ) var ( @@ -89,12 +92,15 @@ var ( GithubToken string // Skip indicates whether to skip execution Skip bool + // SoftErrorMode indicates if certain non-fatal errors will be turned into warnings + SoftErrorMode = false ) func main() { if len(os.Getenv("DEBUG")) > 0 { logrus.SetLevel(logrus.DebugLevel) } + util.InitSoftErrorMode() app := cli.NewApp() app.Name = "charts-build-scripts" @@ -239,6 +245,12 @@ func main() { EnvVar: defaultSkipEnvironmentVariable, Destination: &Skip, } + softErrorsFlag := cli.BoolFlag{ + Name: "soft-errors", + Usage: "Enables soft error mode - some non-fatal errors will become warnings", + EnvVar: softErrorsEnvironmentVariable, + Destination: &SoftErrorMode, + } // Commands app.Commands = []cli.Command{ @@ -253,7 +265,7 @@ func main() { Usage: "Pull in the chart specified from upstream to the charts directory and apply any patch files", Action: prepareCharts, Before: setupCache, - Flags: []cli.Flag{packageFlag, cacheFlag}, + Flags: []cli.Flag{packageFlag, cacheFlag, softErrorsFlag}, }, { Name: "patch", @@ -422,6 +434,7 @@ func listPackages(c *cli.Context) { } func prepareCharts(c *cli.Context) { + util.SetSoftErrorMode(SoftErrorMode) packages := getPackages() if len(packages) == 0 { logrus.Fatal("Could not find any packages in packages/") diff --git a/pkg/filesystem/filesystem.go b/pkg/filesystem/filesystem.go index 758ab68c..3bb379ed 100644 --- a/pkg/filesystem/filesystem.go +++ b/pkg/filesystem/filesystem.go @@ -6,6 +6,7 @@ import ( "compress/gzip" "crypto/sha1" "fmt" + "github.com/rancher/charts-build-scripts/pkg/util" "io" "io/ioutil" "net/http" @@ -561,13 +562,22 @@ func WalkDir(fs billy.Filesystem, dirPath string, doFunc RelativePathFunc) error // Path does not exist anymore, so do not walk it return nil } - return err + if !util.IsSoftErrorOn() { + return err + } + logrus.Error(err) } path, err := GetRelativePath(fs, abspath) if err != nil { return err } - return doFunc(fs, path, info.IsDir()) + walkFuncRes := doFunc(fs, path, info.IsDir()) + if !util.IsSoftErrorOn() { + return walkFuncRes + } else if walkFuncRes != nil { + logrus.Error(walkFuncRes) + } + return nil }) } diff --git a/pkg/util/errors.go b/pkg/util/errors.go new file mode 100644 index 00000000..b31227a3 --- /dev/null +++ b/pkg/util/errors.go @@ -0,0 +1,20 @@ +package util + +// SoftErrorMode allows for skipping certain non-fatal errors from breaking execution +type SoftErrorMode struct { + Enabled bool +} + +var _softErrorMode *SoftErrorMode + +func InitSoftErrorMode() { + _softErrorMode = &SoftErrorMode{false} +} + +func SetSoftErrorMode(newValue bool) { + _softErrorMode.Enabled = newValue +} + +func IsSoftErrorOn() bool { + return _softErrorMode.Enabled +}