|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
4 | 8 | "github.com/sirupsen/logrus" |
5 | 9 | "github.com/spf13/cobra" |
| 10 | + terminal "golang.org/x/term" |
6 | 11 |
|
| 12 | + "github.com/openshift/installer/cmd/openshift-install/command" |
7 | 13 | "github.com/openshift/installer/pkg/nodejoiner" |
8 | 14 | ) |
9 | 15 |
|
@@ -33,14 +39,47 @@ func main() { |
33 | 39 | } |
34 | 40 |
|
35 | 41 | rootCmd := &cobra.Command{ |
36 | | - Use: "node-joiner", |
| 42 | + Use: "node-joiner", |
| 43 | + PersistentPreRun: runRootCmd, |
37 | 44 | } |
38 | 45 | rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file.") |
39 | 46 | rootCmd.PersistentFlags().String("dir", ".", "assets directory") |
| 47 | + rootCmd.PersistentFlags().String("log-level", "info", "log level (e.g. \"debug | info | warn | error\")") |
40 | 48 |
|
41 | 49 | rootCmd.AddCommand(nodesAddCmd) |
42 | 50 | rootCmd.AddCommand(nodesMonitorCmd) |
43 | 51 | if err := rootCmd.Execute(); err != nil { |
44 | 52 | logrus.Fatal(err) |
45 | 53 | } |
46 | 54 | } |
| 55 | + |
| 56 | +func runRootCmd(cmd *cobra.Command, args []string) { |
| 57 | + logrus.SetOutput(io.Discard) |
| 58 | + logrus.SetLevel(logrus.TraceLevel) |
| 59 | + |
| 60 | + logLevel, err := cmd.Flags().GetString("log-level") |
| 61 | + if err != nil { |
| 62 | + logrus.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + level, err := logrus.ParseLevel(logLevel) |
| 66 | + if err != nil { |
| 67 | + level = logrus.InfoLevel |
| 68 | + } |
| 69 | + |
| 70 | + logrus.AddHook(command.NewFileHookWithNewlineTruncate(os.Stderr, level, &logrus.TextFormatter{ |
| 71 | + // Setting ForceColors is necessary because logrus.TextFormatter determines |
| 72 | + // whether or not to enable colors by looking at the output of the logger. |
| 73 | + // In this case, the output is io.Discard, which is not a terminal. |
| 74 | + // Overriding it here allows the same check to be done, but against the |
| 75 | + // hook's output instead of the logger's output. |
| 76 | + ForceColors: terminal.IsTerminal(int(os.Stderr.Fd())), |
| 77 | + DisableTimestamp: true, |
| 78 | + DisableLevelTruncation: true, |
| 79 | + DisableQuote: true, |
| 80 | + })) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + logrus.Fatal(fmt.Errorf("invalid log-level: %w", err)) |
| 84 | + } |
| 85 | +} |
0 commit comments