github.com/devster/tarreleaser@v0.0.0-20221207180803-c608f4eb8918/pkg/context/context.go (about)

     1  package context
     2  
     3  import (
     4  	ctx "context"
     5  	"github.com/devster/tarreleaser/pkg/config"
     6  	"os"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  type GitInfoCommit struct {
    12  	Message string
    13  	Author  string
    14  }
    15  
    16  type GitInfo struct {
    17  	Branch      string
    18  	CurrentTag  string
    19  	Commit      GitInfoCommit
    20  	ShortCommit string
    21  	FullCommit  string
    22  }
    23  
    24  type Archive struct {
    25  	Path string
    26  	Name string
    27  }
    28  
    29  type Context struct {
    30  	ctx.Context
    31  	Config       config.Project
    32  	Env          map[string]string
    33  	Git          GitInfo
    34  	Date         time.Time
    35  	Archive      Archive
    36  	SkipPublish  bool
    37  	OutputFormat string
    38  }
    39  
    40  func NewWithTimeout(cfg config.Project, timeout time.Duration) (*Context, ctx.CancelFunc) {
    41  	ctx, cancel := ctx.WithTimeout(ctx.Background(), timeout)
    42  	return &Context{
    43  		Context: ctx,
    44  		Config:  cfg,
    45  		Env:     splitEnv(os.Environ()),
    46  		Date:    time.Now().UTC(),
    47  	}, cancel
    48  }
    49  
    50  func splitEnv(env []string) map[string]string {
    51  	r := map[string]string{}
    52  	for _, e := range env {
    53  		p := strings.SplitN(e, "=", 2)
    54  		r[p[0]] = p[1]
    55  	}
    56  	return r
    57  }