forked from iximiuz/cdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (77 loc) · 1.93 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
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
package main
import (
cryptorand "crypto/rand"
"encoding/binary"
"fmt"
mathrand "math/rand"
"os"
"time"
"github.com/moby/term"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/iximiuz/cdebug/cmd/exec"
"github.com/iximiuz/cdebug/cmd/portforward"
"github.com/iximiuz/cdebug/pkg/cliutil"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func init() {
var buf [8]byte
if _, err := cryptorand.Read(buf[:]); err == nil {
mathrand.Seed(int64(binary.LittleEndian.Uint64(buf[:])))
} else {
mathrand.Seed(time.Now().UnixNano())
}
}
func main() {
stdin, stdout, stderr := term.StdStreams()
cli := cliutil.NewCLI(stdin, stdout, stderr)
var logLevel string
logrus.SetOutput(cli.ErrorStream())
cmd := &cobra.Command{
Use: "cdebug [OPTIONS] COMMAND [ARG...]",
Short: "cdebug - a swiss army knife of container debugging",
Version: fmt.Sprintf("%s (built: %s commit: %s)", version, date, commit),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(cli, logLevel)
cmd.SilenceUsage = true
cmd.SilenceErrors = true
},
}
cmd.SetOut(cli.OutputStream())
cmd.SetErr(cli.ErrorStream())
cmd.AddCommand(
exec.NewCommand(cli),
portforward.NewCommand(cli),
// TODO: other commands
)
flags := cmd.PersistentFlags()
flags.SetInterspersed(false) // Instead of relying on --
flags.StringVarP(
&logLevel,
"log-level",
"l",
"info",
`log level for cdebug ("debug" | "info" | "warn" | "error" | "fatal")`,
)
if err := cmd.Execute(); err != nil {
if sterr, ok := err.(cliutil.StatusError); ok {
cli.PrintErr("cdebug: %s\n", sterr)
os.Exit(sterr.Code())
}
// Hopefully, only usage errors.
logrus.Debug("Exit error: %s", err)
os.Exit(1)
}
}
func setLogLevel(cli cliutil.CLI, logLevel string) {
lvl, err := logrus.ParseLevel(logLevel)
if err != nil {
cli.PrintErr("Unable to parse log level: %s\n", logLevel)
os.Exit(1)
}
logrus.SetLevel(lvl)
}