2017-12-09 02:30:02 +02:00
|
|
|
// Package dist provides checks to make sure the dist folder is always
|
2017-07-05 03:53:50 +02:00
|
|
|
// empty.
|
2017-12-09 02:30:02 +02:00
|
|
|
package dist
|
2017-07-05 03:53:50 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-05 03:55:52 +02:00
|
|
|
"io/ioutil"
|
2017-07-05 03:53:50 +02:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2017-07-05 03:53:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pipe for cleandis
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 23:53:19 +02:00
|
|
|
func (Pipe) String() string {
|
|
|
|
return "checking ./dist"
|
2017-07-05 03:53:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the pipe
|
|
|
|
func (Pipe) Run(ctx *context.Context) (err error) {
|
|
|
|
_, err = os.Stat(ctx.Config.Dist)
|
|
|
|
if os.IsNotExist(err) {
|
2017-12-09 02:30:02 +02:00
|
|
|
log.Debug("./dist doesn't exist, creating empty folder")
|
|
|
|
return mkdir(ctx)
|
2017-07-05 03:53:50 +02:00
|
|
|
}
|
|
|
|
if ctx.RmDist {
|
2017-12-09 02:30:02 +02:00
|
|
|
log.Info("--rm-dist is set, cleaning it up")
|
|
|
|
err = os.RemoveAll(ctx.Config.Dist)
|
|
|
|
if err == nil {
|
|
|
|
err = mkdir(ctx)
|
|
|
|
}
|
|
|
|
return err
|
2017-07-05 03:53:50 +02:00
|
|
|
}
|
|
|
|
files, err := ioutil.ReadDir(ctx.Config.Dist)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-12-12 22:24:22 +02:00
|
|
|
if len(files) != 0 {
|
2018-01-21 16:31:21 +02:00
|
|
|
log.Debugf("there are %d files on ./dist", len(files))
|
2017-07-05 03:53:50 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"%s is not empty, remove it before running goreleaser or use the --rm-dist flag",
|
|
|
|
ctx.Config.Dist,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
log.Debug("./dist is empty")
|
2017-12-09 02:30:02 +02:00
|
|
|
return mkdir(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkdir(ctx *context.Context) error {
|
2017-12-10 15:45:59 +02:00
|
|
|
// #nosec
|
2017-12-10 15:36:39 +02:00
|
|
|
return os.MkdirAll(ctx.Config.Dist, 0755)
|
2017-07-05 03:53:50 +02:00
|
|
|
}
|