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