github.com/tomsquest/goreleaser@v0.34.3-0.20171008022654-7d6ef4d338b3/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 // ChecksumNameTemplate is the default name_template for the checksum file. 21 const ChecksumNameTemplate = "{{ .ProjectName }}_{{ .Version }}_checksums.txt" 22 23 // Pipe for brew deployment 24 type Pipe struct{} 25 26 // Description of the pipe 27 func (Pipe) Description() string { 28 return "Setting defaults" 29 } 30 31 // Run the pipe 32 func (Pipe) Run(ctx *context.Context) error { 33 ctx.Config.Dist = "dist" 34 if ctx.Config.Snapshot.NameTemplate == "" { 35 ctx.Config.Snapshot.NameTemplate = SnapshotNameTemplate 36 } 37 if ctx.Config.Checksum.NameTemplate == "" { 38 ctx.Config.Checksum.NameTemplate = ChecksumNameTemplate 39 } 40 if err := setReleaseDefaults(ctx); err != nil { 41 return err 42 } 43 if ctx.Config.ProjectName == "" { 44 ctx.Config.ProjectName = ctx.Config.Release.GitHub.Name 45 } 46 47 setBuildDefaults(ctx) 48 49 if ctx.Config.Brew.Install == "" { 50 var installs []string 51 for _, build := range ctx.Config.Builds { 52 if !isBrewBuild(build) { 53 continue 54 } 55 installs = append( 56 installs, 57 fmt.Sprintf(`bin.install "%s"`, build.Binary), 58 ) 59 } 60 ctx.Config.Brew.Install = strings.Join(installs, "\n") 61 } 62 63 if ctx.Config.Brew.CommitAuthor.Name == "" { 64 ctx.Config.Brew.CommitAuthor.Name = "goreleaserbot" 65 } 66 if ctx.Config.Brew.CommitAuthor.Email == "" { 67 ctx.Config.Brew.CommitAuthor.Email = "goreleaser@carlosbecker.com" 68 } 69 70 err := setArchiveDefaults(ctx) 71 setDockerDefaults(ctx) 72 log.WithField("config", ctx.Config).Debug("defaults set") 73 return err 74 } 75 76 func setDockerDefaults(ctx *context.Context) { 77 if len(ctx.Config.Dockers) != 1 { 78 return 79 } 80 if ctx.Config.Dockers[0].Goos == "" { 81 ctx.Config.Dockers[0].Goos = "linux" 82 } 83 if ctx.Config.Dockers[0].Goarch == "" { 84 ctx.Config.Dockers[0].Goarch = "amd64" 85 } 86 if ctx.Config.Dockers[0].Binary == "" { 87 ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary 88 } 89 if ctx.Config.Dockers[0].Dockerfile == "" { 90 ctx.Config.Dockers[0].Dockerfile = "Dockerfile" 91 } 92 } 93 94 func isBrewBuild(build config.Build) bool { 95 for _, ignore := range build.Ignore { 96 if ignore.Goos == "darwin" && ignore.Goarch == "amd64" { 97 return false 98 } 99 } 100 return contains(build.Goos, "darwin") && contains(build.Goarch, "amd64") 101 } 102 103 func contains(ss []string, s string) bool { 104 for _, zs := range ss { 105 if zs == s { 106 return true 107 } 108 } 109 return false 110 } 111 112 func setReleaseDefaults(ctx *context.Context) error { 113 if ctx.Config.Release.GitHub.Name != "" { 114 return nil 115 } 116 repo, err := remoteRepo() 117 if err != nil { 118 return fmt.Errorf("failed reading repo from git: %v", err.Error()) 119 } 120 ctx.Config.Release.GitHub = repo 121 return nil 122 } 123 124 func setBuildDefaults(ctx *context.Context) { 125 for i, build := range ctx.Config.Builds { 126 ctx.Config.Builds[i] = buildWithDefaults(ctx, build) 127 } 128 if len(ctx.Config.Builds) == 0 { 129 ctx.Config.Builds = []config.Build{ 130 buildWithDefaults(ctx, ctx.Config.SingleBuild), 131 } 132 } 133 } 134 135 func buildWithDefaults(ctx *context.Context, build config.Build) config.Build { 136 if build.Binary == "" { 137 build.Binary = ctx.Config.Release.GitHub.Name 138 } 139 if build.Main == "" { 140 build.Main = "." 141 } 142 if len(build.Goos) == 0 { 143 build.Goos = []string{"linux", "darwin"} 144 } 145 if len(build.Goarch) == 0 { 146 build.Goarch = []string{"amd64", "386"} 147 } 148 if len(build.Goarm) == 0 { 149 build.Goarm = []string{"6"} 150 } 151 if build.Ldflags == "" { 152 build.Ldflags = "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}" 153 } 154 return build 155 } 156 157 func setArchiveDefaults(ctx *context.Context) error { 158 if ctx.Config.Archive.NameTemplate == "" { 159 ctx.Config.Archive.NameTemplate = NameTemplate 160 } 161 if ctx.Config.Archive.Format == "" { 162 ctx.Config.Archive.Format = "tar.gz" 163 } 164 if len(ctx.Config.Archive.Files) == 0 { 165 ctx.Config.Archive.Files = []string{ 166 "licence*", 167 "LICENCE*", 168 "license*", 169 "LICENSE*", 170 "readme*", 171 "README*", 172 "changelog*", 173 "CHANGELOG*", 174 } 175 } 176 return nil 177 }