github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/carton/config.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package carton
    18  
    19  import (
    20  	"github.com/buildpacks/libcnb"
    21  
    22  	"github.com/BarDweller/libpak/effect"
    23  )
    24  
    25  //go:generate mockery -name EntryWriter -case=underscore
    26  
    27  // EntryWriter is the interface implemented by a type that wants to write an entry.
    28  type EntryWriter interface {
    29  
    30  	// Write reads a file from source and writes it to the relative path destination.
    31  	Write(source string, destination string) error
    32  }
    33  
    34  // Config is an object that contains configurable properties for execution.
    35  type Config struct {
    36  	entryWriter EntryWriter
    37  	executor    effect.Executor
    38  	exitHandler libcnb.ExitHandler
    39  }
    40  
    41  // Option is a function for configuring a Config instance.
    42  type Option func(config Config) Config
    43  
    44  // WithEntryWriter creates an Option that sets an EntryWriter implementation.
    45  func WithEntryWriter(entryWriter EntryWriter) Option {
    46  	return func(config Config) Config {
    47  		config.entryWriter = entryWriter
    48  		return config
    49  	}
    50  }
    51  
    52  // WithExecutor creates an Option that sets an Executor implementation.
    53  func WithExecutor(executor effect.Executor) Option {
    54  	return func(config Config) Config {
    55  		config.executor = executor
    56  		return config
    57  	}
    58  }
    59  
    60  // WithExitHandler creates an Option that sets an ExitHandler implementation.
    61  func WithExitHandler(exitHandler libcnb.ExitHandler) Option {
    62  	return func(config Config) Config {
    63  		config.exitHandler = exitHandler
    64  		return config
    65  	}
    66  }