@@ -19,6 +19,7 @@ import (
1919 "github.com/spf13/cobra"
2020 "go.uber.org/zap"
2121 "go.uber.org/zap/zapcore"
22+ "go.yaml.in/yaml/v4"
2223
2324 "github.com/siderolabs/omni/client/pkg/constants"
2425 "github.com/siderolabs/omni/client/pkg/panichandler"
@@ -37,28 +38,14 @@ var rootCmd = &cobra.Command{
3738 SilenceUsage : true ,
3839 Version : version .Tag ,
3940 RunE : func (* cobra.Command , []string ) error {
40- var loggerConfig zap.Config
41-
42- if constants .IsDebugBuild {
43- loggerConfig = zap .NewDevelopmentConfig ()
44- loggerConfig .EncoderConfig .EncodeLevel = zapcore .CapitalColorLevelEncoder
45- } else {
46- loggerConfig = zap .NewProductionConfig ()
47- }
48-
49- if ! rootCmdArgs .debug {
50- loggerConfig .Level .SetLevel (zap .InfoLevel )
51- } else {
52- loggerConfig .Level .SetLevel (zap .DebugLevel )
53- }
54-
55- logger , err := loggerConfig .Build (
56- zap .AddStacktrace (zapcore .FatalLevel ), // only print stack traces for fatal errors
57- )
41+ logger , err := buildLogger ()
5842 if err != nil {
5943 return fmt .Errorf ("failed to set up logging: %w" , err )
6044 }
6145
46+ zap .ReplaceGlobals (logger )
47+ zap .RedirectStdLog (logger )
48+
6249 signals := make (chan os.Signal , 1 )
6350
6451 signal .Notify (signals , syscall .SIGINT , syscall .SIGTERM )
@@ -75,11 +62,24 @@ var rootCmd = &cobra.Command{
7562 cancel ()
7663 }, logger )
7764
78- config , err := app .PrepareConfig (logger , cmdConfig )
65+ skipConfigValidation := rootCmdArgs .printConfigAndExit
66+
67+ config , err := app .PrepareConfig (skipConfigValidation , logger , cmdConfig )
7968 if err != nil {
8069 return err
8170 }
8271
72+ if rootCmdArgs .printConfigAndExit {
73+ encoder := yaml .NewEncoder (os .Stdout )
74+ encoder .SetIndent (2 )
75+
76+ if err := encoder .Encode (config ); err != nil {
77+ return err
78+ }
79+
80+ return nil
81+ }
82+
8383 ctx = actor .MarkContextAsInternalActor (ctx )
8484
8585 state , err := omni .NewState (ctx , config , logger , prometheus .DefaultRegisterer )
@@ -106,8 +106,9 @@ var rootCmd = &cobra.Command{
106106}
107107
108108var rootCmdArgs struct {
109- configPath string
110- debug bool
109+ configPath string
110+ debug bool
111+ printConfigAndExit bool
111112}
112113
113114// Execute the command.
@@ -140,6 +141,9 @@ var cmdConfig = config.Default()
140141func newCommand () * cobra.Command {
141142 rootCmd .Flags ().BoolVar (& rootCmdArgs .debug , "debug" , constants .IsDebugBuild , "enable debug logs." )
142143
144+ rootCmd .Flags ().BoolVar (& rootCmdArgs .printConfigAndExit , "print-config-and-exit" , false , "print the final configuration and exit." )
145+ rootCmd .Flags ().MarkHidden ("print-config-and-exit" ) //nolint:errcheck
146+
143147 rootCmd .Flags ().StringVar (& cmdConfig .Account .ID , "account-id" , cmdConfig .Account .ID , "instance account ID, should never be changed." )
144148 rootCmd .Flags ().StringVar (& cmdConfig .Account .Name , "name" , cmdConfig .Account .Name , "instance user-facing name." )
145149 rootCmd .Flags ().StringVar (& cmdConfig .Account .UserPilot .AppToken , "user-pilot-app-token" , cmdConfig .Account .UserPilot .AppToken , "user pilot app token." )
@@ -156,6 +160,36 @@ func newCommand() *cobra.Command {
156160 return rootCmd
157161}
158162
163+ func buildLogger () (* zap.Logger , error ) {
164+ if rootCmdArgs .printConfigAndExit {
165+ return zap .NewNop (), nil
166+ }
167+
168+ var loggerConfig zap.Config
169+
170+ if constants .IsDebugBuild {
171+ loggerConfig = zap .NewDevelopmentConfig ()
172+ loggerConfig .EncoderConfig .EncodeLevel = zapcore .CapitalColorLevelEncoder
173+ } else {
174+ loggerConfig = zap .NewProductionConfig ()
175+ }
176+
177+ if ! rootCmdArgs .debug {
178+ loggerConfig .Level .SetLevel (zap .InfoLevel )
179+ } else {
180+ loggerConfig .Level .SetLevel (zap .DebugLevel )
181+ }
182+
183+ logger , err := loggerConfig .Build (
184+ zap .AddStacktrace (zapcore .FatalLevel ), // only print stack traces for fatal errors
185+ )
186+ if err != nil {
187+ return nil , fmt .Errorf ("failed to set up logging: %w" , err )
188+ }
189+
190+ return logger , nil
191+ }
192+
159193func defineServiceFlags () {
160194 // API
161195 rootCmd .Flags ().StringVar (
0 commit comments