github.com/paketo-buildpacks/libpak@v1.70.0/internal/exit_handler.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 internal
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  
    24  	"github.com/paketo-buildpacks/libpak/bard"
    25  )
    26  
    27  const (
    28  	// ErrorStatusCode is the status code returned for error.
    29  	ErrorStatusCode = 1
    30  
    31  	// FailStatusCode is the status code returned for fail.
    32  	FailStatusCode = 100
    33  
    34  	// PassStatusCode is the status code returned for pass.
    35  	PassStatusCode = 0
    36  )
    37  
    38  // ExitHandler is an implementation of the libcnb.ExitHandler interface.
    39  type ExitHandler struct {
    40  	exitFunc func(int)
    41  	logger   bard.Logger
    42  	writer   io.Writer
    43  }
    44  
    45  // ExitHandlerOption is a function for configuring an ExitHandler instance.
    46  type ExitHandlerOption func(handler ExitHandler) ExitHandler
    47  
    48  // WithExitHandler creates an ExitHandlerOption that configures the exit function.
    49  func WithExitHandlerExitFunc(exitFunc func(int)) ExitHandlerOption {
    50  	return func(handler ExitHandler) ExitHandler {
    51  		handler.exitFunc = exitFunc
    52  		return handler
    53  	}
    54  }
    55  
    56  // WithExitHandlerLogger creates an ExitHandlerOption that configures the logger.
    57  func WithExitHandlerLogger(logger bard.Logger) ExitHandlerOption {
    58  	return func(handler ExitHandler) ExitHandler {
    59  		handler.logger = logger
    60  		return handler
    61  	}
    62  }
    63  
    64  // WithExitHandlerWriter creates an ExitHandlerOption that configures the writer.
    65  func WithExitHandlerWriter(writer io.Writer) ExitHandlerOption {
    66  	return func(handler ExitHandler) ExitHandler {
    67  		handler.writer = writer
    68  		return handler
    69  	}
    70  }
    71  
    72  // NewExitHandler creates a new instance that calls os.Exit and writes to the default bard.Logger and os.stderr.
    73  func NewExitHandler(options ...ExitHandlerOption) ExitHandler {
    74  	h := ExitHandler{
    75  		exitFunc: os.Exit,
    76  		logger:   bard.NewLogger(os.Stdout),
    77  		writer:   os.Stderr,
    78  	}
    79  
    80  	for _, option := range options {
    81  		h = option(h)
    82  	}
    83  
    84  	return h
    85  }
    86  
    87  func (e ExitHandler) Error(err error) {
    88  	if i, ok := err.(bard.IdentifiableError); ok {
    89  		e.logger.TerminalError(i)
    90  	} else {
    91  		_, _ = fmt.Fprintln(e.writer, err)
    92  	}
    93  
    94  	e.exitFunc(ErrorStatusCode)
    95  }
    96  
    97  func (e ExitHandler) Fail() {
    98  	e.exitFunc(FailStatusCode)
    99  }
   100  
   101  func (e ExitHandler) Pass() {
   102  	e.exitFunc(PassStatusCode)
   103  }