github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/acceptance/config/input_configuration_manager.go (about)

     1  //go:build acceptance
     2  // +build acceptance
     3  
     4  package config
     5  
     6  import (
     7  	"encoding/json"
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  const (
    17  	envAcceptanceSuiteConfig    = "ACCEPTANCE_SUITE_CONFIG"
    18  	envCompilePackWithVersion   = "COMPILE_PACK_WITH_VERSION"
    19  	envGitHubToken              = "GITHUB_TOKEN"
    20  	envLifecycleImage           = "LIFECYCLE_IMAGE"
    21  	envLifecyclePath            = "LIFECYCLE_PATH"
    22  	envPackPath                 = "PACK_PATH"
    23  	envPreviousLifecycleImage   = "PREVIOUS_LIFECYCLE_IMAGE"
    24  	envPreviousLifecyclePath    = "PREVIOUS_LIFECYCLE_PATH"
    25  	envPreviousPackFixturesPath = "PREVIOUS_PACK_FIXTURES_PATH"
    26  	envPreviousPackPath         = "PREVIOUS_PACK_PATH"
    27  )
    28  
    29  type InputConfigurationManager struct {
    30  	packPath                 string
    31  	previousPackPath         string
    32  	previousPackFixturesPath string
    33  	lifecyclePath            string
    34  	previousLifecyclePath    string
    35  	lifecycleImage           string
    36  	previousLifecycleImage   string
    37  	compilePackWithVersion   string
    38  	githubToken              string
    39  	combinations             ComboSet
    40  }
    41  
    42  func NewInputConfigurationManager() (InputConfigurationManager, error) {
    43  	packPath := os.Getenv(envPackPath)
    44  	previousPackPath := os.Getenv(envPreviousPackPath)
    45  	previousPackFixturesPath := os.Getenv(envPreviousPackFixturesPath)
    46  	lifecyclePath := os.Getenv(envLifecyclePath)
    47  	previousLifecyclePath := os.Getenv(envPreviousLifecyclePath)
    48  
    49  	err := resolveAbsolutePaths(&packPath, &previousPackPath, &previousPackFixturesPath, &lifecyclePath, &previousLifecyclePath)
    50  	if err != nil {
    51  		return InputConfigurationManager{}, err
    52  	}
    53  
    54  	var combos ComboSet
    55  
    56  	comboConfig := os.Getenv(envAcceptanceSuiteConfig)
    57  	if comboConfig != "" {
    58  		if err := json.Unmarshal([]byte(comboConfig), &combos); err != nil {
    59  			return InputConfigurationManager{}, errors.Errorf("failed to parse combination config: %s", err)
    60  		}
    61  	} else {
    62  		combos = defaultRunCombo
    63  	}
    64  
    65  	if lifecyclePath != "" && len(combos) == 1 && combos[0] == defaultRunCombo[0] {
    66  		combos[0].Lifecycle = Current
    67  	}
    68  
    69  	return InputConfigurationManager{
    70  		packPath:                 packPath,
    71  		previousPackPath:         previousPackPath,
    72  		previousPackFixturesPath: previousPackFixturesPath,
    73  		lifecyclePath:            lifecyclePath,
    74  		previousLifecyclePath:    previousLifecyclePath,
    75  		lifecycleImage:           os.Getenv(envLifecycleImage),
    76  		previousLifecycleImage:   os.Getenv(envPreviousLifecycleImage),
    77  		compilePackWithVersion:   os.Getenv(envCompilePackWithVersion),
    78  		githubToken:              os.Getenv(envGitHubToken),
    79  		combinations:             combos,
    80  	}, nil
    81  }
    82  
    83  func (i InputConfigurationManager) Combinations() ComboSet {
    84  	return i.combinations
    85  }
    86  
    87  func resolveAbsolutePaths(paths ...*string) error {
    88  	for _, path := range paths {
    89  		if *path == "" {
    90  			continue
    91  		}
    92  
    93  		// Manually expand ~ to home dir
    94  		if strings.HasPrefix(*path, "~/") {
    95  			usr, err := user.Current()
    96  			if err != nil {
    97  				return errors.Wrapf(err, "getting current user")
    98  			}
    99  			dir := usr.HomeDir
   100  			*path = filepath.Join(dir, (*path)[2:])
   101  		} else {
   102  			absPath, err := filepath.Abs(*path)
   103  			if err != nil {
   104  				return errors.Wrapf(err, "getting absolute path for %s", *path)
   105  			}
   106  			*path = absPath
   107  		}
   108  	}
   109  
   110  	return nil
   111  }