gitee.com/h79/goutils@v1.22.10/build/context.go (about)

     1  package build
     2  
     3  import (
     4  	stdctx "context"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  type Config struct {
    12  	ProductId   string
    13  	ProjectName string
    14  	Version     string
    15  	Model       string
    16  	Os          string
    17  	Env         []string
    18  }
    19  
    20  // Git includes tags and diffs used in some point.
    21  type Git struct {
    22  	Branch      string
    23  	CurrentTag  string
    24  	PreviousTag string
    25  	Commit      string
    26  	ShortCommit string
    27  	FullCommit  string
    28  	FirstCommit string
    29  	URL         string
    30  	Summary     string
    31  	TagSubject  string
    32  	TagContents string
    33  	TagBody     string
    34  	CommitDate  time.Time
    35  	Dirty       bool
    36  }
    37  
    38  // Env is the environment variables.
    39  type Env map[string]string
    40  
    41  // Copy returns a copy of the environment.
    42  func (e Env) Copy() Env {
    43  	out := Env{}
    44  	for k, v := range e {
    45  		out[k] = v
    46  	}
    47  	return out
    48  }
    49  
    50  // Strings returns the current environment as a list of strings, suitable for
    51  // os executions.
    52  func (e Env) Strings() []string {
    53  	result := make([]string, 0, len(e))
    54  	for k, v := range e {
    55  		result = append(result, k+"="+v)
    56  	}
    57  	return result
    58  }
    59  
    60  type DateFormatFunc func(d time.Time) string
    61  
    62  func DefaultDateFormat(d time.Time) string {
    63  	return d.UTC().Format(time.RFC3339)
    64  }
    65  
    66  // Context carries along some data through the pipes.
    67  type Context struct {
    68  	stdctx.Context
    69  	Config     Config
    70  	Env        Env
    71  	Git        Git
    72  	Semver     Semver
    73  	Runtime    Runtime
    74  	DateFormat DateFormatFunc
    75  	Date       time.Time
    76  	Snapshot   bool
    77  }
    78  
    79  type Runtime struct {
    80  	Goos   string
    81  	Goarch string
    82  }
    83  
    84  // Semver represents a semantic version.
    85  type Semver struct {
    86  	Major      uint64
    87  	Minor      uint64
    88  	Patch      uint64
    89  	Prerelease string
    90  }
    91  
    92  // NewContext context.
    93  func NewContext(config Config) *Context {
    94  	return WrapContext(stdctx.Background(), config)
    95  }
    96  
    97  // NewWithTimeout new context with the given timeout.
    98  func NewWithTimeout(config Config, timeout time.Duration) (*Context, stdctx.CancelFunc) {
    99  	ctx, cancel := stdctx.WithTimeout(stdctx.Background(), timeout) // nosem
   100  	return WrapContext(ctx, config), cancel
   101  }
   102  
   103  // WrapContext wraps an existing context.
   104  func WrapContext(ctx stdctx.Context, config Config) *Context {
   105  	return &Context{
   106  		Context:    ctx,
   107  		Config:     config,
   108  		Env:        ToEnv(append(os.Environ(), config.Env...)),
   109  		Date:       time.Now(),
   110  		DateFormat: DefaultDateFormat,
   111  		Runtime: Runtime{
   112  			Goos:   runtime.GOOS,
   113  			Goarch: runtime.GOARCH,
   114  		},
   115  	}
   116  }
   117  
   118  // ToEnv converts a list of strings to an Env (aka a map[string]string).
   119  func ToEnv(env []string) Env {
   120  	r := Env{}
   121  	for _, e := range env {
   122  		k, v, ok := strings.Cut(e, "=")
   123  		if !ok || k == "" {
   124  			continue
   125  		}
   126  		r[k] = v
   127  	}
   128  	return r
   129  }