github.com/tomsquest/goreleaser@v0.34.3-0.20171008022654-7d6ef4d338b3/pipeline/checksums/checksums.go (about)

     1  // Package checksums provides a Pipe that creates .checksums files for
     2  // each artifact.
     3  package checksums
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/apex/log"
    11  	"github.com/goreleaser/goreleaser/checksum"
    12  	"github.com/goreleaser/goreleaser/context"
    13  	"github.com/goreleaser/goreleaser/internal/name"
    14  	"golang.org/x/sync/errgroup"
    15  )
    16  
    17  // Pipe for checksums
    18  type Pipe struct{}
    19  
    20  // Description of the pipe
    21  func (Pipe) Description() string {
    22  	return "Calculating checksums"
    23  }
    24  
    25  // Run the pipe
    26  func (Pipe) Run(ctx *context.Context) (err error) {
    27  	filename, err := name.ForChecksums(ctx)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	file, err := os.OpenFile(
    32  		filepath.Join(ctx.Config.Dist, filename),
    33  		os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
    34  		0644,
    35  	)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	defer func() {
    40  		_ = file.Close()
    41  		ctx.AddArtifact(file.Name())
    42  	}()
    43  	var g errgroup.Group
    44  	for _, artifact := range ctx.Artifacts {
    45  		artifact := artifact
    46  		g.Go(func() error {
    47  			return checksums(ctx, file, artifact)
    48  		})
    49  	}
    50  	return g.Wait()
    51  }
    52  
    53  func checksums(ctx *context.Context, file *os.File, name string) error {
    54  	log.WithField("file", name).Info("checksumming")
    55  	var artifact = filepath.Join(ctx.Config.Dist, name)
    56  	sha, err := checksum.SHA256(artifact)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	_, err = file.WriteString(fmt.Sprintf("%v  %v\n", sha, name))
    61  	return err
    62  }