github.com/elyscape/goreleaser@v0.66.1-0.20180515111211-5252f74ade63/config/config.go (about) 1 // Package config contains the model and loader of the goreleaser configuration 2 // file. 3 package config 4 5 import ( 6 "io" 7 "io/ioutil" 8 "os" 9 "strings" 10 11 "github.com/apex/log" 12 "gopkg.in/yaml.v2" 13 ) 14 15 // GitHubURLs holds the URLs to be used when using github enterprise 16 type GitHubURLs struct { 17 API string `yaml:"api,omitempty"` 18 Upload string `yaml:"upload,omitempty"` 19 Download string `yaml:"download,omitempty"` 20 } 21 22 // Repo represents any kind of repo (github, gitlab, etc) 23 type Repo struct { 24 Owner string `yaml:",omitempty"` 25 Name string `yaml:",omitempty"` 26 } 27 28 // String of the repo, e.g. owner/name 29 func (r Repo) String() string { 30 if r.Owner == "" && r.Name == "" { 31 return "" 32 } 33 return r.Owner + "/" + r.Name 34 } 35 36 // Homebrew contains the brew section 37 type Homebrew struct { 38 Name string `yaml:",omitempty"` 39 GitHub Repo `yaml:",omitempty"` 40 CommitAuthor CommitAuthor `yaml:"commit_author,omitempty"` 41 Folder string `yaml:",omitempty"` 42 Caveats string `yaml:",omitempty"` 43 Plist string `yaml:",omitempty"` 44 Install string `yaml:",omitempty"` 45 Dependencies []string `yaml:",omitempty"` 46 BuildDependencies []string `yaml:"build_dependencies,omitempty"` 47 Test string `yaml:",omitempty"` 48 Conflicts []string `yaml:",omitempty"` 49 Description string `yaml:",omitempty"` 50 Homepage string `yaml:",omitempty"` 51 SkipUpload bool `yaml:"skip_upload,omitempty"` 52 DownloadStrategy string `yaml:"download_strategy,omitempty"` 53 SourceTarball string `yaml:"-"` 54 } 55 56 // Scoop contains the scoop.sh section 57 type Scoop struct { 58 Bucket Repo `yaml:",omitempty"` 59 CommitAuthor CommitAuthor `yaml:"commit_author,omitempty"` 60 Homepage string `yaml:",omitempty"` 61 Description string `yaml:",omitempty"` 62 License string `yaml:",omitempty"` 63 } 64 65 // CommitAuthor is the author of a Git commit 66 type CommitAuthor struct { 67 Name string `yaml:",omitempty"` 68 Email string `yaml:",omitempty"` 69 } 70 71 // Hooks define actions to run before and/or after something 72 type Hooks struct { 73 Pre string `yaml:",omitempty"` 74 Post string `yaml:",omitempty"` 75 } 76 77 // IgnoredBuild represents a build ignored by the user 78 type IgnoredBuild struct { 79 Goos, Goarch, Goarm string 80 } 81 82 // StringArray is a wrapper for an array of strings 83 type StringArray []string 84 85 // UnmarshalYAML is a custom unmarshaler that wraps strings in arrays 86 func (a *StringArray) UnmarshalYAML(unmarshal func(interface{}) error) error { 87 var strings []string 88 if err := unmarshal(&strings); err != nil { 89 var str string 90 if err := unmarshal(&str); err != nil { 91 return err 92 } 93 *a = []string{str} 94 } else { 95 *a = strings 96 } 97 return nil 98 } 99 100 // FlagArray is a wrapper for an array of strings 101 type FlagArray []string 102 103 // UnmarshalYAML is a custom unmarshaler that wraps strings in arrays 104 func (a *FlagArray) UnmarshalYAML(unmarshal func(interface{}) error) error { 105 var flags []string 106 if err := unmarshal(&flags); err != nil { 107 var flagstr string 108 if err := unmarshal(&flagstr); err != nil { 109 return err 110 } 111 *a = strings.Fields(flagstr) 112 } else { 113 *a = flags 114 } 115 return nil 116 } 117 118 // Build contains the build configuration section 119 type Build struct { 120 Goos []string `yaml:",omitempty"` 121 Goarch []string `yaml:",omitempty"` 122 Goarm []string `yaml:",omitempty"` 123 Targets []string `yaml:",omitempty"` 124 Ignore []IgnoredBuild `yaml:",omitempty"` 125 Main string `yaml:",omitempty"` 126 Ldflags StringArray `yaml:",omitempty"` 127 Flags FlagArray `yaml:",omitempty"` 128 Binary string `yaml:",omitempty"` 129 Hooks Hooks `yaml:",omitempty"` 130 Env []string `yaml:",omitempty"` 131 Lang string `yaml:",omitempty"` 132 Asmflags StringArray `yaml:",omitempty"` 133 Gcflags StringArray `yaml:",omitempty"` 134 } 135 136 // FormatOverride is used to specify a custom format for a specific GOOS. 137 type FormatOverride struct { 138 Goos string `yaml:",omitempty"` 139 Format string `yaml:",omitempty"` 140 } 141 142 // Archive config used for the archive 143 type Archive struct { 144 NameTemplate string `yaml:"name_template,omitempty"` 145 Replacements map[string]string `yaml:",omitempty"` 146 147 Format string `yaml:",omitempty"` 148 FormatOverrides []FormatOverride `yaml:"format_overrides,omitempty"` 149 WrapInDirectory bool `yaml:"wrap_in_directory,omitempty"` 150 Files []string `yaml:",omitempty"` 151 } 152 153 // Release config used for the GitHub release 154 type Release struct { 155 GitHub Repo `yaml:",omitempty"` 156 Draft bool `yaml:",omitempty"` 157 Prerelease bool `yaml:",omitempty"` 158 Disable bool `yaml:",omitempty"` 159 NameTemplate string `yaml:"name_template,omitempty"` 160 } 161 162 // NFPM config 163 type NFPM struct { 164 NFPMOverridables `yaml:",inline"` 165 Overrides map[string]NFPMOverridables `yaml:"overrides,omitempty"` 166 167 Formats []string `yaml:",omitempty"` 168 Vendor string `yaml:",omitempty"` 169 Homepage string `yaml:",omitempty"` 170 Maintainer string `yaml:",omitempty"` 171 Description string `yaml:",omitempty"` 172 License string `yaml:",omitempty"` 173 Bindir string `yaml:",omitempty"` 174 } 175 176 // NFPMScripts is used to specify maintainer scripts 177 type NFPMScripts struct { 178 PreInstall string `yaml:"preinstall,omitempty"` 179 PostInstall string `yaml:"postinstall,omitempty"` 180 PreRemove string `yaml:"preremove,omitempty"` 181 PostRemove string `yaml:"postremove,omitempty"` 182 } 183 184 // NFPMOverridables is used to specify per package format settings 185 type NFPMOverridables struct { 186 NameTemplate string `yaml:"name_template,omitempty"` 187 Replacements map[string]string `yaml:",omitempty"` 188 Dependencies []string `yaml:",omitempty"` 189 Recommends []string `yaml:",omitempty"` 190 Suggests []string `yaml:",omitempty"` 191 Conflicts []string `yaml:",omitempty"` 192 Files map[string]string `yaml:",omitempty"` 193 ConfigFiles map[string]string `yaml:"config_files,omitempty"` 194 Scripts NFPMScripts `yaml:"scripts,omitempty"` 195 } 196 197 // Sign config 198 type Sign struct { 199 Cmd string `yaml:"cmd,omitempty"` 200 Args []string `yaml:"args,omitempty"` 201 Signature string `yaml:"signature,omitempty"` 202 Artifacts string `yaml:"artifacts,omitempty"` 203 } 204 205 // SnapcraftAppMetadata for the binaries that will be in the snap package 206 type SnapcraftAppMetadata struct { 207 Plugs []string 208 Daemon string 209 } 210 211 // Snapcraft config 212 type Snapcraft struct { 213 NameTemplate string `yaml:"name_template,omitempty"` 214 Replacements map[string]string `yaml:",omitempty"` 215 216 Name string `yaml:",omitempty"` 217 Summary string `yaml:",omitempty"` 218 Description string `yaml:",omitempty"` 219 Grade string `yaml:",omitempty"` 220 Confinement string `yaml:",omitempty"` 221 Apps map[string]SnapcraftAppMetadata `yaml:",omitempty"` 222 } 223 224 // Snapshot config 225 type Snapshot struct { 226 NameTemplate string `yaml:"name_template,omitempty"` 227 } 228 229 // Checksum config 230 type Checksum struct { 231 NameTemplate string `yaml:"name_template,omitempty"` 232 } 233 234 // Docker image config 235 type Docker struct { 236 Binary string `yaml:",omitempty"` 237 Goos string `yaml:",omitempty"` 238 Goarch string `yaml:",omitempty"` 239 Goarm string `yaml:",omitempty"` 240 Image string `yaml:",omitempty"` 241 Dockerfile string `yaml:",omitempty"` 242 Latest bool `yaml:",omitempty"` 243 SkipPush bool `yaml:"skip_push,omitempty"` 244 OldTagTemplate string `yaml:"tag_template,omitempty"` 245 TagTemplates []string `yaml:"tag_templates,omitempty"` 246 Files []string `yaml:"extra_files,omitempty"` 247 } 248 249 // Artifactory server configuration 250 type Artifactory struct { 251 Target string `yaml:",omitempty"` 252 Name string `yaml:",omitempty"` 253 Username string `yaml:",omitempty"` 254 Mode string `yaml:",omitempty"` 255 } 256 257 // Filters config 258 type Filters struct { 259 Exclude []string `yaml:",omitempty"` 260 } 261 262 // Changelog Config 263 type Changelog struct { 264 Filters Filters `yaml:",omitempty"` 265 Sort string `yaml:",omitempty"` 266 } 267 268 // EnvFiles holds paths to files that contains environment variables 269 // values like the github token for example 270 type EnvFiles struct { 271 GitHubToken string `yaml:"github_token,omitempty"` 272 } 273 274 // Git config 275 type Git struct { 276 ShortHash bool `yaml:"short_hash,omitempty"` 277 } 278 279 // Before config 280 type Before struct { 281 Hooks []string `yaml:",omitempty"` 282 } 283 284 // S3 contains s3 config 285 type S3 struct { 286 Region string 287 Bucket string 288 Folder string 289 Profile string 290 Endpoint string // used for minio for example 291 } 292 293 // Project includes all project configuration 294 type Project struct { 295 ProjectName string `yaml:"project_name,omitempty"` 296 Release Release `yaml:",omitempty"` 297 Brew Homebrew `yaml:",omitempty"` 298 Scoop Scoop `yaml:",omitempty"` 299 Builds []Build `yaml:",omitempty"` 300 Archive Archive `yaml:",omitempty"` 301 FPM NFPM `yaml:",omitempty"` // deprecated 302 NFPM NFPM `yaml:",omitempty"` 303 Snapcraft Snapcraft `yaml:",omitempty"` 304 Snapshot Snapshot `yaml:",omitempty"` 305 Checksum Checksum `yaml:",omitempty"` 306 Dockers []Docker `yaml:",omitempty"` 307 Artifactories []Artifactory `yaml:",omitempty"` 308 S3 []S3 `yaml:"s3,omitempty"` 309 Changelog Changelog `yaml:",omitempty"` 310 Dist string `yaml:",omitempty"` 311 Sign Sign `yaml:",omitempty"` 312 EnvFiles EnvFiles `yaml:"env_files,omitempty"` 313 Git Git `yaml:",omitempty"` 314 Before Before `yaml:",omitempty"` 315 316 // this is a hack ¯\_(ツ)_/¯ 317 SingleBuild Build `yaml:"build,omitempty"` 318 319 // should be set if using github enterprise 320 GitHubURLs GitHubURLs `yaml:"github_urls,omitempty"` 321 } 322 323 // Load config file 324 func Load(file string) (config Project, err error) { 325 f, err := os.Open(file) 326 if err != nil { 327 return 328 } 329 log.WithField("file", file).Info("loading config file") 330 return LoadReader(f) 331 } 332 333 // LoadReader config via io.Reader 334 func LoadReader(fd io.Reader) (config Project, err error) { 335 data, err := ioutil.ReadAll(fd) 336 if err != nil { 337 return config, err 338 } 339 err = yaml.UnmarshalStrict(data, &config) 340 log.WithField("config", config).Debug("loaded config file") 341 return config, err 342 }