2024-04-01 10:01:56 -03:00
|
|
|
// Package dist provides checks to make sure the dist directory is always
|
2017-07-04 22:53:50 -03:00
|
|
|
// empty.
|
2017-12-08 22:30:02 -02:00
|
|
|
package dist
|
2017-07-04 22:53:50 -03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2018-08-14 23:50:20 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2017-07-04 22:53:50 -03:00
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe for dist.
|
2017-07-04 22:53:50 -03:00
|
|
|
type Pipe struct{}
|
|
|
|
|
2017-12-02 19:53:19 -02:00
|
|
|
func (Pipe) String() string {
|
2021-11-23 13:25:15 +01:00
|
|
|
return "checking distribution directory"
|
2017-07-04 22:53:50 -03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -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) {
|
2024-04-01 10:01:56 -03:00
|
|
|
log.Debugf("%s doesn't exist, creating empty directory", ctx.Config.Dist)
|
2017-12-08 22:30:02 -02:00
|
|
|
return mkdir(ctx)
|
2017-07-04 22:53:50 -03:00
|
|
|
}
|
2023-01-29 00:24:11 -03:00
|
|
|
if ctx.Clean {
|
2023-01-20 23:47:08 -03:00
|
|
|
log.Infof("cleaning %s", ctx.Config.Dist)
|
2017-12-08 22:30:02 -02:00
|
|
|
err = os.RemoveAll(ctx.Config.Dist)
|
|
|
|
if err == nil {
|
|
|
|
err = mkdir(ctx)
|
|
|
|
}
|
|
|
|
return err
|
2017-07-04 22:53:50 -03:00
|
|
|
}
|
2021-04-25 13:00:51 -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 {
|
2021-11-23 13:25:15 +01:00
|
|
|
log.Debugf("there are %d files on %s", len(files), ctx.Config.Dist)
|
2017-07-04 22:53:50 -03:00
|
|
|
return fmt.Errorf(
|
2023-01-20 23:47:08 -03:00
|
|
|
"%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,
|
|
|
|
)
|
|
|
|
}
|
2021-11-23 13:25:15 +01:00
|
|
|
log.Debugf("%s is empty", ctx.Config.Dist)
|
2017-12-08 22:30:02 -02:00
|
|
|
return mkdir(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkdir(ctx *context.Context) error {
|
2017-12-10 11:45:59 -02:00
|
|
|
// #nosec
|
2021-04-25 14:20:49 -03:00
|
|
|
return os.MkdirAll(ctx.Config.Dist, 0o755)
|
2017-07-04 22:53:50 -03:00
|
|
|
}
|