github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/dist/dist.go (about)

     1  // Package dist provides checks to make sure the dist folder is always
     2  // empty.
     3  package dist
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/apex/log"
    11  	"github.com/goreleaser/goreleaser/pkg/context"
    12  )
    13  
    14  // Pipe for cleandis
    15  type Pipe struct{}
    16  
    17  func (Pipe) String() string {
    18  	return "checking ./dist"
    19  }
    20  
    21  // Run the pipe
    22  func (Pipe) Run(ctx *context.Context) (err error) {
    23  	_, err = os.Stat(ctx.Config.Dist)
    24  	if os.IsNotExist(err) {
    25  		log.Debug("./dist doesn't exist, creating empty folder")
    26  		return mkdir(ctx)
    27  	}
    28  	if ctx.RmDist {
    29  		log.Info("--rm-dist is set, cleaning it up")
    30  		err = os.RemoveAll(ctx.Config.Dist)
    31  		if err == nil {
    32  			err = mkdir(ctx)
    33  		}
    34  		return err
    35  	}
    36  	files, err := ioutil.ReadDir(ctx.Config.Dist)
    37  	if err != nil {
    38  		return
    39  	}
    40  	if len(files) > 0 {
    41  		log.Debugf("there are %d files on ./dist", len(files))
    42  		return fmt.Errorf(
    43  			"%s is not empty, remove it before running goreleaser or use the --rm-dist flag",
    44  			ctx.Config.Dist,
    45  		)
    46  	}
    47  	log.Debug("./dist is empty")
    48  	return mkdir(ctx)
    49  }
    50  
    51  func mkdir(ctx *context.Context) error {
    52  	// #nosec
    53  	return os.MkdirAll(ctx.Config.Dist, 0755)
    54  }