From d79484ef1d57d98b2de8a840d02de7db9c578d8a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 22 Jun 2022 21:56:53 -0300 Subject: [PATCH] feat: added --skip-before flag (#3182) * feat: added --skip-before flag this would allow to skip global before hooks Signed-off-by: Carlos A Becker * fix: skip docker test Signed-off-by: Carlos A Becker --- cmd/build.go | 3 +++ cmd/release.go | 3 +++ internal/pipe/before/before.go | 6 ++++-- internal/pipe/before/before_test.go | 10 ++++++++++ internal/pipe/docker/docker_test.go | 8 ++++++-- pkg/context/context.go | 1 + 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 27f1ca1bc..83d14d0d9 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -30,6 +30,7 @@ type buildOpts struct { id string snapshot bool skipValidate bool + skipBefore bool skipPostHooks bool rmDist bool deprecated bool @@ -70,6 +71,7 @@ When using ` + "`--single-target`" + `, the ` + "`GOOS`" + ` and ` + "`GOARCH`" cmd.Flags().StringVarP(&root.opts.config, "config", "f", "", "Load configuration from file") cmd.Flags().BoolVar(&root.opts.snapshot, "snapshot", false, "Generate an unversioned snapshot build, skipping all validations") cmd.Flags().BoolVar(&root.opts.skipValidate, "skip-validate", false, "Skips several sanity checks") + cmd.Flags().BoolVar(&root.opts.skipBefore, "skip-before", false, "Skips global before hooks") cmd.Flags().BoolVar(&root.opts.skipPostHooks, "skip-post-hooks", false, "Skips all post-build hooks") cmd.Flags().BoolVar(&root.opts.rmDist, "rm-dist", false, "Remove the dist folder before building") cmd.Flags().IntVarP(&root.opts.parallelism, "parallelism", "p", 0, "Amount tasks to run concurrently (default: number of CPUs)") @@ -125,6 +127,7 @@ func setupBuildContext(ctx *context.Context, options buildOpts) error { log.Debugf("parallelism: %v", ctx.Parallelism) ctx.Snapshot = options.snapshot ctx.SkipValidate = ctx.Snapshot || options.skipValidate + ctx.SkipBefore = options.skipBefore ctx.SkipPostBuildHooks = options.skipPostHooks ctx.RmDist = options.rmDist ctx.SkipTokenCheck = true diff --git a/cmd/release.go b/cmd/release.go index add1ae1c8..878bd10dd 100644 --- a/cmd/release.go +++ b/cmd/release.go @@ -36,6 +36,7 @@ type releaseOpts struct { skipAnnounce bool skipSBOMCataloging bool skipDocker bool + skipBefore bool rmDist bool deprecated bool parallelism int @@ -76,6 +77,7 @@ func newReleaseCmd() *releaseCmd { cmd.Flags().BoolVar(&root.opts.skipSign, "skip-sign", false, "Skips signing artifacts") cmd.Flags().BoolVar(&root.opts.skipSBOMCataloging, "skip-sbom", false, "Skips cataloging artifacts") cmd.Flags().BoolVar(&root.opts.skipDocker, "skip-docker", false, "Skips Docker Images/Manifests builds") + cmd.Flags().BoolVar(&root.opts.skipBefore, "skip-before", false, "Skips global before hooks") cmd.Flags().BoolVar(&root.opts.skipValidate, "skip-validate", false, "Skips git checks") cmd.Flags().BoolVar(&root.opts.rmDist, "rm-dist", false, "Removes the dist folder") cmd.Flags().IntVarP(&root.opts.parallelism, "parallelism", "p", 0, "Amount tasks to run concurrently (default: number of CPUs)") @@ -134,6 +136,7 @@ func setupReleaseContext(ctx *context.Context, options releaseOpts) *context.Con ctx.SkipSign = options.skipSign ctx.SkipSBOMCataloging = options.skipSBOMCataloging ctx.SkipDocker = options.skipDocker + ctx.SkipBefore = options.skipBefore ctx.RmDist = options.rmDist // test only diff --git a/internal/pipe/before/before.go b/internal/pipe/before/before.go index f76dc71e7..b6a882d35 100644 --- a/internal/pipe/before/before.go +++ b/internal/pipe/before/before.go @@ -18,8 +18,10 @@ import ( // Pipe is a global hook pipe. type Pipe struct{} -func (Pipe) String() string { return "running before hooks" } -func (Pipe) Skip(ctx *context.Context) bool { return len(ctx.Config.Before.Hooks) == 0 } +func (Pipe) String() string { return "running before hooks" } +func (Pipe) Skip(ctx *context.Context) bool { + return len(ctx.Config.Before.Hooks) == 0 || ctx.SkipBefore +} // Run executes the hooks. func (Pipe) Run(ctx *context.Context) error { diff --git a/internal/pipe/before/before_test.go b/internal/pipe/before/before_test.go index 5205a4996..0deddb5c8 100644 --- a/internal/pipe/before/before_test.go +++ b/internal/pipe/before/before_test.go @@ -97,6 +97,16 @@ func TestSkip(t *testing.T) { require.True(t, Pipe{}.Skip(context.New(config.Project{}))) }) + t.Run("skip before", func(t *testing.T) { + ctx := context.New(config.Project{ + Before: config.Before{ + Hooks: []string{""}, + }, + }) + ctx.SkipBefore = true + require.True(t, Pipe{}.Skip(ctx)) + }) + t.Run("dont skip", func(t *testing.T) { ctx := context.New(config.Project{ Before: config.Before{ diff --git a/internal/pipe/docker/docker_test.go b/internal/pipe/docker/docker_test.go index 670a82285..ab656b9ce 100644 --- a/internal/pipe/docker/docker_test.go +++ b/internal/pipe/docker/docker_test.go @@ -1373,7 +1373,9 @@ func TestSkip(t *testing.T) { }) t.Run("skip docker", func(t *testing.T) { - ctx := context.New(config.Project{}) + ctx := context.New(config.Project{ + Dockers: []config.Docker{{}}, + }) ctx.SkipDocker = true require.True(t, Pipe{}.Skip(ctx)) }) @@ -1392,7 +1394,9 @@ func TestSkip(t *testing.T) { }) t.Run("skip docker", func(t *testing.T) { - ctx := context.New(config.Project{}) + ctx := context.New(config.Project{ + DockerManifests: []config.DockerManifest{{}}, + }) ctx.SkipDocker = true require.True(t, ManifestPipe{}.Skip(ctx)) }) diff --git a/pkg/context/context.go b/pkg/context/context.go index f6ca6dc9b..4b7f47db2 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -96,6 +96,7 @@ type Context struct { SkipValidate bool SkipSBOMCataloging bool SkipDocker bool + SkipBefore bool RmDist bool PreRelease bool Deprecated bool