istio
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
15package cmd16
17import (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.
35const (36ExitUnknownError = 1 // for compatibility with existing exit code37ExitIncorrectUsage = 6438ExitDataError = 65 // some format error with input data39
40// below here are non-zero exit codes that don't indicate an error with istioctl itself41ExitAnalyzerFoundIssues = 79 // istioctl analyze found issues, for CI/CD42)
43
44func GetExitCode(e error) int {45if strings.Contains(e.Error(), "unknown command") {46e = util.CommandParseError{Err: e}47}48
49switch e.(type) {50case util.CommandParseError:51return ExitIncorrectUsage52case analyze.FileParseError:53return ExitDataError54case analyze.AnalyzerFoundIssuesError:55return ExitAnalyzerFoundIssues56default:57return ExitUnknownError58}59}
60