Skip to content

Commit

Permalink
re enable debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Jun 20, 2024
1 parent 8685e76 commit 974df45
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 90 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
18 changes: 4 additions & 14 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 All @@ -34,17 +31,10 @@ This will restart the following Docker services:
return err
}

if cfg.Debug {
err = teacmd.DockerRestart(cfg, []teacmd.DockerService{
teacmd.DockerServiceCardinalDebug,
teacmd.DockerServiceNakama,
})
} else {
err = teacmd.DockerRestart(cfg, []teacmd.DockerService{
teacmd.DockerServiceCardinal,
teacmd.DockerServiceNakama,
})
}
err = teacmd.DockerRestart(cfg, []teacmd.DockerService{
teacmd.DockerServiceCardinal,
teacmd.DockerServiceNakama,
})

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 {

Check warning on line 15 in cmd/world/evm/stop.go

View check run for this annotation

Codecov / codecov/patch

cmd/world/evm/stop.go#L15

Added line #L15 was not covered by tests
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
7 changes: 2 additions & 5 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 Expand Up @@ -146,7 +143,7 @@ func Execute() {
logger.Errors(err)
}
// print log stack
logger.PrintLogs()
logger.PrintLogs(rootCmd)

Check warning on line 146 in cmd/world/root/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/world/root/root.go#L146

Added line #L146 was not covered by tests
}

// contextWithSigterm provides a context that automatically terminates when either the parent context is canceled or
Expand Down
33 changes: 14 additions & 19 deletions common/logger/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")

Check warning on line 57 in common/logger/init.go

View check run for this annotation

Codecov / codecov/patch

common/logger/init.go#L54-L57

Added lines #L54 - L57 were not covered by tests
}
if verbose {

Check warning on line 59 in common/logger/init.go

View check run for this annotation

Codecov / codecov/patch

common/logger/init.go#L59

Added line #L59 was not covered by tests
// Extract the logs from the buffer and print them
logs := logBuffer.String()
if len(logs) > 0 {
Expand All @@ -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")
}
}
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 {

Check warning on line 121 in common/logger/logger.go

View check run for this annotation

Codecov / codecov/patch

common/logger/logger.go#L121

Added line #L121 was not covered by tests
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 {

Check warning on line 135 in common/logger/logger.go

View check run for this annotation

Codecov / codecov/patch

common/logger/logger.go#L135

Added line #L135 was not covered by tests
fmt.Print(v...)
}
}
33 changes: 14 additions & 19 deletions common/teacmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import (
)

const (
DockerServiceCardinal DockerService = "cardinal"
DockerServiceNakama DockerService = "nakama"
DockerServiceNakamaDB DockerService = "nakama-db"
DockerServiceRedis DockerService = "redis"
DockerServiceEVM DockerService = "evm"
DockerServiceDA DockerService = "celestia-devnet"
DockerServiceCardinalDebug DockerService = "cardinal-debug"
DockerServiceCardinal DockerService = "cardinal"
DockerServiceNakama DockerService = "nakama"
DockerServiceNakamaDB DockerService = "nakama-db"
DockerServiceRedis DockerService = "redis"
DockerServiceEVM DockerService = "evm"
DockerServiceDA DockerService = "celestia-devnet"
)

type DockerService string
Expand All @@ -32,7 +31,12 @@ func dockerCompose(args ...string) error {

func dockerComposeWithCfg(cfg *config.Config, args ...string) error {
yml := path.Join(cfg.RootDir, "docker-compose.yml")
args = append([]string{"compose", "-f", yml}, args...)
compose := []string{"compose", "-f", yml}
if cfg.Debug {
debugYml := path.Join(cfg.RootDir, "docker-compose.debug.yml")
compose = append(compose, "-f", debugYml)

Check warning on line 37 in common/teacmd/docker.go

View check run for this annotation

Codecov / codecov/patch

common/teacmd/docker.go#L34-L37

Added lines #L34 - L37 were not covered by tests
}
args = append(compose, args...)

Check warning on line 39 in common/teacmd/docker.go

View check run for this annotation

Codecov / codecov/patch

common/teacmd/docker.go#L39

Added line #L39 was not covered by tests

cmd := exec.Command("docker", args...)
cmd.Stdout = os.Stdout
Expand All @@ -42,9 +46,6 @@ func dockerComposeWithCfg(cfg *config.Config, args ...string) error {
for k, v := range cfg.DockerEnv {
env = append(env, k+"="+v)
}
if cfg.Debug {
env = append(env, "CARDINAL_ADDR=cardinal-debug:4040")
}

cmd.Env = env
if err := cmd.Run(); err != nil {
Expand Down Expand Up @@ -98,15 +99,10 @@ 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)
} else {
services = append(services, DockerServiceCardinal)
DockerServiceNakama,
DockerServiceCardinal,

Check warning on line 105 in common/teacmd/docker.go

View check run for this annotation

Codecov / codecov/patch

common/teacmd/docker.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}

return DockerStart(cfg, services)
Expand Down Expand Up @@ -148,7 +144,6 @@ func DockerStop(services []DockerService) error {
func DockerStopAll() error {
return DockerStop([]DockerService{
DockerServiceCardinal,
DockerServiceCardinalDebug,
DockerServiceNakama,
DockerServiceNakamaDB,
DockerServiceRedis,
Expand Down

0 comments on commit 974df45

Please sign in to comment.