github.com/paketo-buildpacks/libpak/v2@v2.0.0-alpha.3.0.20231023030503-8365f81de65a/detect.go (about)

     1  /*
     2   * Copyright 2018-2023 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 libpak
    18  
    19  import (
    20  	"github.com/buildpacks/libcnb/v2"
    21  
    22  	"github.com/paketo-buildpacks/libpak/v2/internal"
    23  	"github.com/paketo-buildpacks/libpak/v2/log"
    24  )
    25  
    26  // Detect is called by the main function of a buildpack, for detection.
    27  func Detect(detector libcnb.DetectFunc, options ...libcnb.Option) {
    28  	libcnb.Detect(detectDelegate{delegate: detector}.Detect,
    29  		libcnb.NewConfig(append([]libcnb.Option{
    30  			libcnb.WithExitHandler(internal.NewExitHandler()),
    31  			libcnb.WithTOMLWriter(internal.NewTOMLWriter()),
    32  		}, options...)...))
    33  }
    34  
    35  type detectDelegate struct {
    36  	delegate libcnb.DetectFunc
    37  }
    38  
    39  func (d detectDelegate) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error) {
    40  	result, err := d.delegate(context)
    41  	if err != nil {
    42  		err = log.IdentifiableError{
    43  			Name:        context.Buildpack.Info.Name,
    44  			Description: context.Buildpack.Info.Version,
    45  			Err:         err,
    46  		}
    47  	}
    48  
    49  	return result, err
    50  }