-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (60 loc) · 1.86 KB
/
main.go
File metadata and controls
67 lines (60 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
"go.datum.net/datumctl/internal/cmd"
customerrors "go.datum.net/datumctl/internal/errors"
"k8s.io/component-base/cli"
"k8s.io/component-base/logs"
kubectlcmd "k8s.io/kubectl/pkg/cmd"
"k8s.io/kubectl/pkg/cmd/util"
)
func main() {
logs.GlogSetter(kubectlcmd.GetLogVerbosity(os.Args))
rootCmd := cmd.RootCmd()
// Route kubectl's internal fatal errors (from util.CheckErr) through the
// same Format helper so --error-format applies uniformly. In human mode
// we print kubectl's preformatted string verbatim to match legacy output;
// in structured modes we strip the "error: " prefix kubectl may add and
// re-encode the message inside the JSON/YAML envelope.
util.BehaviorOnFatal(func(msg string, code int) {
msg = strings.TrimSuffix(msg, "\n")
format := formatFor(rootCmd)
if format == customerrors.FormatHuman {
fmt.Fprintln(os.Stderr, msg)
os.Exit(code)
}
clean := strings.TrimPrefix(msg, "error: ")
customerrors.Format(os.Stderr, errors.New(clean), format, verbosity())
os.Exit(code)
})
if err := cli.RunNoErrOutput(rootCmd); err != nil {
customerrors.Format(os.Stderr, err, formatFor(rootCmd), verbosity())
os.Exit(1)
}
}
// formatFor reads --error-format off the parsed root command, falling back to
// human when the flag is absent or unrecognized (e.g. when an error fires
// before flag parsing completes).
func formatFor(rootCmd *cobra.Command) string {
f := rootCmd.PersistentFlags().Lookup("error-format")
if f == nil {
return customerrors.FormatHuman
}
switch f.Value.String() {
case customerrors.FormatJSON:
return customerrors.FormatJSON
case customerrors.FormatYAML:
return customerrors.FormatYAML
default:
return customerrors.FormatHuman
}
}
func verbosity() int {
v, _ := strconv.Atoi(kubectlcmd.GetLogVerbosity(os.Args))
return v
}