github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/client/input_image_reference.go (about)

     1  package client
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  type InputImageReference interface {
    13  	Name() string
    14  	Layout() bool
    15  	FullName() (string, error)
    16  }
    17  
    18  type defaultInputImageReference struct {
    19  	name string
    20  }
    21  
    22  type layoutInputImageReference struct {
    23  	name string
    24  }
    25  
    26  func ParseInputImageReference(input string) InputImageReference {
    27  	if strings.HasPrefix(input, "oci:") {
    28  		imageNameParsed := strings.SplitN(input, ":", 2)
    29  		return &layoutInputImageReference{
    30  			name: imageNameParsed[1],
    31  		}
    32  	}
    33  	return &defaultInputImageReference{
    34  		name: input,
    35  	}
    36  }
    37  
    38  func (d *defaultInputImageReference) Name() string {
    39  	return d.name
    40  }
    41  
    42  func (d *defaultInputImageReference) Layout() bool {
    43  	return false
    44  }
    45  
    46  func (d *defaultInputImageReference) FullName() (string, error) {
    47  	return d.name, nil
    48  }
    49  
    50  func (l *layoutInputImageReference) Name() string {
    51  	return filepath.Base(l.name)
    52  }
    53  
    54  func (l *layoutInputImageReference) Layout() bool {
    55  	return true
    56  }
    57  
    58  func (l *layoutInputImageReference) FullName() (string, error) {
    59  	var (
    60  		fullImagePath string
    61  		err           error
    62  	)
    63  
    64  	path := parsePath(l.name)
    65  
    66  	if fullImagePath, err = filepath.EvalSymlinks(path); err != nil {
    67  		if !os.IsNotExist(err) {
    68  			return "", errors.Wrap(err, "evaluate symlink")
    69  		} else {
    70  			fullImagePath = path
    71  		}
    72  	}
    73  
    74  	if fullImagePath, err = filepath.Abs(fullImagePath); err != nil {
    75  		return "", errors.Wrap(err, "resolve absolute path")
    76  	}
    77  
    78  	return fullImagePath, nil
    79  }
    80  
    81  func parsePath(path string) string {
    82  	var result string
    83  	if filepath.IsAbs(path) && runtime.GOOS == "windows" {
    84  		dir, fileWithTag := filepath.Split(path)
    85  		file := removeTag(fileWithTag)
    86  		result = filepath.Join(dir, file)
    87  	} else {
    88  		result = removeTag(path)
    89  	}
    90  	return result
    91  }
    92  
    93  func removeTag(path string) string {
    94  	result := path
    95  	if strings.Contains(path, ":") {
    96  		split := strings.SplitN(path, ":", 2)
    97  		// do not include the tag in the path
    98  		result = split[0]
    99  	}
   100  	return result
   101  }