1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00
goreleaser/goreleaserlib/goreleaser.go

104 lines
2.8 KiB
Go
Raw Normal View History

package goreleaserlib
2017-04-21 15:25:32 -03:00
import (
2017-06-22 00:09:14 -03:00
"fmt"
2017-04-21 15:25:32 -03:00
"io/ioutil"
"os"
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
2017-04-21 15:25:32 -03:00
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/pipeline"
"github.com/goreleaser/goreleaser/pipeline/archive"
"github.com/goreleaser/goreleaser/pipeline/brew"
"github.com/goreleaser/goreleaser/pipeline/build"
"github.com/goreleaser/goreleaser/pipeline/checksums"
"github.com/goreleaser/goreleaser/pipeline/defaults"
"github.com/goreleaser/goreleaser/pipeline/env"
"github.com/goreleaser/goreleaser/pipeline/fpm"
"github.com/goreleaser/goreleaser/pipeline/git"
"github.com/goreleaser/goreleaser/pipeline/release"
2017-06-22 00:09:14 -03:00
yaml "gopkg.in/yaml.v1"
2017-04-21 15:25:32 -03:00
)
var pipes = []pipeline.Pipe{
defaults.Pipe{}, // load default configs
git.Pipe{}, // get and validate git repo state
env.Pipe{}, // load and validate environment variables
build.Pipe{}, // build
archive.Pipe{}, // archive (tar.gz, zip, etc)
fpm.Pipe{}, // archive via fpm (deb, rpm, etc)
checksums.Pipe{}, // checksums of the files
release.Pipe{}, // release to github
brew.Pipe{}, // push to brew tap
}
// Flags interface represents an extractor of cli flags
type Flags interface {
IsSet(s string) bool
String(s string) string
Bool(s string) bool
}
// Release runs the release process with the given flags
func Release(flags Flags) error {
var file = flags.String("config")
var notes = flags.String("release-notes")
cfg, err := config.Load(file)
if err != nil {
// Allow file not found errors if config file was not
// explicitly specified
_, statErr := os.Stat(file)
if !os.IsNotExist(statErr) || flags.IsSet("config") {
return err
}
2017-06-22 00:09:14 -03:00
log.WithField("file", file).Warn("Could not load")
2017-04-21 15:25:32 -03:00
}
var ctx = context.New(cfg)
ctx.Validate = !flags.Bool("skip-validate")
ctx.Publish = !flags.Bool("skip-publish")
if notes != "" {
bts, err := ioutil.ReadFile(notes)
if err != nil {
return err
}
2017-06-22 00:09:14 -03:00
log.WithField("notes", notes).Info("Loaded custom release notes")
2017-04-21 15:25:32 -03:00
ctx.ReleaseNotes = string(bts)
}
2017-04-29 12:49:22 +02:00
ctx.Snapshot = flags.Bool("snapshot")
if ctx.Snapshot {
2017-06-22 00:09:14 -03:00
log.Info("Publishing disabled in snapshot mode")
2017-04-29 12:49:22 +02:00
ctx.Publish = false
}
2017-04-21 15:25:32 -03:00
for _, pipe := range pipes {
2017-06-22 00:09:14 -03:00
log.Infof(pipe.Description())
2017-04-21 15:25:32 -03:00
if err := pipe.Run(ctx); err != nil {
return err
}
}
2017-06-22 00:09:14 -03:00
log.Info("Done!")
2017-04-21 15:25:32 -03:00
return nil
}
2017-04-27 11:03:26 +02:00
// InitProject creates an example goreleaser.yml in the current directory
func InitProject(filename string) error {
2017-04-27 11:03:26 +02:00
if _, err := os.Stat(filename); !os.IsNotExist(err) {
if err != nil {
return err
}
return fmt.Errorf("%s already exists", filename)
}
var ctx = context.New(config.Project{})
var pipe = defaults.Pipe{}
if err := pipe.Run(ctx); err != nil {
return err
}
out, err := yaml.Marshal(ctx.Config)
if err != nil {
return err
}
return ioutil.WriteFile(filename, out, 0644)
2017-04-27 11:03:26 +02:00
}