forked from openshift/installer
/
main.go
72 lines (61 loc) · 1.8 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
package main
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
)
var (
rootOpts struct {
dir string
logLevel string
}
)
func main() {
rootCmd := newRootCmd()
for _, subCmd := range []*cobra.Command{
newCreateCmd(),
newDestroyCmd(),
newVersionCmd(),
newGraphCmd(),
} {
rootCmd.AddCommand(subCmd)
}
if err := rootCmd.Execute(); err != nil {
logrus.Fatalf("Error executing openshift-install: %v", err)
}
}
func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "openshift-install",
Short: "Creates OpenShift clusters",
Long: "",
PersistentPreRunE: runRootCmd,
SilenceErrors: true,
SilenceUsage: true,
}
cmd.PersistentFlags().StringVar(&rootOpts.dir, "dir", ".", "assets directory")
cmd.PersistentFlags().StringVar(&rootOpts.logLevel, "log-level", "info", "log level (e.g. \"debug | info | warn | error\")")
return cmd
}
func runRootCmd(cmd *cobra.Command, args []string) error {
logrus.SetOutput(ioutil.Discard)
logrus.SetLevel(logrus.TraceLevel)
level, err := logrus.ParseLevel(rootOpts.logLevel)
if err != nil {
return errors.Wrap(err, "invalid log-level")
}
logrus.AddHook(newFileHook(os.Stderr, level, &logrus.TextFormatter{
// Setting ForceColors is necessary because logrus.TextFormatter determines
// whether or not to enable colors by looking at the output of the logger.
// In this case, the output is ioutil.Discard, which is not a terminal.
// Overriding it here allows the same check to be done, but against the
// hook's output instead of the logger's output.
ForceColors: terminal.IsTerminal(int(os.Stderr.Fd())),
DisableTimestamp: true,
DisableLevelTruncation: true,
}))
return nil
}