github.com/triarius/goreleaser@v1.12.5/internal/pipe/metadata/metadata.go (about)

     1  // Package metadata provides the pipe implementation that creates a artifacts.json file in the dist folder.
     2  package metadata
     3  
     4  import (
     5  	"encoding/json"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	"github.com/caarlos0/log"
    11  	"github.com/triarius/goreleaser/internal/artifact"
    12  	"github.com/triarius/goreleaser/pkg/context"
    13  )
    14  
    15  // Pipe implementation.
    16  type Pipe struct{}
    17  
    18  func (Pipe) String() string                 { return "storing release metadata" }
    19  func (Pipe) Skip(ctx *context.Context) bool { return false }
    20  
    21  // Run the pipe.
    22  func (Pipe) Run(ctx *context.Context) error {
    23  	if err := writeArtifacts(ctx); err != nil {
    24  		return err
    25  	}
    26  	return writeMetadata(ctx)
    27  }
    28  
    29  func writeMetadata(ctx *context.Context) error {
    30  	return writeJSON(ctx, metadata{
    31  		ProjectName: ctx.Config.ProjectName,
    32  		Tag:         ctx.Git.CurrentTag,
    33  		PreviousTag: ctx.Git.PreviousTag,
    34  		Version:     ctx.Version,
    35  		Commit:      ctx.Git.Commit,
    36  		Date:        ctx.Date,
    37  		Runtime: metaRuntime{
    38  			Goos:   ctx.Runtime.Goos,
    39  			Goarch: ctx.Runtime.Goarch,
    40  		},
    41  	}, "metadata.json")
    42  }
    43  
    44  func writeArtifacts(ctx *context.Context) error {
    45  	_ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error {
    46  		a.TypeS = a.Type.String()
    47  		return nil
    48  	})
    49  	return writeJSON(ctx, ctx.Artifacts.List(), "artifacts.json")
    50  }
    51  
    52  func writeJSON(ctx *context.Context, j interface{}, name string) error {
    53  	bts, err := json.Marshal(j)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	path := filepath.Join(ctx.Config.Dist, name)
    58  	log.Log.WithField("file", path).Info("writing")
    59  	return os.WriteFile(path, bts, 0o644)
    60  }
    61  
    62  type metadata struct {
    63  	ProjectName string      `json:"project_name"`
    64  	Tag         string      `json:"tag"`
    65  	PreviousTag string      `json:"previous_tag"`
    66  	Version     string      `json:"version"`
    67  	Commit      string      `json:"commit"`
    68  	Date        time.Time   `json:"date"`
    69  	Runtime     metaRuntime `json:"runtime"`
    70  }
    71  
    72  type metaRuntime struct {
    73  	Goos   string `json:"goos"`
    74  	Goarch string `json:"goarch"`
    75  }