-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
51 lines (42 loc) · 1.24 KB
/
main.go
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
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/fatih/color"
"github.com/leg100/etok/cmd"
cmdutil "github.com/leg100/etok/cmd/util"
etokerrors "github.com/leg100/etok/pkg/errors"
"github.com/leg100/etok/pkg/signals"
)
func main() {
// Exit code
var code int
if err := run(os.Args[1:], os.Stdout, os.Stderr, os.Stdin); err != nil {
code = handleError(err, os.Stderr)
}
os.Exit(code)
}
func run(args []string, out, errout io.Writer, in io.Reader) error {
// Create context, and cancel if interrupt is received
ctx, cancel := context.WithCancel(context.Background())
signals.CatchCtrlC(cancel)
// Construct options and their defaults
f := cmdutil.NewFactory(out, errout, in)
// Parse args and execute selected command
return cmd.ParseArgs(ctx, args, f)
}
// Print error message unless the error originated from executing a program (which would have
// printed its own message)
func handleError(err error, out io.Writer) int {
var exit etokerrors.ExitError
if errors.As(err, &exit) {
return exit.ExitCode()
}
fmt.Fprintf(out, "%s %s\n", color.HiRedString("Error:"), err.Error())
return 1
}