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

     1  package packit
     2  
     3  import "io"
     4  
     5  // OptionConfig is the set of configurable options for the Build and Detect
     6  // functions.
     7  type OptionConfig struct {
     8  	exitHandler ExitHandler
     9  	args        []string
    10  	tomlWriter  TOMLWriter
    11  	envWriter   EnvironmentWriter
    12  	fileWriter  FileWriter
    13  }
    14  
    15  // Option declares a function signature that can be used to define optional
    16  // modifications to the behavior of the Detect and Build functions.
    17  type Option func(config OptionConfig) OptionConfig
    18  
    19  //go:generate faux --interface ExitHandler --output fakes/exit_handler.go
    20  
    21  // ExitHandler serves as the interface for types that can handle an error
    22  // during the Detect or Build functions. ExitHandlers are responsible for
    23  // translating error values into exit codes according the specification:
    24  // https://github.com/buildpacks/spec/blob/main/buildpack.md#detection and
    25  // https://github.com/buildpacks/spec/blob/main/buildpack.md#build.
    26  type ExitHandler interface {
    27  	Error(error)
    28  }
    29  
    30  // TOMLWriter serves as the interface for types that can handle the writing of
    31  // TOML files. TOMLWriters take a path to a file location on disk and a
    32  // datastructure to marshal.
    33  type TOMLWriter interface {
    34  	Write(path string, value interface{}) error
    35  }
    36  
    37  // EnvironmentWriter serves as the interface for types that can write an
    38  // Environment to a directory on disk according to the specification:
    39  // https://github.com/buildpacks/spec/blob/main/buildpack.md#provided-by-the-buildpacks.
    40  type EnvironmentWriter interface {
    41  	Write(dir string, env map[string]string) error
    42  }
    43  
    44  type FileWriter interface {
    45  	Write(path string, reader io.Reader) error
    46  }
    47  
    48  // WithExitHandler is an Option that overrides the ExitHandler for a given
    49  // invocation of Build or Detect.
    50  func WithExitHandler(exitHandler ExitHandler) Option {
    51  	return func(config OptionConfig) OptionConfig {
    52  		config.exitHandler = exitHandler
    53  		return config
    54  	}
    55  }
    56  
    57  // WithArgs is an Option that overrides the value of os.Args for a given
    58  // invocation of Build or Detect.
    59  func WithArgs(args []string) Option {
    60  	return func(config OptionConfig) OptionConfig {
    61  		config.args = args
    62  		return config
    63  	}
    64  }