Skip to content

Commit

Permalink
fix: re-enable debug mode (#69)
Browse files Browse the repository at this point in the history
Closes: WORLD-1154

## Overview

Re enable debug mode with delve on `world cardinal start` with `--debug`
flag

## Brief Changelog

- Re activate `--debug` flag
- Add `nakama-debug` container to fix nakama cannot run because
`cardinal` is not running

## Testing and Verifying

- Tested manually using `world cardinal start --debug` command and delve
with vs code

Notes : Unit test failed because
Argus-Labs/starter-game-template#51 need to be
merged first and create new tag
  • Loading branch information
zulkhair committed Jun 21, 2024
1 parent 8685e76 commit b0fefd8
Show file tree
Hide file tree
Showing 16 changed files with 17 additions and 56 deletions.
2 changes: 0 additions & 2 deletions cmd/world/cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 0 additions & 2 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var devCmd = &cobra.Command{
return err
}

logger.SetDebugMode(cmd)

cfg, err := config.GetConfig(cmd)
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/cardinal/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions cmd/world/cardinal/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/cardinal/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/cardinal/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions cmd/world/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
2 changes: 0 additions & 2 deletions cmd/world/evm/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions cmd/world/evm/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import (

"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

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
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/root/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/root/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions cmd/world/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions cmd/world/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 6 additions & 16 deletions common/logger/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
}
6 changes: 3 additions & 3 deletions common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
1 change: 1 addition & 0 deletions common/teacmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0fefd8

Please sign in to comment.