github.com/marianogappa/goreleaser@v0.26.2-0.20170715090149-96acd0a9fc46/pipeline/defaults/defaults.go (about) 1 // Package defaults implements the Pipe interface providing default values 2 // for missing configuration. 3 package defaults 4 5 import ( 6 "fmt" 7 "strings" 8 9 "github.com/apex/log" 10 "github.com/goreleaser/goreleaser/config" 11 "github.com/goreleaser/goreleaser/context" 12 ) 13 14 // NameTemplate default name_template for the archive. 15 const NameTemplate = "{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" 16 17 // SnapshotNameTemplate represents the default format for snapshot release names. 18 const SnapshotNameTemplate = "SNAPSHOT-{{ .Commit }}" 19 20 // Pipe for brew deployment 21 type Pipe struct{} 22 23 // Description of the pipe 24 func (Pipe) Description() string { 25 return "Setting defaults" 26 } 27 28 // Run the pipe 29 func (Pipe) Run(ctx *context.Context) error { 30 ctx.Config.Dist = "dist" 31 if ctx.Config.Snapshot.NameTemplate == "" { 32 ctx.Config.Snapshot.NameTemplate = SnapshotNameTemplate 33 } 34 if err := setReleaseDefaults(ctx); err != nil { 35 return err 36 } 37 if ctx.Config.ProjectName == "" { 38 ctx.Config.ProjectName = ctx.Config.Release.GitHub.Name 39 } 40 setBuildDefaults(ctx) 41 if ctx.Config.Brew.Install == "" { 42 var installs []string 43 for _, build := range ctx.Config.Builds { 44 if !isBrewBuild(build) { 45 continue 46 } 47 installs = append( 48 installs, 49 fmt.Sprintf(`bin.install "%s"`, build.Binary), 50 ) 51 } 52 ctx.Config.Brew.Install = strings.Join(installs, "\n") 53 } 54 err := setArchiveDefaults(ctx) 55 log.WithField("config", ctx.Config).Debug("defaults set") 56 return err 57 } 58 59 func isBrewBuild(build config.Build) bool { 60 for _, ignore := range build.Ignore { 61 if ignore.Goos == "darwin" && ignore.Goarch == "amd64" { 62 return false 63 } 64 } 65 return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64") 66 } 67 68 func contains(ss []string, s string) bool { 69 for _, zs := range ss { 70 if zs == s { 71 return true 72 } 73 } 74 return false 75 } 76 77 func setReleaseDefaults(ctx *context.Context) error { 78 if ctx.Config.Release.GitHub.Name != "" { 79 return nil 80 } 81 repo, err := remoteRepo() 82 if err != nil { 83 return fmt.Errorf("failed reading repo from git: %v", err.Error()) 84 } 85 ctx.Config.Release.GitHub = repo 86 return nil 87 } 88 89 func setBuildDefaults(ctx *context.Context) { 90 for i, build := range ctx.Config.Builds { 91 ctx.Config.Builds[i] = buildWithDefaults(ctx, build) 92 } 93 if len(ctx.Config.Builds) == 0 { 94 ctx.Config.Builds = []config.Build{ 95 buildWithDefaults(ctx, ctx.Config.SingleBuild), 96 } 97 } 98 } 99 100 func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { 101 if build.Binary == "" { 102 build.Binary = ctx.Config.Release.GitHub.Name 103 } 104 if build.Main == "" { 105 build.Main = "." 106 } 107 if len(build.Goos) == 0 { 108 build.Goos = []string{"linux", "darwin"} 109 } 110 if len(build.Goarch) == 0 { 111 build.Goarch = []string{"amd64", "386"} 112 } 113 if len(build.Goarm) == 0 { 114 build.Goarm = []string{"6"} 115 } 116 if build.Ldflags == "" { 117 build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" 118 } 119 return build 120 } 121 122 func setArchiveDefaults(ctx *context.Context) error { 123 if ctx.Config.Archive.NameTemplate == "" { 124 ctx.Config.Archive.NameTemplate = NameTemplate 125 } 126 if ctx.Config.Archive.Format == "" { 127 ctx.Config.Archive.Format = "tar.gz" 128 } 129 if len(ctx.Config.Archive.Files) == 0 { 130 ctx.Config.Archive.Files = []string{ 131 "licence*", 132 "LICENCE*", 133 "license*", 134 "LICENSE*", 135 "readme*", 136 "README*", 137 "changelog*", 138 "CHANGELOG*", 139 } 140 } 141 return nil 142 }