1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-16 03:52:12 +02:00

Merge pull request #100 from goreleaser/fix-deploy

Fix deploy
This commit is contained in:
Carlos Alexandro Becker 2017-01-21 19:18:37 -02:00 committed by GitHub
commit 197ff0e3ca
3 changed files with 25 additions and 93 deletions

30
main.go
View File

@ -49,7 +49,7 @@ func main() {
Value: "goreleaser.yml",
},
}
app.Action = func(c *cli.Context) error {
app.Action = func(c *cli.Context) (err error) {
var file = c.String("config")
cfg, err := config.Load(file)
// Allow failing to load the config file if file is not explicitly specified
@ -58,30 +58,18 @@ func main() {
}
ctx := context.New(cfg)
log.SetFlags(0)
if err := runPipeline(ctx); err != nil {
return cli.NewExitError(err.Error(), 1)
for _, pipe := range pipes {
log.Println(pipe.Description())
log.SetPrefix(" -> ")
if err := pipe.Run(ctx); err != nil {
return cli.NewExitError(err.Error(), 1)
}
log.SetPrefix("")
}
log.Println("Done!")
return nil
return
}
if err := app.Run(os.Args); err != nil {
log.Fatalln(err)
}
}
func runPipeline(ctx *context.Context) error {
for _, pipe := range pipes {
log.Println(pipe.Description())
log.SetPrefix(" -> ")
err := pipe.Run(ctx)
log.SetPrefix("")
cleaner, ok := pipe.(pipeline.Cleaner)
if ok {
defer cleaner.Clean(ctx)
}
if err != nil {
return err
}
}
return nil
}

View File

@ -1,10 +0,0 @@
package pipeline
import "github.com/goreleaser/goreleaser/context"
// Cleaner is an interface that a pipe can implement
// to cleanup after all pipes ran.
type Cleaner interface {
// Clean is called after pipeline is done - even if a pipe returned an error.
Clean(*context.Context)
}

View File

@ -1,85 +1,39 @@
// Package source provides pipes to take care of using the correct source files.
// Package source provides pipes to take care of validating the current
// git repo state.
// For the releasing process we need the files of the tag we are releasing.
package source
import (
"bytes"
"errors"
"fmt"
"log"
"os/exec"
"github.com/goreleaser/goreleaser/context"
)
// Pipe to use the latest Git tag as source.
type Pipe struct {
dirty bool
wrongBranch bool
}
// ErrDirty happens when the repo has uncommitted/unstashed changes
var ErrDirty = errors.New("git is currently in a dirty state, commit or stash your changes to continue")
var ErrWrongRef = errors.New("current tag ref is different from HEAD ref, checkout the latest tag to continue")
// Pipe to make sure we are in the latest Git tag as source.
type Pipe struct{}
// Description of the pipe
func (p *Pipe) Description() string {
return "Using source from latest tag"
return "Validating current git state"
}
// Run uses the latest tag as source.
// Uncommitted changes are stashed.
// Run errors we the repo is dirty or if the current ref is different from the
// tag ref
func (p *Pipe) Run(ctx *context.Context) error {
cmd := exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
err := cmd.Run()
dirty := err != nil
if dirty {
log.Println("Stashing changes")
if err = run("git", "stash", "--include-untracked", "--quiet"); err != nil {
return fmt.Errorf("failed stashing changes: %v", err)
}
if err := cmd.Run(); err != nil {
return ErrDirty
}
p.dirty = dirty
cmd = exec.Command("git", "describe", "--exact-match", "--match", ctx.Git.CurrentTag)
err = cmd.Run()
wrongBranch := err != nil
if wrongBranch {
log.Println("Checking out tag", ctx.Git.CurrentTag)
if err = run("git", "checkout", ctx.Git.CurrentTag); err != nil {
return fmt.Errorf("failed changing branch: %v", err)
}
}
p.wrongBranch = wrongBranch
return nil
}
// Clean switches back to the original branch and restores changes.
func (p *Pipe) Clean(ctx *context.Context) {
if p.wrongBranch {
log.Println("Checking out original branch")
if err := run("git", "checkout", "-"); err != nil {
log.Println("failed changing branch: ", err.Error())
}
}
if p.dirty {
log.Println("Popping stashed changes")
if err := run("git", "stash", "pop"); err != nil {
log.Println("failed popping stashed changes:", err.Error())
}
}
}
func run(bin string, args ...string) error {
cmd := exec.Command(bin, args...)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
return errors.New(out.String())
if err := cmd.Run(); err != nil {
return ErrWrongRef
}
return nil
}