github.com/paketo-buildpacks/libpak@v1.70.0/sherpa/sherpa.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 sherpa
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strconv"
    24  
    25  	"github.com/paketo-buildpacks/libpak/internal"
    26  )
    27  
    28  // ExecuteFunc is the callback function for buildpack helper application implementations.
    29  type ExecuteFunc func() error
    30  
    31  // Execute is called by the main function of a buildpack helper application, for execution.
    32  func Execute(f ExecuteFunc, options ...Option) {
    33  	config := Config{
    34  		exitHandler: internal.NewExitHandler(),
    35  	}
    36  
    37  	for _, option := range options {
    38  		config = option(config)
    39  	}
    40  
    41  	if err := f(); err != nil {
    42  		config.exitHandler.Error(err)
    43  		return
    44  	}
    45  }
    46  
    47  //go:generate mockery -name ExecD -case=underscore
    48  
    49  // ExecD describes an interface for types that Exec.d specification.
    50  type ExecD interface {
    51  	Execute() (map[string]string, error)
    52  }
    53  
    54  // Helpers is called by the main function of a buildpack's helper application, for execution.
    55  func Helpers(helpers map[string]ExecD, options ...Option) error {
    56  	config := Config{
    57  		arguments:   os.Args,
    58  		execdWriter: os.NewFile(3, "/dev/fd/3"),
    59  	}
    60  
    61  	for _, option := range options {
    62  		config = option(config)
    63  	}
    64  
    65  	if len(config.arguments) == 0 {
    66  		return fmt.Errorf("expected command name")
    67  	}
    68  
    69  	c := filepath.Base(config.arguments[0])
    70  	e, ok := helpers[c]
    71  	if !ok {
    72  		return fmt.Errorf("unsupported command %s", c)
    73  	}
    74  
    75  	r, err := e.Execute()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	// TODO: Enable once exec.d is implemented
    81  	// if err := toml.NewEncoder(config.execdWriter).Encode(r); err != nil {
    82  	// 	return fmt.Errorf("unable to write environment\n%w", err)
    83  	// }
    84  
    85  	// TODO: Remove once exec.d is implemented
    86  	for k, v := range r {
    87  		if _, err := fmt.Fprintf(config.execdWriter, "%s=%s\n", k, strconv.Quote(v)); err != nil {
    88  			return fmt.Errorf("unable to write environment\n%w", err)
    89  		}
    90  	}
    91  
    92  	return nil
    93  }