istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/cmd/sysexits.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"strings"
    19  
    20  	"istio.io/istio/istioctl/pkg/analyze"
    21  	"istio.io/istio/istioctl/pkg/util"
    22  )
    23  
    24  // Values should try to use sendmail-style values as in <sysexits.h>
    25  // See e.g. https://man.openbsd.org/sysexits.3
    26  // or `less /usr/includes/sysexits.h` if you're on Linux
    27  //
    28  // Picking the right range is tricky--there are a lot of reserved ones (see
    29  // https://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF) and then some
    30  // used by convention (see sysexits).
    31  //
    32  // The intention here is to use 64-78 in a way that matches the attempt in
    33  // sysexits to signify some error running istioctl, and use 79-125 as custom
    34  // error codes for other info that we'd like to use to pass info on.
    35  const (
    36  	ExitUnknownError   = 1 // for compatibility with existing exit code
    37  	ExitIncorrectUsage = 64
    38  	ExitDataError      = 65 // some format error with input data
    39  
    40  	// below here are non-zero exit codes that don't indicate an error with istioctl itself
    41  	ExitAnalyzerFoundIssues = 79 // istioctl analyze found issues, for CI/CD
    42  )
    43  
    44  func GetExitCode(e error) int {
    45  	if strings.Contains(e.Error(), "unknown command") {
    46  		e = util.CommandParseError{Err: e}
    47  	}
    48  
    49  	switch e.(type) {
    50  	case util.CommandParseError:
    51  		return ExitIncorrectUsage
    52  	case analyze.FileParseError:
    53  		return ExitDataError
    54  	case analyze.AnalyzerFoundIssuesError:
    55  		return ExitAnalyzerFoundIssues
    56  	default:
    57  		return ExitUnknownError
    58  	}
    59  }