github.com/replicatedhq/ship@v0.55.0/pkg/ship/exit.go (about)

     1  package ship
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"time"
     8  
     9  	"github.com/go-kit/kit/log/level"
    10  	"github.com/pkg/errors"
    11  	"github.com/replicatedhq/ship/pkg/constants"
    12  	"github.com/replicatedhq/ship/pkg/util/warnings"
    13  )
    14  
    15  // ExitWithError can be called if something goes wrong to print some friendly output
    16  func (s *Ship) ExitWithError(err error) {
    17  	if warnings.IsWarning(err) {
    18  		s.ExitWithWarn(err)
    19  		return
    20  	}
    21  
    22  	if s.Viper.GetString("log-level") == "debug" {
    23  		s.UI.Error(fmt.Sprintf("There was an unexpected error! %+v", err))
    24  	} else {
    25  		s.UI.Error(fmt.Sprintf("There was an unexpected error! %v", err))
    26  	}
    27  	level.Warn(s.Logger).Log("event", "exit.withErr", "errorWithStack", fmt.Sprintf("%+v", err))
    28  	s.UI.Output("")
    29  	time.Sleep(100 * time.Millisecond) // hack, need to wait for flush output from above
    30  	s.preserveDebugLogsOrRequestReRun()
    31  
    32  	if !s.Viper.GetBool("no-os-exit") {
    33  		os.Exit(1)
    34  	}
    35  }
    36  
    37  // ExitWithWarn can be called if something goes wrong to print some friendly output
    38  func (s *Ship) ExitWithWarn(err error) {
    39  	s.UI.Warn(fmt.Sprintf("%v", errors.Cause(err)))
    40  	os.Exit(1)
    41  }
    42  
    43  func (s *Ship) preserveDebugLogsOrRequestReRun() {
    44  	debugLogFile := path.Join(constants.ShipPathInternalLog)
    45  	// make sure it exists
    46  	if exists, err := s.FS.Exists(debugLogFile); err != nil || !exists {
    47  		s.UI.Info(
    48  			"There was an error configuring the application. " +
    49  				"Please re-run with --log-level=debug and include " +
    50  				"the output in any support inquiries.",
    51  		)
    52  	} else {
    53  		s.UI.Info(fmt.Sprintf(
    54  			"There was an error configuring the application. "+
    55  				"A debug log has been written to %q, please include it "+
    56  				"in any support inquiries.",
    57  			debugLogFile),
    58  		)
    59  	}
    60  }