Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (95 loc) · 3.28 KB
/
Copy pathmain.go
File metadata and controls
105 lines (95 loc) · 3.28 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/spf13/cobra"
"go.datum.net/datumctl/internal/cmd"
customerrors "go.datum.net/datumctl/internal/errors"
datumversion "go.datum.net/datumctl/internal/version"
"go.miloapis.com/service-catalog/pkg/activation"
"k8s.io/component-base/cli"
"k8s.io/component-base/logs"
kubectlcmd "k8s.io/kubectl/pkg/cmd"
"k8s.io/kubectl/pkg/cmd/util"
)
func main() {
datumversion.ApplyFallback()
logs.GlogSetter(kubectlcmd.GetLogVerbosity(os.Args))
cmd.RouteControllerRuntimeLogs()
rootCmd := cmd.RootCmd()
// Wire SIGINT/SIGTERM to a cancellable context so long interactive waits
// (e.g. the login callback/device-code polling) can be interrupted with
// ^C. cli.RunNoErrOutput calls cmd.Execute(), and Cobra honors a context
// set via SetContext, so the signal-derived context reaches cmd.Context().
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
rootCmd.SetContext(ctx)
// 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")
hint := cmd.FatalHint(msg)
format := formatFor(rootCmd)
if format == customerrors.FormatHuman {
fmt.Fprintln(os.Stderr, msg)
if hint != "" {
fmt.Fprintln(os.Stderr, hint)
}
os.Exit(code)
}
clean := strings.TrimPrefix(msg, "error: ")
customerrors.Format(os.Stderr, customerrors.NewUserErrorWithHint(clean, hint), format, verbosity())
os.Exit(code)
})
if err := cli.RunNoErrOutput(rootCmd); err != nil {
code, interrupted := exitCodeForError(err)
if interrupted {
// The user interrupted (^C / SIGTERM). Exit quietly instead of
// printing a spurious "context canceled" error.
fmt.Fprintln(os.Stderr, "\nAborted.")
} else {
customerrors.Format(os.Stderr, err, formatFor(rootCmd), verbosity())
}
os.Exit(code)
}
}
// exitCodeForError maps a command error to a process exit code. interrupted is
// true when the error was a user interrupt (^C / SIGTERM), which surfaces as a
// canceled context and should be reported quietly with the conventional
// 128+SIGINT exit code rather than as an error.
func exitCodeForError(err error) (code int, interrupted bool) {
if errors.Is(err, context.Canceled) {
return 130, true
}
return activation.ExitCodeOf(err), false
}
// 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
}