1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-05 13:15:26 +02:00

54 lines
1.1 KiB
Go
Raw Normal View History

// Package dist provides checks to make sure the dist directory is always
2017-07-04 22:53:50 -03:00
// empty.
package dist
2017-07-04 22:53:50 -03:00
import (
"fmt"
"os"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/pkg/context"
2017-07-04 22:53:50 -03:00
)
// Pipe for dist.
2017-07-04 22:53:50 -03:00
type Pipe struct{}
func (Pipe) String() string {
return "checking distribution directory"
2017-07-04 22:53:50 -03:00
}
// Run the pipe.
2017-07-04 22:53:50 -03:00
func (Pipe) Run(ctx *context.Context) (err error) {
_, err = os.Stat(ctx.Config.Dist)
if os.IsNotExist(err) {
log.Debugf("%s doesn't exist, creating empty directory", ctx.Config.Dist)
return mkdir(ctx)
2017-07-04 22:53:50 -03:00
}
if ctx.Clean {
log.Infof("cleaning %s", ctx.Config.Dist)
err = os.RemoveAll(ctx.Config.Dist)
if err == nil {
err = mkdir(ctx)
}
return err
2017-07-04 22:53:50 -03:00
}
files, err := os.ReadDir(ctx.Config.Dist)
2017-07-04 22:53:50 -03:00
if err != nil {
return
}
2018-12-12 18:24:22 -02:00
if len(files) != 0 {
log.Debugf("there are %d files on %s", len(files), ctx.Config.Dist)
2017-07-04 22:53:50 -03:00
return fmt.Errorf(
"%s is not empty, remove it before running goreleaser or use the --clean flag",
2017-07-04 22:53:50 -03:00
ctx.Config.Dist,
)
}
log.Debugf("%s is empty", ctx.Config.Dist)
return mkdir(ctx)
}
func mkdir(ctx *context.Context) error {
// #nosec
return os.MkdirAll(ctx.Config.Dist, 0o755)
2017-07-04 22:53:50 -03:00
}