From 1ed299a6d7b713473bf3fcf7200852782355d713 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 2 Dec 2017 19:53:19 -0200 Subject: [PATCH] refactor: defaulter interface Right now the code looks weird because the defaults of a pipe are far away of the implementation of the pipe. the intend of this PR is to bring them closer by having a Defaulter interface. I also renamed the Pipe interface to Piper, and removed the Description method in favor for fmt.Stringer. --- goreleaserlib/goreleaser.go | 4 +- pipeline/archive/archive.go | 27 ++- pipeline/archive/archive_test.go | 2 +- pipeline/brew/brew.go | 49 ++++- pipeline/brew/brew_test.go | 2 +- pipeline/build/build.go | 39 +++- pipeline/build/build_test.go | 2 +- pipeline/changelog/changelog.go | 4 +- pipeline/changelog/changelog_test.go | 2 +- pipeline/checksums/checksums.go | 12 +- pipeline/checksums/checksums_test.go | 2 +- pipeline/cleandist/dist.go | 4 +- pipeline/cleandist/dist_test.go | 2 +- pipeline/default.go | 15 ++ pipeline/defaults/defaults.go | 194 +++--------------- pipeline/defaults/defaults_test.go | 2 +- pipeline/docker/docker.go | 25 ++- pipeline/docker/docker_test.go | 2 +- pipeline/env/env.go | 4 +- pipeline/env/env_test.go | 2 +- pipeline/fpm/fpm.go | 12 +- pipeline/fpm/fpm_test.go | 2 +- pipeline/git/git.go | 4 +- pipeline/git/git_test.go | 2 +- pipeline/{pipe.go => piper.go} | 13 +- pipeline/{pipe_test.go => piper_test.go} | 0 pipeline/release/release.go | 20 +- pipeline/release/release_test.go | 2 +- pipeline/{defaults => release}/remote.go | 2 +- pipeline/{defaults => release}/remote_test.go | 2 +- pipeline/snapcraft/snapcraft.go | 4 +- pipeline/snapcraft/snapcraft_test.go | 2 +- pipeline/snapshot/package.go | 2 + pipeline/snapshot/snapshot.go | 20 ++ pipeline/snapshot/snapshot_test.go | 15 ++ 35 files changed, 285 insertions(+), 212 deletions(-) create mode 100644 pipeline/default.go rename pipeline/{pipe.go => piper.go} (81%) rename pipeline/{pipe_test.go => piper_test.go} (100%) rename pipeline/{defaults => release}/remote.go (98%) rename pipeline/{defaults => release}/remote_test.go (97%) create mode 100644 pipeline/snapshot/package.go create mode 100644 pipeline/snapshot/snapshot.go create mode 100644 pipeline/snapshot/snapshot_test.go diff --git a/goreleaserlib/goreleaser.go b/goreleaserlib/goreleaser.go index 15a2e071f..ada9a8e2d 100644 --- a/goreleaserlib/goreleaser.go +++ b/goreleaserlib/goreleaser.go @@ -26,7 +26,7 @@ import ( yaml "gopkg.in/yaml.v2" ) -var pipes = []pipeline.Pipe{ +var pipes = []pipeline.Piper{ defaults.Pipe{}, // load default configs git.Pipe{}, // get and validate git repo state changelog.Pipe{}, // builds the release changelog @@ -89,7 +89,7 @@ func Release(flags Flags) error { } ctx.RmDist = flags.Bool("rm-dist") for _, pipe := range pipes { - log.Infof("\033[1m%s\033[0m", strings.ToUpper(pipe.Description())) + log.Infof("\033[1m%s\033[0m", strings.ToUpper(pipe.String())) if err := handle(pipe.Run(ctx)); err != nil { return err } diff --git a/pipeline/archive/archive.go b/pipeline/archive/archive.go index e6a3b0469..f57ff2d3c 100644 --- a/pipeline/archive/archive.go +++ b/pipeline/archive/archive.go @@ -20,8 +20,8 @@ import ( type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Creating archives" +func (Pipe) String() string { + return "creating archives" } // Run the pipe @@ -40,6 +40,29 @@ func (Pipe) Run(ctx *context.Context) error { return g.Wait() } +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.Archive.NameTemplate == "" { + ctx.Config.Archive.NameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" + } + if ctx.Config.Archive.Format == "" { + ctx.Config.Archive.Format = "tar.gz" + } + if len(ctx.Config.Archive.Files) == 0 { + ctx.Config.Archive.Files = []string{ + "licence*", + "LICENCE*", + "license*", + "LICENSE*", + "readme*", + "README*", + "changelog*", + "CHANGELOG*", + } + } + return nil +} + func create(ctx *context.Context, platform string, groups map[string][]context.Binary) error { for folder, binaries := range groups { var format = archiveformat.For(ctx, platform) diff --git a/pipeline/archive/archive_test.go b/pipeline/archive/archive_test.go index 1a6acad3e..eed4a1bc6 100644 --- a/pipeline/archive/archive_test.go +++ b/pipeline/archive/archive_test.go @@ -15,7 +15,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestRunPipe(t *testing.T) { diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 31602b909..8be664de7 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -5,12 +5,14 @@ package brew import ( "bytes" "errors" + "fmt" "path/filepath" "strings" "text/template" "github.com/apex/log" "github.com/goreleaser/goreleaser/checksum" + "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" "github.com/goreleaser/goreleaser/internal/archiveformat" "github.com/goreleaser/goreleaser/internal/client" @@ -27,8 +29,8 @@ const platform = "darwinamd64" type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Creating homebrew formula" +func (Pipe) String() string { + return "creating homebrew formula" } // Run the pipe @@ -40,6 +42,49 @@ func (Pipe) Run(ctx *context.Context) error { return doRun(ctx, client) } +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.Brew.Install == "" { + var installs []string + for _, build := range ctx.Config.Builds { + if !isBrewBuild(build) { + continue + } + installs = append( + installs, + fmt.Sprintf(`bin.install "%s"`, build.Binary), + ) + } + ctx.Config.Brew.Install = strings.Join(installs, "\n") + } + + if ctx.Config.Brew.CommitAuthor.Name == "" { + ctx.Config.Brew.CommitAuthor.Name = "goreleaserbot" + } + if ctx.Config.Brew.CommitAuthor.Email == "" { + ctx.Config.Brew.CommitAuthor.Email = "goreleaser@carlosbecker.com" + } + return nil +} + +func isBrewBuild(build config.Build) bool { + for _, ignore := range build.Ignore { + if ignore.Goos == "darwin" && ignore.Goarch == "amd64" { + return false + } + } + return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64") +} + +func contains(ss []string, s string) bool { + for _, zs := range ss { + if zs == s { + return true + } + } + return false +} + func doRun(ctx *context.Context, client client.Client) error { if !ctx.Publish { return pipeline.Skip("--skip-publish is set") diff --git a/pipeline/brew/brew_test.go b/pipeline/brew/brew_test.go index ae910e306..111357f1b 100644 --- a/pipeline/brew/brew_test.go +++ b/pipeline/brew/brew_test.go @@ -14,7 +14,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestNameWithDash(t *testing.T) { diff --git a/pipeline/build/build.go b/pipeline/build/build.go index 9f9f2b73b..e28ce7306 100644 --- a/pipeline/build/build.go +++ b/pipeline/build/build.go @@ -26,8 +26,8 @@ import ( type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Building binaries" +func (Pipe) String() string { + return "building binaries" } // Run the pipe @@ -44,6 +44,41 @@ func (Pipe) Run(ctx *context.Context) error { return nil } +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + for i, build := range ctx.Config.Builds { + ctx.Config.Builds[i] = buildWithDefaults(ctx, build) + } + if len(ctx.Config.Builds) == 0 { + ctx.Config.Builds = []config.Build{ + buildWithDefaults(ctx, ctx.Config.SingleBuild), + } + } + return nil +} + +func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { + if build.Binary == "" { + build.Binary = ctx.Config.Release.GitHub.Name + } + if build.Main == "" { + build.Main = "." + } + if len(build.Goos) == 0 { + build.Goos = []string{"linux", "darwin"} + } + if len(build.Goarch) == 0 { + build.Goarch = []string{"amd64", "386"} + } + if len(build.Goarm) == 0 { + build.Goarm = []string{"6"} + } + if build.Ldflags == "" { + build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" + } + return build +} + func checkMain(ctx *context.Context, build config.Build) error { var main = build.Main if main == "" { diff --git a/pipeline/build/build_test.go b/pipeline/build/build_test.go index 765a4d10d..307bf72a0 100644 --- a/pipeline/build/build_test.go +++ b/pipeline/build/build_test.go @@ -17,7 +17,7 @@ import ( var emptyEnv []string func TestPipeDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestRun(t *testing.T) { diff --git a/pipeline/changelog/changelog.go b/pipeline/changelog/changelog.go index d72b974f9..b1507567e 100644 --- a/pipeline/changelog/changelog.go +++ b/pipeline/changelog/changelog.go @@ -20,8 +20,8 @@ var ErrInvalidSortDirection = errors.New("invalid sort direction") type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Generating changelog" +func (Pipe) String() string { + return "generating changelog" } // Run the pipe diff --git a/pipeline/changelog/changelog_test.go b/pipeline/changelog/changelog_test.go index 55a776b2d..5cf58fd90 100644 --- a/pipeline/changelog/changelog_test.go +++ b/pipeline/changelog/changelog_test.go @@ -12,7 +12,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestChangelogProvidedViaFlag(t *testing.T) { diff --git a/pipeline/checksums/checksums.go b/pipeline/checksums/checksums.go index 5373598c4..f85a666bd 100644 --- a/pipeline/checksums/checksums.go +++ b/pipeline/checksums/checksums.go @@ -18,8 +18,8 @@ import ( type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Calculating checksums" +func (Pipe) String() string { + return "calculating checksums" } // Run the pipe @@ -52,6 +52,14 @@ func (Pipe) Run(ctx *context.Context) (err error) { return g.Wait() } +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.Checksum.NameTemplate != "" { + ctx.Config.Checksum.NameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt" + } + return nil +} + func checksums(ctx *context.Context, file *os.File, name string) error { log.WithField("file", name).Info("checksumming") var artifact = filepath.Join(ctx.Config.Dist, name) diff --git a/pipeline/checksums/checksums_test.go b/pipeline/checksums/checksums_test.go index 24d4a35c3..d4c890be9 100644 --- a/pipeline/checksums/checksums_test.go +++ b/pipeline/checksums/checksums_test.go @@ -11,7 +11,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestPipe(t *testing.T) { diff --git a/pipeline/cleandist/dist.go b/pipeline/cleandist/dist.go index 6e19ca7c3..7bbc1f7da 100644 --- a/pipeline/cleandist/dist.go +++ b/pipeline/cleandist/dist.go @@ -15,8 +15,8 @@ import ( type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Checking ./dist" +func (Pipe) String() string { + return "checking ./dist" } // Run the pipe diff --git a/pipeline/cleandist/dist_test.go b/pipeline/cleandist/dist_test.go index 3c3628e29..7429d45ad 100644 --- a/pipeline/cleandist/dist_test.go +++ b/pipeline/cleandist/dist_test.go @@ -59,5 +59,5 @@ func TestEmptyDistExists(t *testing.T) { } func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } diff --git a/pipeline/default.go b/pipeline/default.go new file mode 100644 index 000000000..259ed7bf6 --- /dev/null +++ b/pipeline/default.go @@ -0,0 +1,15 @@ +package pipeline + +import ( + "fmt" + + "github.com/goreleaser/goreleaser/context" +) + +// Defaulter interface +type Defaulter interface { + fmt.Stringer + + // Default sets the configuration defaults + Default(ctx *context.Context) error +} diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index 8fd1bf97a..5d7609a17 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -3,190 +3,52 @@ package defaults import ( - "fmt" - "strings" - "github.com/apex/log" - "github.com/goreleaser/goreleaser/config" "github.com/goreleaser/goreleaser/context" + "github.com/goreleaser/goreleaser/pipeline" + "github.com/goreleaser/goreleaser/pipeline/archive" + "github.com/goreleaser/goreleaser/pipeline/brew" + "github.com/goreleaser/goreleaser/pipeline/build" + "github.com/goreleaser/goreleaser/pipeline/checksums" + "github.com/goreleaser/goreleaser/pipeline/docker" + "github.com/goreleaser/goreleaser/pipeline/fpm" + "github.com/goreleaser/goreleaser/pipeline/release" + "github.com/goreleaser/goreleaser/pipeline/snapshot" ) -// NameTemplate default name_template for the archive. -const NameTemplate = "{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" - -// ReleaseNameTemplate is the default name for the release. -const ReleaseNameTemplate = "{{.Tag}}" - -// SnapshotNameTemplate represents the default format for snapshot release names. -const SnapshotNameTemplate = "SNAPSHOT-{{ .Commit }}" - -// ChecksumNameTemplate is the default name_template for the checksum file. -const ChecksumNameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt" - // Pipe for brew deployment type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Setting defaults" +func (Pipe) String() string { + return "setting defaults for:" +} + +var defaulters = []pipeline.Defaulter{ + snapshot.Pipe{}, + release.Pipe{}, + archive.Pipe{}, + build.Pipe{}, + fpm.Pipe{}, + checksums.Pipe{}, + docker.Pipe{}, + brew.Pipe{}, } // Run the pipe -func (Pipe) Run(ctx *context.Context) error { // nolint: gocyclo +func (Pipe) Run(ctx *context.Context) error { if ctx.Config.Dist == "" { ctx.Config.Dist = "dist" } - if ctx.Config.Release.NameTemplate == "" { - ctx.Config.Release.NameTemplate = ReleaseNameTemplate - } - if ctx.Config.Snapshot.NameTemplate == "" { - ctx.Config.Snapshot.NameTemplate = SnapshotNameTemplate - } - if ctx.Config.Checksum.NameTemplate == "" { - ctx.Config.Checksum.NameTemplate = ChecksumNameTemplate - } - if err := setReleaseDefaults(ctx); err != nil { - return err + for _, defaulter := range defaulters { + log.Infof("\t%s", defaulter.String()) + if err := defaulter.Default(ctx); err != nil { + return err + } } if ctx.Config.ProjectName == "" { ctx.Config.ProjectName = ctx.Config.Release.GitHub.Name } - - setBuildDefaults(ctx) - - if ctx.Config.Brew.Install == "" { - var installs []string - for _, build := range ctx.Config.Builds { - if !isBrewBuild(build) { - continue - } - installs = append( - installs, - fmt.Sprintf(`bin.install "%s"`, build.Binary), - ) - } - ctx.Config.Brew.Install = strings.Join(installs, "\n") - } - - if ctx.Config.Brew.CommitAuthor.Name == "" { - ctx.Config.Brew.CommitAuthor.Name = "goreleaserbot" - } - if ctx.Config.Brew.CommitAuthor.Email == "" { - ctx.Config.Brew.CommitAuthor.Email = "goreleaser@carlosbecker.com" - } - - err := setArchiveDefaults(ctx) - setDockerDefaults(ctx) - setFpmDefaults(ctx) log.WithField("config", ctx.Config).Debug("defaults set") - return err -} - -func setDockerDefaults(ctx *context.Context) { - if len(ctx.Config.Dockers) != 1 { - return - } - if ctx.Config.Dockers[0].Goos == "" { - ctx.Config.Dockers[0].Goos = "linux" - } - if ctx.Config.Dockers[0].Goarch == "" { - ctx.Config.Dockers[0].Goarch = "amd64" - } - if ctx.Config.Dockers[0].Binary == "" { - ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary - } - if ctx.Config.Dockers[0].Dockerfile == "" { - ctx.Config.Dockers[0].Dockerfile = "Dockerfile" - } -} - -func isBrewBuild(build config.Build) bool { - for _, ignore := range build.Ignore { - if ignore.Goos == "darwin" && ignore.Goarch == "amd64" { - return false - } - } - return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64") -} - -func contains(ss []string, s string) bool { - for _, zs := range ss { - if zs == s { - return true - } - } - return false -} - -func setReleaseDefaults(ctx *context.Context) error { - if ctx.Config.Release.GitHub.Name != "" { - return nil - } - repo, err := remoteRepo() - if err != nil { - return err - } - ctx.Config.Release.GitHub = repo return nil } - -func setBuildDefaults(ctx *context.Context) { - for i, build := range ctx.Config.Builds { - ctx.Config.Builds[i] = buildWithDefaults(ctx, build) - } - if len(ctx.Config.Builds) == 0 { - ctx.Config.Builds = []config.Build{ - buildWithDefaults(ctx, ctx.Config.SingleBuild), - } - } -} - -func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { - if build.Binary == "" { - build.Binary = ctx.Config.Release.GitHub.Name - } - if build.Main == "" { - build.Main = "." - } - if len(build.Goos) == 0 { - build.Goos = []string{"linux", "darwin"} - } - if len(build.Goarch) == 0 { - build.Goarch = []string{"amd64", "386"} - } - if len(build.Goarm) == 0 { - build.Goarm = []string{"6"} - } - if build.Ldflags == "" { - build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" - } - return build -} - -func setArchiveDefaults(ctx *context.Context) error { - if ctx.Config.Archive.NameTemplate == "" { - ctx.Config.Archive.NameTemplate = NameTemplate - } - if ctx.Config.Archive.Format == "" { - ctx.Config.Archive.Format = "tar.gz" - } - if len(ctx.Config.Archive.Files) == 0 { - ctx.Config.Archive.Files = []string{ - "licence*", - "LICENCE*", - "license*", - "LICENSE*", - "readme*", - "README*", - "changelog*", - "CHANGELOG*", - } - } - return nil -} - -func setFpmDefaults(ctx *context.Context) { - if ctx.Config.FPM.Bindir == "" { - ctx.Config.FPM.Bindir = "/usr/local/bin" - } -} diff --git a/pipeline/defaults/defaults_test.go b/pipeline/defaults/defaults_test.go index 0130a9aab..765baaf07 100644 --- a/pipeline/defaults/defaults_test.go +++ b/pipeline/defaults/defaults_test.go @@ -10,7 +10,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestFillBasicData(t *testing.T) { diff --git a/pipeline/docker/docker.go b/pipeline/docker/docker.go index d94574006..a517bf65b 100644 --- a/pipeline/docker/docker.go +++ b/pipeline/docker/docker.go @@ -23,8 +23,8 @@ var ErrNoDocker = errors.New("docker not present in $PATH") type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Creating Docker images" +func (Pipe) String() string { + return "creating Docker images" } // Run the pipe @@ -39,6 +39,27 @@ func (Pipe) Run(ctx *context.Context) error { return doRun(ctx) } +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + // TODO: this if condition looks wrong + if len(ctx.Config.Dockers) != 1 { + return nil + } + if ctx.Config.Dockers[0].Goos == "" { + ctx.Config.Dockers[0].Goos = "linux" + } + if ctx.Config.Dockers[0].Goarch == "" { + ctx.Config.Dockers[0].Goarch = "amd64" + } + if ctx.Config.Dockers[0].Binary == "" { + ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary + } + if ctx.Config.Dockers[0].Dockerfile == "" { + ctx.Config.Dockers[0].Dockerfile = "Dockerfile" + } + return nil +} + func doRun(ctx *context.Context) error { for _, docker := range ctx.Config.Dockers { var imagePlatform = docker.Goos + docker.Goarch + docker.Goarm diff --git a/pipeline/docker/docker_test.go b/pipeline/docker/docker_test.go index 0e2e74003..5be37a93b 100644 --- a/pipeline/docker/docker_test.go +++ b/pipeline/docker/docker_test.go @@ -96,7 +96,7 @@ func TestRunPipe(t *testing.T) { } func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestNoDockers(t *testing.T) { diff --git a/pipeline/env/env.go b/pipeline/env/env.go index a2e8b72a3..8cf9df52f 100644 --- a/pipeline/env/env.go +++ b/pipeline/env/env.go @@ -17,8 +17,8 @@ var ErrMissingToken = errors.New("missing GITHUB_TOKEN") type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Loading environment variables" +func (Pipe) String() string { + return "loading environment variables" } // Run the pipe diff --git a/pipeline/env/env_test.go b/pipeline/env/env_test.go index b3ef5131d..d1981b24d 100644 --- a/pipeline/env/env_test.go +++ b/pipeline/env/env_test.go @@ -12,7 +12,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestValidEnv(t *testing.T) { diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 2c9a1f5e3..00909e09d 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -23,8 +23,16 @@ var ErrNoFPM = errors.New("fpm not present in $PATH") type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Creating Linux packages with fpm" +func (Pipe) String() string { + return "creating Linux packages with fpm" +} + +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.FPM.Bindir == "" { + ctx.Config.FPM.Bindir = "/usr/local/bin" + } + return nil } // Run the pipe diff --git a/pipeline/fpm/fpm_test.go b/pipeline/fpm/fpm_test.go index 7a7baedc9..b9bde9903 100644 --- a/pipeline/fpm/fpm_test.go +++ b/pipeline/fpm/fpm_test.go @@ -14,7 +14,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestRunPipeNoFormats(t *testing.T) { diff --git a/pipeline/git/git.go b/pipeline/git/git.go index 884aaec82..08f7aeca9 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -20,8 +20,8 @@ import ( type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Getting and validating git state" +func (Pipe) String() string { + return "getting and validating git state" } // Run the pipe diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index ffe1dde24..7e304fdc2 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -14,7 +14,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestNotAGitFolder(t *testing.T) { diff --git a/pipeline/pipe.go b/pipeline/piper.go similarity index 81% rename from pipeline/pipe.go rename to pipeline/piper.go index f09b385aa..676c7922b 100644 --- a/pipeline/pipe.go +++ b/pipeline/piper.go @@ -1,12 +1,15 @@ // Package pipeline provides a generic pipe interface. package pipeline -import "github.com/goreleaser/goreleaser/context" +import ( + "fmt" -// Pipe interface -type Pipe interface { - // Name of the pipe - Description() string + "github.com/goreleaser/goreleaser/context" +) + +// Piper interface +type Piper interface { + fmt.Stringer // Run the pipe Run(ctx *context.Context) error diff --git a/pipeline/pipe_test.go b/pipeline/piper_test.go similarity index 100% rename from pipeline/pipe_test.go rename to pipeline/piper_test.go diff --git a/pipeline/release/release.go b/pipeline/release/release.go index 4336307c5..c0df9b0df 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -17,8 +17,8 @@ import ( type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Releasing to GitHub" +func (Pipe) String() string { + return "releasing to GitHub" } // Run the pipe @@ -30,6 +30,22 @@ func (Pipe) Run(ctx *context.Context) error { return doRun(ctx, c) } +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.Release.NameTemplate == "" { + ctx.Config.Release.NameTemplate = "{{.Tag}}" + } + if ctx.Config.Release.GitHub.Name != "" { + return nil + } + repo, err := remoteRepo() + if err != nil { + return err + } + ctx.Config.Release.GitHub = repo + return nil +} + func doRun(ctx *context.Context, c client.Client) error { if !ctx.Publish { return pipeline.Skip("--skip-publish is set") diff --git a/pipeline/release/release_test.go b/pipeline/release/release_test.go index 82ef2b16a..d1dbcc894 100644 --- a/pipeline/release/release_test.go +++ b/pipeline/release/release_test.go @@ -15,7 +15,7 @@ import ( ) func TestPipeDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestRunPipe(t *testing.T) { diff --git a/pipeline/defaults/remote.go b/pipeline/release/remote.go similarity index 98% rename from pipeline/defaults/remote.go rename to pipeline/release/remote.go index 194f32920..391e30be3 100644 --- a/pipeline/defaults/remote.go +++ b/pipeline/release/remote.go @@ -1,4 +1,4 @@ -package defaults +package release import ( "strings" diff --git a/pipeline/defaults/remote_test.go b/pipeline/release/remote_test.go similarity index 97% rename from pipeline/defaults/remote_test.go rename to pipeline/release/remote_test.go index 4ecd8bcf2..39fdf46b7 100644 --- a/pipeline/defaults/remote_test.go +++ b/pipeline/release/remote_test.go @@ -1,4 +1,4 @@ -package defaults +package release import ( "testing" diff --git a/pipeline/snapcraft/snapcraft.go b/pipeline/snapcraft/snapcraft.go index f7d25724d..46ae91626 100644 --- a/pipeline/snapcraft/snapcraft.go +++ b/pipeline/snapcraft/snapcraft.go @@ -50,8 +50,8 @@ type AppMetadata struct { type Pipe struct{} // Description of the pipe -func (Pipe) Description() string { - return "Creating Linux packages with snapcraft" +func (Pipe) String() string { + return "creating Linux packages with snapcraft" } // Run the pipe diff --git a/pipeline/snapcraft/snapcraft_test.go b/pipeline/snapcraft/snapcraft_test.go index b9fe5a306..d6dfd7b38 100644 --- a/pipeline/snapcraft/snapcraft_test.go +++ b/pipeline/snapcraft/snapcraft_test.go @@ -15,7 +15,7 @@ import ( ) func TestDescription(t *testing.T) { - assert.NotEmpty(t, Pipe{}.Description()) + assert.NotEmpty(t, Pipe{}.String()) } func TestRunPipeMissingInfo(t *testing.T) { diff --git a/pipeline/snapshot/package.go b/pipeline/snapshot/package.go new file mode 100644 index 000000000..b0a966baa --- /dev/null +++ b/pipeline/snapshot/package.go @@ -0,0 +1,2 @@ +// Package snapshot provides the snapshoting functionaly to goreleaser +package snapshot diff --git a/pipeline/snapshot/snapshot.go b/pipeline/snapshot/snapshot.go new file mode 100644 index 000000000..688d08cad --- /dev/null +++ b/pipeline/snapshot/snapshot.go @@ -0,0 +1,20 @@ +package snapshot + +import ( + "github.com/goreleaser/goreleaser/context" +) + +// Pipe for checksums +type Pipe struct{} + +func (Pipe) String() string { + return "generating changelog" +} + +// Default sets the pipe defaults +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.Snapshot.NameTemplate == "" { + ctx.Config.Snapshot.NameTemplate = "SNAPSHOT-{{ .Commit }}" + } + return nil +} diff --git a/pipeline/snapshot/snapshot_test.go b/pipeline/snapshot/snapshot_test.go new file mode 100644 index 000000000..a5e0ce5bb --- /dev/null +++ b/pipeline/snapshot/snapshot_test.go @@ -0,0 +1,15 @@ +package snapshot + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStringer(t *testing.T) { + assert.NotEmpty(t, Pipe{}.String()) +} + +func TestDefault(t *testing.T) { + // TODO: implement this +}