github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/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  	EmptyFolders []string          `yaml:"empty_folders,omitempty"`
   193  	Files        map[string]string `yaml:",omitempty"`
   194  	ConfigFiles  map[string]string `yaml:"config_files,omitempty"`
   195  	Scripts      NFPMScripts       `yaml:"scripts,omitempty"`
   196  }
   197  
   198  // Sign config
   199  type Sign struct {
   200  	Cmd       string   `yaml:"cmd,omitempty"`
   201  	Args      []string `yaml:"args,omitempty"`
   202  	Signature string   `yaml:"signature,omitempty"`
   203  	Artifacts string   `yaml:"artifacts,omitempty"`
   204  }
   205  
   206  // SnapcraftAppMetadata for the binaries that will be in the snap package
   207  type SnapcraftAppMetadata struct {
   208  	Plugs  []string
   209  	Daemon string
   210  }
   211  
   212  // Snapcraft config
   213  type Snapcraft struct {
   214  	NameTemplate string            `yaml:"name_template,omitempty"`
   215  	Replacements map[string]string `yaml:",omitempty"`
   216  
   217  	Name        string                          `yaml:",omitempty"`
   218  	Summary     string                          `yaml:",omitempty"`
   219  	Description string                          `yaml:",omitempty"`
   220  	Grade       string                          `yaml:",omitempty"`
   221  	Confinement string                          `yaml:",omitempty"`
   222  	Apps        map[string]SnapcraftAppMetadata `yaml:",omitempty"`
   223  }
   224  
   225  // Snapshot config
   226  type Snapshot struct {
   227  	NameTemplate string `yaml:"name_template,omitempty"`
   228  }
   229  
   230  // Checksum config
   231  type Checksum struct {
   232  	NameTemplate string `yaml:"name_template,omitempty"`
   233  }
   234  
   235  // Docker image config
   236  type Docker struct {
   237  	Binary         string   `yaml:",omitempty"`
   238  	Goos           string   `yaml:",omitempty"`
   239  	Goarch         string   `yaml:",omitempty"`
   240  	Goarm          string   `yaml:",omitempty"`
   241  	Image          string   `yaml:",omitempty"`
   242  	Dockerfile     string   `yaml:",omitempty"`
   243  	Latest         bool     `yaml:",omitempty"`
   244  	SkipPush       bool     `yaml:"skip_push,omitempty"`
   245  	OldTagTemplate string   `yaml:"tag_template,omitempty"`
   246  	TagTemplates   []string `yaml:"tag_templates,omitempty"`
   247  	Files          []string `yaml:"extra_files,omitempty"`
   248  }
   249  
   250  // Artifactory server configuration
   251  type Artifactory struct {
   252  	Target   string `yaml:",omitempty"`
   253  	Name     string `yaml:",omitempty"`
   254  	Username string `yaml:",omitempty"`
   255  	Mode     string `yaml:",omitempty"`
   256  }
   257  
   258  // Filters config
   259  type Filters struct {
   260  	Exclude []string `yaml:",omitempty"`
   261  }
   262  
   263  // Changelog Config
   264  type Changelog struct {
   265  	Filters Filters `yaml:",omitempty"`
   266  	Sort    string  `yaml:",omitempty"`
   267  }
   268  
   269  // EnvFiles holds paths to files that contains environment variables
   270  // values like the github token for example
   271  type EnvFiles struct {
   272  	GitHubToken string `yaml:"github_token,omitempty"`
   273  }
   274  
   275  // Git config
   276  type Git struct {
   277  	ShortHash bool `yaml:"short_hash,omitempty"`
   278  }
   279  
   280  // Before config
   281  type Before struct {
   282  	Hooks []string `yaml:",omitempty"`
   283  }
   284  
   285  // S3 contains s3 config
   286  type S3 struct {
   287  	Region   string
   288  	Bucket   string
   289  	Folder   string
   290  	Profile  string
   291  	Endpoint string // used for minio for example
   292  }
   293  
   294  // Project includes all project configuration
   295  type Project struct {
   296  	ProjectName   string        `yaml:"project_name,omitempty"`
   297  	Release       Release       `yaml:",omitempty"`
   298  	Brew          Homebrew      `yaml:",omitempty"`
   299  	Scoop         Scoop         `yaml:",omitempty"`
   300  	Builds        []Build       `yaml:",omitempty"`
   301  	Archive       Archive       `yaml:",omitempty"`
   302  	FPM           NFPM          `yaml:",omitempty"` // deprecated
   303  	NFPM          NFPM          `yaml:",omitempty"`
   304  	Snapcraft     Snapcraft     `yaml:",omitempty"`
   305  	Snapshot      Snapshot      `yaml:",omitempty"`
   306  	Checksum      Checksum      `yaml:",omitempty"`
   307  	Dockers       []Docker      `yaml:",omitempty"`
   308  	Artifactories []Artifactory `yaml:",omitempty"`
   309  	S3            []S3          `yaml:"s3,omitempty"`
   310  	Changelog     Changelog     `yaml:",omitempty"`
   311  	Dist          string        `yaml:",omitempty"`
   312  	Sign          Sign          `yaml:",omitempty"`
   313  	EnvFiles      EnvFiles      `yaml:"env_files,omitempty"`
   314  	Git           Git           `yaml:",omitempty"`
   315  	Before        Before        `yaml:",omitempty"`
   316  
   317  	// this is a hack ¯\_(ツ)_/¯
   318  	SingleBuild Build `yaml:"build,omitempty"`
   319  
   320  	// should be set if using github enterprise
   321  	GitHubURLs GitHubURLs `yaml:"github_urls,omitempty"`
   322  }
   323  
   324  // Load config file
   325  func Load(file string) (config Project, err error) {
   326  	f, err := os.Open(file)
   327  	if err != nil {
   328  		return
   329  	}
   330  	log.WithField("file", file).Info("loading config file")
   331  	return LoadReader(f)
   332  }
   333  
   334  // LoadReader config via io.Reader
   335  func LoadReader(fd io.Reader) (config Project, err error) {
   336  	data, err := ioutil.ReadAll(fd)
   337  	if err != nil {
   338  		return config, err
   339  	}
   340  	err = yaml.UnmarshalStrict(data, &config)
   341  	log.WithField("config", config).Debug("loaded config file")
   342  	return config, err
   343  }