From 1df124c7a89c765050a39612d04995387d398d3f Mon Sep 17 00:00:00 2001 From: zulkhair Date: Wed, 12 Jun 2024 15:59:30 +0700 Subject: [PATCH] re enable debug mode --- cmd/world/cardinal/cardinal.go | 2 -- cmd/world/cardinal/dev.go | 2 -- cmd/world/cardinal/purge.go | 4 +--- cmd/world/cardinal/restart.go | 5 +---- cmd/world/cardinal/start.go | 4 +--- cmd/world/cardinal/stop.go | 4 +--- cmd/world/evm/evm.go | 3 --- cmd/world/evm/start.go | 2 -- cmd/world/evm/stop.go | 5 +---- cmd/world/root/create.go | 4 +--- cmd/world/root/doctor.go | 4 +--- cmd/world/root/login.go | 2 -- cmd/world/root/root.go | 7 ++----- common/logger/init.go | 33 ++++++++++++++------------------- common/logger/logger.go | 6 +++--- common/teacmd/docker.go | 7 ++++--- 16 files changed, 30 insertions(+), 64 deletions(-) diff --git a/cmd/world/cardinal/cardinal.go b/cmd/world/cardinal/cardinal.go index 14f3a30..2136326 100644 --- a/cmd/world/cardinal/cardinal.go +++ b/cmd/world/cardinal/cardinal.go @@ -34,6 +34,4 @@ var BaseCmd = &cobra.Command{ func init() { // Register subcommands - `world cardinal [subcommand]` BaseCmd.AddCommand(startCmd, devCmd, restartCmd, purgeCmd, stopCmd) - // Add --log-debug flag - logger.AddLogFlag(startCmd, devCmd, restartCmd, purgeCmd, stopCmd) } diff --git a/cmd/world/cardinal/dev.go b/cmd/world/cardinal/dev.go index ae9dd96..5c32d7c 100644 --- a/cmd/world/cardinal/dev.go +++ b/cmd/world/cardinal/dev.go @@ -52,8 +52,6 @@ var devCmd = &cobra.Command{ return err } - logger.SetDebugMode(cmd) - cfg, err := config.GetConfig(cmd) if err != nil { return err diff --git a/cmd/world/cardinal/purge.go b/cmd/world/cardinal/purge.go index 40c525e..46e2daf 100644 --- a/cmd/world/cardinal/purge.go +++ b/cmd/world/cardinal/purge.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" ) @@ -20,8 +19,7 @@ var purgeCmd = &cobra.Command{ Short: "Stop and reset the state of your Cardinal game shard", Long: `Stop and reset the state of your Cardinal game shard. This command stop all Docker services and remove all Docker volumes.`, - RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) + RunE: func(_ *cobra.Command, _ []string) error { err := teacmd.DockerPurge() if err != nil { return err diff --git a/cmd/world/cardinal/restart.go b/cmd/world/cardinal/restart.go index 4f519ed..354c3a4 100644 --- a/cmd/world/cardinal/restart.go +++ b/cmd/world/cardinal/restart.go @@ -4,7 +4,6 @@ import ( "github.com/spf13/cobra" "pkg.world.dev/world-cli/common/config" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" ) @@ -19,8 +18,6 @@ This will restart the following Docker services: - Cardinal (Core game logic) - Nakama (Relay)`, RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) - cfg, err := config.GetConfig(cmd) if err != nil { return err @@ -37,7 +34,7 @@ This will restart the following Docker services: if cfg.Debug { err = teacmd.DockerRestart(cfg, []teacmd.DockerService{ teacmd.DockerServiceCardinalDebug, - teacmd.DockerServiceNakama, + teacmd.DockerServiceNakamaDebug, }) } else { err = teacmd.DockerRestart(cfg, []teacmd.DockerService{ diff --git a/cmd/world/cardinal/start.go b/cmd/world/cardinal/start.go index dd0e567..8ed5085 100644 --- a/cmd/world/cardinal/start.go +++ b/cmd/world/cardinal/start.go @@ -11,7 +11,6 @@ import ( "pkg.world.dev/world-cli/common" "pkg.world.dev/world-cli/common/config" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" ) @@ -58,8 +57,6 @@ This will start the following Docker services and its dependencies: - Nakama (Relay) - Redis (Cardinal dependency)`, RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) - cfg, err := config.GetConfig(cmd) if err != nil { return err @@ -150,6 +147,7 @@ func init() { startCmd.Flags().Bool(flagBuild, true, "Rebuild Docker images before starting") startCmd.Flags().Bool(flagDetach, false, "Run in detached mode") startCmd.Flags().String(flagLogLevel, "", "Set the log level") + startCmd.Flags().Bool(flagDebug, false, "Enable delve debugging") } // replaceBoolWithFlag overwrites the contents of vale with the contents of the given flag. If the flag diff --git a/cmd/world/cardinal/stop.go b/cmd/world/cardinal/stop.go index decf8ec..c10a96d 100644 --- a/cmd/world/cardinal/stop.go +++ b/cmd/world/cardinal/stop.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" ) @@ -24,8 +23,7 @@ This will stop the following Docker services: - Cardinal (Game shard) - Nakama (Relay) + DB - Redis (Cardinal dependency)`, - RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) + RunE: func(_ *cobra.Command, _ []string) error { err := teacmd.DockerStopAll() if err != nil { return err diff --git a/cmd/world/evm/evm.go b/cmd/world/evm/evm.go index 32c4a17..ce9863e 100644 --- a/cmd/world/evm/evm.go +++ b/cmd/world/evm/evm.go @@ -3,7 +3,6 @@ package evm import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" "pkg.world.dev/world-cli/tea/style" ) @@ -28,6 +27,4 @@ var BaseCmd = &cobra.Command{ func init() { // Register subcommands - `world evm [subcommand]` BaseCmd.AddCommand(startCmd, stopCmd) - // Add --debug flag - logger.AddLogFlag(startCmd, stopCmd) } diff --git a/cmd/world/evm/start.go b/cmd/world/evm/start.go index fa7732b..e530592 100644 --- a/cmd/world/evm/start.go +++ b/cmd/world/evm/start.go @@ -27,8 +27,6 @@ var startCmd = &cobra.Command{ Short: "Start the EVM base shard. Use --da-auth-token to pass in an auth token directly.", Long: "Start the EVM base shard. Requires connection to celestia DA.", RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) - cfg, err := config.GetConfig(cmd) if err != nil { return err diff --git a/cmd/world/evm/stop.go b/cmd/world/evm/stop.go index f6162a5..badf018 100644 --- a/cmd/world/evm/stop.go +++ b/cmd/world/evm/stop.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" ) @@ -13,9 +12,7 @@ var stopCmd = &cobra.Command{ Use: "stop", Short: "Stop the EVM base shard and DA layer client.", Long: "Stop the EVM base shard and data availability layer client if they are running.", - RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) - + RunE: func(_ *cobra.Command, _ []string) error { err := teacmd.DockerStop(services(teacmd.DockerServiceEVM, teacmd.DockerServiceDA)) if err != nil { return err diff --git a/cmd/world/root/create.go b/cmd/world/root/create.go index bbe27b9..0e01512 100644 --- a/cmd/world/root/create.go +++ b/cmd/world/root/create.go @@ -8,7 +8,6 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" "pkg.world.dev/world-cli/tea/component/steps" "pkg.world.dev/world-cli/tea/style" @@ -204,8 +203,7 @@ If [directory_name] is set, it will automatically clone the starter project into Otherwise, it will prompt you to enter a directory name.`, GroupID: "starter", Args: cobra.MaximumNArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - logger.SetDebugMode(cmd) + RunE: func(_ *cobra.Command, args []string) error { p := tea.NewProgram(NewWorldCreateModel(args), tea.WithOutput(writer)) if _, err := p.Run(); err != nil { return err diff --git a/cmd/world/root/doctor.go b/cmd/world/root/doctor.go index cd23811..807e03c 100644 --- a/cmd/world/root/doctor.go +++ b/cmd/world/root/doctor.go @@ -7,7 +7,6 @@ import ( "github.com/spf13/cobra" "pkg.world.dev/world-cli/common/dependency" - "pkg.world.dev/world-cli/common/logger" "pkg.world.dev/world-cli/common/teacmd" "pkg.world.dev/world-cli/tea/style" ) @@ -84,8 +83,7 @@ World CLI requires the following dependencies to be installed: - Go - Docker`, GroupID: "starter", - RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) + RunE: func(_ *cobra.Command, _ []string) error { p := tea.NewProgram(NewWorldDoctorModel(), tea.WithOutput(writer)) _, err := p.Run() if err != nil { diff --git a/cmd/world/root/login.go b/cmd/world/root/login.go index 21be91f..2125080 100644 --- a/cmd/world/root/login.go +++ b/cmd/world/root/login.go @@ -39,8 +39,6 @@ func getLoginCmd() *cobra.Command { Use: "login", Short: "Authenticate using an access token", RunE: func(cmd *cobra.Command, _ []string) error { - logger.SetDebugMode(cmd) - err := loginOnBrowser(cmd.Context()) if err != nil { return eris.Wrap(err, "failed to login") diff --git a/cmd/world/root/root.go b/cmd/world/root/root.go index 1939f91..bc2de56 100644 --- a/cmd/world/root/root.go +++ b/cmd/world/root/root.go @@ -74,10 +74,7 @@ func init() { rootCmd.AddCommand(evm.BaseCmd) config.AddConfigFlag(rootCmd) - - // Add --debug flag - logger.AddLogFlag(createCmd) - logger.AddLogFlag(doctorCmd) + logger.AddVerboseFlag(rootCmd) } func checkLatestVersion() error { @@ -146,7 +143,7 @@ func Execute() { logger.Errors(err) } // print log stack - logger.PrintLogs() + logger.PrintLogs(rootCmd) } // contextWithSigterm provides a context that automatically terminates when either the parent context is canceled or diff --git a/common/logger/init.go b/common/logger/init.go index 83e9b9c..f727390 100644 --- a/common/logger/init.go +++ b/common/logger/init.go @@ -13,16 +13,16 @@ const ( DefaultTimeFormat = "15:04:05.000" DefaultCallerSkipFrameCount = 3 // set to 3 because logger wrapped in logger.go - NoColor = true - UseCaller = false // for developer, if you want to expose line of code of caller - flagDebug = "log-debug" + NoColor = true + UseCaller = false // for developer, if you want to expose line of code of caller + flagVerbose = "verbose" ) var ( logBuffer bytes.Buffer - // DebugMode flag for determining debug mode - DebugMode = false + // VerboseMode flag for determining verbose logging + VerboseMode = false ) func init() { @@ -51,8 +51,12 @@ func init() { } // PrintLogs print all stacked log -func PrintLogs() { - if DebugMode { +func PrintLogs(cmd *cobra.Command) { + verbose, err := cmd.PersistentFlags().GetBool(flagVerbose) + if err != nil { + log.Error().Err(err).Msg("Cannot get flag --verbose") + } + if verbose { // Extract the logs from the buffer and print them logs := logBuffer.String() if len(logs) > 0 { @@ -62,18 +66,9 @@ func PrintLogs() { } } -// SetDebugMode Allow particular logger/message to be printed -// This function will extract flag --log-debug from command -func SetDebugMode(cmd *cobra.Command) { - val, err := cmd.Flags().GetBool(flagDebug) - if err == nil { - DebugMode = val - } -} - -// AddLogFlag set flag --log-debug -func AddLogFlag(cmd ...*cobra.Command) { +// AddVerboseFlag set flag --log-debug +func AddVerboseFlag(cmd ...*cobra.Command) { for _, c := range cmd { - c.Flags().Bool(flagDebug, false, "Enable World CLI debug logs") + c.PersistentFlags().Bool(flagVerbose, true, "Enable World CLI debug logs") } } diff --git a/common/logger/logger.go b/common/logger/logger.go index 2d24cef..a7a2d3e 100644 --- a/common/logger/logger.go +++ b/common/logger/logger.go @@ -118,21 +118,21 @@ func FatalWithFields(msg string, kv map[string]interface{}) { // Printf standard printf with debug mode validation func Printf(format string, v ...interface{}) { - if DebugMode { + if VerboseMode { fmt.Printf(format, v...) } } // Println standard println with debug mode validation func Println(v ...interface{}) { - if DebugMode { + if VerboseMode { fmt.Println(v...) } } // Print standard print with debug mode validation func Print(v ...interface{}) { - if DebugMode { + if VerboseMode { fmt.Print(v...) } } diff --git a/common/teacmd/docker.go b/common/teacmd/docker.go index 07d5d2c..f8093c6 100644 --- a/common/teacmd/docker.go +++ b/common/teacmd/docker.go @@ -22,6 +22,7 @@ const ( DockerServiceEVM DockerService = "evm" DockerServiceDA DockerService = "celestia-devnet" DockerServiceCardinalDebug DockerService = "cardinal-debug" + DockerServiceNakamaDebug DockerService = "nakama-debug" ) type DockerService string @@ -98,15 +99,14 @@ func DockerStart(cfg *config.Config, services []DockerService) error { // DockerStartAll starts both cardinal and nakama func DockerStartAll(cfg *config.Config) error { services := []DockerService{ - DockerServiceNakama, DockerServiceNakamaDB, DockerServiceRedis, } if cfg.Debug { - services = append(services, DockerServiceCardinalDebug) + services = append(services, DockerServiceCardinalDebug, DockerServiceNakamaDebug) } else { - services = append(services, DockerServiceCardinal) + services = append(services, DockerServiceCardinal, DockerServiceNakama) } return DockerStart(cfg, services) @@ -150,6 +150,7 @@ func DockerStopAll() error { DockerServiceCardinal, DockerServiceCardinalDebug, DockerServiceNakama, + DockerServiceNakamaDebug, DockerServiceNakamaDB, DockerServiceRedis, })