From a4b91200e4cdd24027e00e1e12b18e4cd1f0d7fc 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 | 3 --- 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 | 5 +---- common/logger/init.go | 22 ++++++---------------- common/logger/logger.go | 6 +++--- common/teacmd/docker.go | 1 + 16 files changed, 17 insertions(+), 56 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..84488c5 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 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..58591b2 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 { diff --git a/common/logger/init.go b/common/logger/init.go index 83e9b9c..80da3d4 100644 --- a/common/logger/init.go +++ b/common/logger/init.go @@ -15,14 +15,13 @@ const ( NoColor = true UseCaller = false // for developer, if you want to expose line of code of caller - flagDebug = "log-debug" ) var ( logBuffer bytes.Buffer - // DebugMode flag for determining debug mode - DebugMode = false + // VerboseMode flag for determining verbose logging + verboseMode = false ) func init() { @@ -52,7 +51,7 @@ func init() { // PrintLogs print all stacked log func PrintLogs() { - if DebugMode { + if verboseMode { // Extract the logs from the buffer and print them logs := logBuffer.String() if len(logs) > 0 { @@ -62,18 +61,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().BoolVarP(&verboseMode, "verbose", "v", false, "Enable World CLI debug logs") } } diff --git a/common/logger/logger.go b/common/logger/logger.go index 2d24cef..f0425ca 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..e1e7d3d 100644 --- a/common/teacmd/docker.go +++ b/common/teacmd/docker.go @@ -44,6 +44,7 @@ func dockerComposeWithCfg(cfg *config.Config, args ...string) error { } if cfg.Debug { env = append(env, "CARDINAL_ADDR=cardinal-debug:4040") + env = append(env, "CARDINAL_CONTAINER=cardinal-debug") } cmd.Env = env