From 72a32f0475d226b8393f1e9e03f33e391712b975 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 1 May 2017 09:59:18 -0300 Subject: [PATCH] improved defaults cyclo complexity --- pipeline/defaults/defaults.go | 42 +++++++++++++++++++++--------- pipeline/defaults/defaults_test.go | 16 ++++++++++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index e0b65dc3d..4c4a414e4 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -3,7 +3,7 @@ package defaults import ( - "errors" + "fmt" "io/ioutil" "strings" @@ -16,7 +16,7 @@ var defaultFiles = []string{"licence", "license", "readme", "changelog"} const NameTemplate = "{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" // SnapshotNameTemplate represents the default format for snapshot release names. -const SnapshotNameTemplate = "SNAPSHOT-{{.Commit}}" +const SnapshotNameTemplate = "SNAPSHOT-{{ .Commit }}" // Pipe for brew deployment type Pipe struct{} @@ -28,16 +28,36 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { + ctx.Config.Dist = "dist" if ctx.Config.Snapshot.NameTemplate == "" { ctx.Config.Snapshot.NameTemplate = SnapshotNameTemplate } - if ctx.Config.Release.GitHub.Name == "" { - repo, err := remoteRepo() - ctx.Config.Release.GitHub = repo - if err != nil { - return errors.New("failed reading repo from git: " + err.Error()) - } + if err := setReleaseDefaults(ctx); err != nil { + return err } + setBuildDefaults(ctx) + if ctx.Config.Brew.Install == "" { + ctx.Config.Brew.Install = fmt.Sprintf( + `bin.install "%s"`, + ctx.Config.Build.Binary, + ) + } + return setArchiveDefaults(ctx) +} + +func setReleaseDefaults(ctx *context.Context) error { + if ctx.Config.Release.GitHub.Name != "" { + return nil + } + repo, err := remoteRepo() + if err != nil { + return fmt.Errorf("failed reading repo from git: %v", err.Error()) + } + ctx.Config.Release.GitHub = repo + return nil +} + +func setBuildDefaults(ctx *context.Context) { if ctx.Config.Build.Binary == "" { ctx.Config.Build.Binary = ctx.Config.Release.GitHub.Name } @@ -56,7 +76,9 @@ func (Pipe) Run(ctx *context.Context) error { if ctx.Config.Build.Ldflags == "" { ctx.Config.Build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" } +} +func setArchiveDefaults(ctx *context.Context) error { if ctx.Config.Archive.NameTemplate == "" { ctx.Config.Archive.NameTemplate = NameTemplate } @@ -82,10 +104,6 @@ func (Pipe) Run(ctx *context.Context) error { } ctx.Config.Archive.Files = files } - if ctx.Config.Brew.Install == "" { - ctx.Config.Brew.Install = "bin.install \"" + ctx.Config.Build.Binary + "\"" - } - ctx.Config.Dist = "dist" return nil } diff --git a/pipeline/defaults/defaults_test.go b/pipeline/defaults/defaults_test.go index 1deb79d42..e851ebf3d 100644 --- a/pipeline/defaults/defaults_test.go +++ b/pipeline/defaults/defaults_test.go @@ -41,6 +41,22 @@ func TestFillBasicData(t *testing.T) { ) } +func TestFillPartial(t *testing.T) { + assert := assert.New(t) + + var ctx = &context.Context{ + Config: config.Project{ + Release: config.Release{ + GitHub: config.Repo{ + Owner: "goreleaser", + Name: "test", + }, + }, + }, + } + assert.NoError(Pipe{}.Run(ctx)) +} + func TestFilesFilled(t *testing.T) { assert := assert.New(t)