-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (113 loc) · 3.62 KB
/
Copy pathmain.go
File metadata and controls
132 lines (113 loc) · 3.62 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2023,2026 The kpt and Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"github.com/kptdev/kpt/pkg/lib/errors"
"github.com/kptdev/kpt/pkg/lib/errors/resolver"
"github.com/kptdev/kpt/pkg/lib/util/cmdutil"
"github.com/nephio-project/porch/cmd/porchctl/run"
"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/component-base/logs"
"k8s.io/klog/v2"
k8scmdutil "k8s.io/kubectl/pkg/cmd/util"
)
func main() {
// Handle all setup in the runMain function so os.Exit doesn't interfere
// with defer.
os.Exit(runMain())
}
// runMain does the initial setup in order to run kpt. The return value from
// this function will be the exit code when kpt terminates.
func runMain() int {
var err error
ctx := context.Background()
// Enable commandline flags for klog.
// logging will help in collecting debugging information from users
klog.InitFlags(nil)
cmd := run.GetMain(ctx)
err = k8sCliRunNoErrOutput(cmd)
if err != nil {
return handleErr(cmd, err)
}
return 0
}
// k8sCliRunNoErrOutput is a copy of k8s.io/component-base/cli.RunNoErrOutput
// that does not mess up the global normalization func. This means that we lose
// the WordSepNormalize functionality in porchctl.
//
// TODO: is that a problem?
func k8sCliRunNoErrOutput(cmd *cobra.Command) error {
defer logs.FlushLogs()
// cmd.SetGlobalNormalizationFunc(cliflag.WordSepNormalizeFunc)
if !cmd.SilenceUsage {
cmd.SilenceUsage = true
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
c.SilenceUsage = false
return err
})
}
cmd.SilenceErrors = true
logs.AddFlags(cmd.PersistentFlags())
switch {
case cmd.PersistentPreRun != nil:
pre := cmd.PersistentPreRun
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
logs.InitLogs()
pre(cmd, args)
}
case cmd.PersistentPreRunE != nil:
pre := cmd.PersistentPreRunE
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
logs.InitLogs()
return pre(cmd, args)
}
default:
cmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
logs.InitLogs()
}
}
return cmd.Execute()
}
// handleErr takes care of printing an error message for a given error.
func handleErr(cmd *cobra.Command, err error) int {
// First attempt to see if we can resolve the error into a specific
// error message.
if re, resolved := resolver.ResolveError(err); resolved {
if re.Message != "" {
fmt.Fprintf(cmd.ErrOrStderr(), "%s \n", re.Message)
}
return re.ExitCode
}
// Then try to see if it is of type *errors.Error
var kptErr *errors.Error
if errors.As(err, &kptErr) {
unwrapped, ok := errors.UnwrapErrors(kptErr)
if ok && !cmdutil.PrintErrorStacktrace() {
fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s \n", unwrapped.Error())
return 1
}
fmt.Fprintf(cmd.ErrOrStderr(), "%s \n", kptErr.Error())
return 1
}
// Finally just let the error handler for kubectl handle it. This handles
// printing of several error types used in kubectl
// TODO: See if we can handle this in kpt and get a uniform experience
// across all of kpt.
k8scmdutil.CheckErr(err)
return 1
}