github.com/paketo-buildpacks/packit@v1.3.2-0.20211206231111-86b75c657449/layer.go (about)

     1  package packit
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/Masterminds/semver/v3"
     8  	"github.com/pelletier/go-toml"
     9  )
    10  
    11  // Layer provides a representation of a layer managed by a buildpack as
    12  // described by the specification:
    13  // https://github.com/buildpacks/spec/blob/main/buildpack.md#layers.
    14  type Layer struct {
    15  	// Path is the absolute location of the layer on disk.
    16  	Path string
    17  
    18  	// Name is the descriptive name of the layer.
    19  	Name string
    20  
    21  	// Build indicates whether the layer is available to subsequent buildpacks
    22  	// during their build phase according to the specification:
    23  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#build-layers.
    24  	Build bool
    25  
    26  	// Launch indicates whether the layer is exported into the application image
    27  	// and made available during the launch phase according to the specification:
    28  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#launch-layers.
    29  	Launch bool
    30  
    31  	// Cache indicates whether the layer is persisted and made available to
    32  	// subsequent builds of the same application according to the specification:
    33  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#launch-layers
    34  	// and
    35  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#build-layers.
    36  	Cache bool
    37  
    38  	// SharedEnv is the set of environment variables attached to the layer and
    39  	// made available during both the build and launch phases according to the
    40  	// specification:
    41  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
    42  	SharedEnv Environment
    43  
    44  	// BuildEnv is the set of environment variables attached to the layer and
    45  	// made available during the build phase according to the specification:
    46  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
    47  	BuildEnv Environment
    48  
    49  	// LaunchEnv is the set of environment variables attached to the layer and
    50  	// made available during the launch phase according to the specification:
    51  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
    52  	LaunchEnv Environment
    53  
    54  	// ProcessLaunchEnv is a map of environment variables attached to the layer and
    55  	// made available to specified proccesses in the launch phase accoring to the specification:
    56  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks
    57  	ProcessLaunchEnv map[string]Environment
    58  
    59  	// Metadata is an unspecified field allowing buildpacks to communicate extra
    60  	// details about the layer. Examples of this type of metadata might include
    61  	// details about what versions of software are included in the layer such
    62  	// that subsequent builds can inspect that metadata and choose to reuse the
    63  	// layer if suitable. The Metadata field ultimately fills the metadata field
    64  	// of the Layer Content Metadata TOML file according to the specification:
    65  	// https://github.com/buildpacks/spec/blob/main/buildpack.md#layer-content-metadata-toml.
    66  	Metadata map[string]interface{}
    67  
    68  	// SBOM is a type that implements SBOMFormatter and declares the formats that
    69  	// bill-of-materials should be output for the layer SBoM.
    70  	SBOM SBOMFormatter
    71  }
    72  
    73  // Reset clears the state of a layer such that the layer can be replaced with
    74  // new content and metadata. It clears all environment variables, and removes
    75  // the content of the layer directory on disk.
    76  func (l Layer) Reset() (Layer, error) {
    77  	l.Build = false
    78  	l.Launch = false
    79  	l.Cache = false
    80  
    81  	l.SharedEnv = Environment{}
    82  	l.BuildEnv = Environment{}
    83  	l.LaunchEnv = Environment{}
    84  	l.ProcessLaunchEnv = make(map[string]Environment)
    85  	l.Metadata = nil
    86  
    87  	err := os.RemoveAll(l.Path)
    88  	if err != nil {
    89  		return Layer{}, fmt.Errorf("error could not remove file: %s", err)
    90  	}
    91  
    92  	err = os.MkdirAll(l.Path, os.ModePerm)
    93  	if err != nil {
    94  		return Layer{}, fmt.Errorf("error could not create directory: %s", err)
    95  	}
    96  
    97  	return l, nil
    98  }
    99  
   100  type formattedLayer struct {
   101  	layer Layer
   102  	api   *semver.Version
   103  }
   104  
   105  func (l formattedLayer) MarshalTOML() ([]byte, error) {
   106  	layer := map[string]interface{}{
   107  		"metadata": l.layer.Metadata,
   108  	}
   109  
   110  	apiV06, _ := semver.NewVersion("0.6")
   111  	if l.api.LessThan(apiV06) {
   112  		layer["build"] = l.layer.Build
   113  		layer["launch"] = l.layer.Launch
   114  		layer["cache"] = l.layer.Cache
   115  	} else {
   116  		layer["types"] = map[string]bool{
   117  			"build":  l.layer.Build,
   118  			"launch": l.layer.Launch,
   119  			"cache":  l.layer.Cache,
   120  		}
   121  	}
   122  
   123  	return toml.Marshal(layer)
   124  }