github.com/bitrise-io/go-steputils/v2@v2.0.0-alpha.30/cache/keytemplate/keytemplate.go (about)

     1  package keytemplate
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"runtime"
     7  	"text/template"
     8  
     9  	"github.com/bitrise-io/go-utils/v2/env"
    10  	"github.com/bitrise-io/go-utils/v2/log"
    11  )
    12  
    13  // Model ...
    14  type Model struct {
    15  	envRepo env.Repository
    16  	logger  log.Logger
    17  	os      string
    18  	arch    string
    19  }
    20  
    21  type templateInventory struct {
    22  	OS         string
    23  	Arch       string
    24  	Workflow   string
    25  	Branch     string
    26  	CommitHash string
    27  }
    28  
    29  // NewModel ...
    30  func NewModel(envRepo env.Repository, logger log.Logger) Model {
    31  	return Model{
    32  		envRepo: envRepo,
    33  		logger:  logger,
    34  		os:      runtime.GOOS,
    35  		arch:    runtime.GOARCH,
    36  	}
    37  }
    38  
    39  // Evaluate returns the final string from a key template
    40  func (m Model) Evaluate(key string) (string, error) {
    41  	funcMap := template.FuncMap{
    42  		"getenv":   m.getEnvVar,
    43  		"checksum": m.checksum,
    44  	}
    45  
    46  	tmpl, err := template.New("").Funcs(funcMap).Parse(key)
    47  	if err != nil {
    48  		return "", fmt.Errorf("invalid template: %w", err)
    49  	}
    50  
    51  	workflow := m.envRepo.Get("BITRISE_TRIGGERED_WORKFLOW_ID")
    52  	branch := m.envRepo.Get("BITRISE_GIT_BRANCH")
    53  	var commitHash = m.envRepo.Get("BITRISE_GIT_COMMIT")
    54  	if commitHash == "" {
    55  		commitHash = m.envRepo.Get("GIT_CLONE_COMMIT_HASH")
    56  		m.logger.Infof("Build trigger doesn't have an explicit git commit hash, using the Git Clone Step's output for the .CommitHash template variable (value: %s)", commitHash)
    57  	}
    58  
    59  	inventory := templateInventory{
    60  		OS:         m.os,
    61  		Arch:       m.arch,
    62  		Workflow:   workflow,
    63  		Branch:     branch,
    64  		CommitHash: commitHash,
    65  	}
    66  	m.validateInventory(inventory)
    67  
    68  	resultBuffer := bytes.Buffer{}
    69  	if err := tmpl.Execute(&resultBuffer, inventory); err != nil {
    70  		return "", err
    71  	}
    72  	return resultBuffer.String(), nil
    73  }
    74  
    75  func (m Model) getEnvVar(key string) string {
    76  	value := m.envRepo.Get(key)
    77  	if value == "" {
    78  		m.logger.Warnf("Environment variable %s is empty", key)
    79  	}
    80  	return value
    81  }
    82  
    83  func (m Model) validateInventory(inventory templateInventory) {
    84  	m.warnIfEmpty("Workflow", inventory.Workflow)
    85  	m.warnIfEmpty("Branch", inventory.Branch)
    86  	m.warnIfEmpty("CommitHash", inventory.CommitHash)
    87  }
    88  
    89  func (m Model) warnIfEmpty(name, value string) {
    90  	if value == "" {
    91  		m.logger.Debugf("Template variable .%s is not defined", name)
    92  	}
    93  }