From 497e54f0bf1b274f5c2e91a876e857df9fbb4494 Mon Sep 17 00:00:00 2001 From: tyler <48813565+technicallyty@users.noreply.github.com> Date: Thu, 16 Nov 2023 12:25:58 -0800 Subject: [PATCH 1/2] wip --- cmd/world/cardinal/cardinal.go | 30 ++++---- cmd/world/cardinal/create.go | 19 +++--- cmd/world/cardinal/dev.go | 2 +- cmd/world/cardinal/purge.go | 3 +- cmd/world/cardinal/restart.go | 7 +- cmd/world/cardinal/start.go | 78 +++++++++++---------- cmd/world/cardinal/stop.go | 3 +- cmd/world/evm/evm.go | 79 ++++++++++++++++++++++ cmd/world/root/doctor.go | 21 +++--- cmd/world/root/root.go | 5 +- common/{tea_cmd => teacmd}/dependencies.go | 2 +- common/{tea_cmd => teacmd}/docker.go | 12 ++-- common/{tea_cmd => teacmd}/git.go | 2 +- go.mod | 19 +++--- go.sum | 32 ++++----- 15 files changed, 194 insertions(+), 120 deletions(-) create mode 100644 cmd/world/evm/evm.go rename common/{tea_cmd => teacmd}/dependencies.go (99%) rename common/{tea_cmd => teacmd}/docker.go (93%) rename common/{tea_cmd => teacmd}/git.go (98%) diff --git a/cmd/world/cardinal/cardinal.go b/cmd/world/cardinal/cardinal.go index aefa27b..4a928ca 100644 --- a/cmd/world/cardinal/cardinal.go +++ b/cmd/world/cardinal/cardinal.go @@ -6,21 +6,19 @@ import ( "pkg.world.dev/world-cli/tea/style" ) -func init() { - // Register subcommands - `world cardinal [subcommand]` - BaseCmd.AddCommand(createCmd, startCmd, devCmd, restartCmd, purgeCmd, stopCmd) -} - // BaseCmd is the base command for the cardinal subcommand -// Usage: `world cardinal` -var BaseCmd = &cobra.Command{ - Use: "cardinal", - Short: "Manage your Cardinal game shard project", - Long: style.CLIHeader("World CLI — CARDINAL", "Manage your Cardinal game shard project"), - GroupID: "Core", - Run: func(cmd *cobra.Command, args []string) { - if err := cmd.Help(); err != nil { - log.Fatal().Err(err).Msg("Failed to execute cardinal command") - } - }, +func BaseCmd() *cobra.Command { + base := &cobra.Command{ + Use: "cardinal", + Short: "Manage your Cardinal game shard project", + Long: style.CLIHeader("World CLI — CARDINAL", "Manage your Cardinal game shard project"), + GroupID: "Core", + Run: func(cmd *cobra.Command, args []string) { + if err := cmd.Help(); err != nil { + log.Fatal().Err(err).Msg("Failed to execute cardinal command") + } + }, + } + base.AddCommand(createCmd, StartCmd(), devCmd, restartCmd, purgeCmd, stopCmd) + return base } diff --git a/cmd/world/cardinal/create.go b/cmd/world/cardinal/create.go index 232244b..edbe7f2 100644 --- a/cmd/world/cardinal/create.go +++ b/cmd/world/cardinal/create.go @@ -4,7 +4,6 @@ import ( "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/tea_cmd" "pkg.world.dev/world-cli/tea/component/steps" "pkg.world.dev/world-cli/tea/style" "strings" @@ -12,8 +11,8 @@ import ( const TemplateGitUrl = "https://github.com/Argus-Labs/starter-game-template.git" -var CreateDeps = []tea_cmd.Dependency{ - tea_cmd.GitDependency, +var CreateDeps = []teacmd.Dependency{ + teacmd.GitDependency, } ///////////////// @@ -47,7 +46,7 @@ type WorldCreateModel struct { steps steps.Model projectNameInput textinput.Model args []string - depStatus []tea_cmd.DependencyStatus + depStatus []teacmd.DependencyStatus depStatusErr error err error } @@ -85,15 +84,15 @@ func NewWorldCreateModel(args []string) WorldCreateModel { func (m WorldCreateModel) Init() tea.Cmd { // If the project name was passed in as an argument, skip the 1st step if m.projectNameInput.Value() != "" { - return tea.Sequence(tea_cmd.CheckDependenciesCmd(CreateDeps), textinput.Blink, m.steps.StartCmd(), m.steps.CompleteStepCmd(nil)) + return tea.Sequence(teacmd.CheckDependenciesCmd(CreateDeps), textinput.Blink, m.steps.StartCmd(), m.steps.CompleteStepCmd(nil)) } - return tea.Sequence(tea_cmd.CheckDependenciesCmd(CreateDeps), textinput.Blink, m.steps.StartCmd()) + return tea.Sequence(teacmd.CheckDependenciesCmd(CreateDeps), textinput.Blink, m.steps.StartCmd()) } // Update handles incoming events and updates the model accordingly func (m WorldCreateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { - case tea_cmd.CheckDependenciesMsg: + case teacmd.CheckDependenciesMsg: m.depStatus = msg.DepStatus m.depStatusErr = msg.Err if msg.Err != nil { @@ -123,7 +122,7 @@ func (m WorldCreateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.Index == 1 { return m, tea.Sequence( NewLogCmd(style.ChevronIcon.Render()+"Cloning starter-game-template..."), - tea_cmd.GitCloneCmd(TemplateGitUrl, m.projectNameInput.Value(), "Initial commit from World CLI"), + teacmd.GitCloneCmd(TemplateGitUrl, m.projectNameInput.Value(), "Initial commit from World CLI"), ) } return m, nil @@ -142,7 +141,7 @@ func (m WorldCreateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // All done, quit return m, tea.Quit - case tea_cmd.GitCloneFinishMsg: + case teacmd.GitCloneFinishMsg: // If there is an error, log stderr then mark step as failed if msg.Err != nil { m.logs = append(m.logs, style.CrossIcon.Render()+msg.Err.Error()) @@ -166,7 +165,7 @@ func (m WorldCreateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // View renders the UI based on the data in the WorldCreateModel func (m WorldCreateModel) View() string { if m.depStatusErr != nil { - return tea_cmd.PrettyPrintMissingDependency(m.depStatus) + return teacmd.PrettyPrintMissingDependency(m.depStatus) } output := "" diff --git a/cmd/world/cardinal/dev.go b/cmd/world/cardinal/dev.go index 4dee39a..8545d5c 100644 --- a/cmd/world/cardinal/dev.go +++ b/cmd/world/cardinal/dev.go @@ -130,7 +130,7 @@ func runRedis() error { func runCardinal() (*exec.Cmd, error) { err := os.Chdir("cardinal") if err != nil { - return nil, errors.New("can't find cardinal directory. Are you in the root of a World Engine project") + return nil, errors.New("can't find cardinal directory. Are you in the root of a World Engine project?") } env := map[string]string{ diff --git a/cmd/world/cardinal/purge.go b/cmd/world/cardinal/purge.go index b35f12b..0ea492b 100644 --- a/cmd/world/cardinal/purge.go +++ b/cmd/world/cardinal/purge.go @@ -2,7 +2,6 @@ package cardinal import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/tea_cmd" ) ///////////////// @@ -17,7 +16,7 @@ var purgeCmd = &cobra.Command{ 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, args []string) error { - err := tea_cmd.DockerPurge() + err := teacmd.DockerPurge() if err != nil { return err } diff --git a/cmd/world/cardinal/restart.go b/cmd/world/cardinal/restart.go index f2355cc..7b50cca 100644 --- a/cmd/world/cardinal/restart.go +++ b/cmd/world/cardinal/restart.go @@ -2,7 +2,6 @@ package cardinal import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/tea_cmd" ) ///////////////// @@ -20,9 +19,9 @@ This will restart the following Docker services: - Cardinal (Core game logic) - Nakama (Relay)`, RunE: func(cmd *cobra.Command, args []string) error { - err := tea_cmd.DockerRestart(true, []tea_cmd.DockerService{ - tea_cmd.DockerServiceCardinal, - tea_cmd.DockerServiceNakama, + err := teacmd.DockerRestart(true, []teacmd.DockerService{ + teacmd.DockerServiceCardinal, + teacmd.DockerServiceNakama, }) if err != nil { return err diff --git a/cmd/world/cardinal/start.go b/cmd/world/cardinal/start.go index aa99e05..0f93a31 100644 --- a/cmd/world/cardinal/start.go +++ b/cmd/world/cardinal/start.go @@ -3,56 +3,54 @@ package cardinal import ( "fmt" "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/tea_cmd" ) ///////////////// // Cobra Setup // ///////////////// -func init() { - startCmd.Flags().Bool("build", true, "Rebuild Docker images before starting") - startCmd.Flags().Bool("debug", false, "Run in debug mode") - startCmd.Flags().Bool("detach", false, "Run in detached mode") -} - -// startCmd starts your Cardinal game shard stack -// Usage: `world cardinal start` -var startCmd = &cobra.Command{ - Use: "start", - Short: "Start your Cardinal game shard stack", - Long: `Start your Cardinal game shard stack. +// StartCmd returns a command that starts your Cardinal game shard stack. +func StartCmd() *cobra.Command { + startCmd := &cobra.Command{ + Use: "start", + Short: "Start your Cardinal game shard stack", + Long: `Start your Cardinal game shard stack. This will start the following Docker services and its dependencies: - Cardinal (Core game logic) - Nakama (Relay) - Redis (Cardinal dependency) - Postgres (Nakama dependency)`, - RunE: func(cmd *cobra.Command, args []string) error { - buildFlag, err := cmd.Flags().GetBool("build") - if err != nil { - return err - } - - debugFlag, err := cmd.Flags().GetBool("debug") - if err != nil { - return err - } - - detachFlag, err := cmd.Flags().GetBool("detach") - if err != nil { - return err - } - - fmt.Println("Starting Cardinal game shard...") - fmt.Println("This may take a few minutes to rebuild the Docker images.") - fmt.Println("Use `world cardinal dev` to run Cardinal faster/easier in development mode.") - - err = tea_cmd.DockerStartAll(buildFlag, debugFlag, detachFlag, -1) - if err != nil { - return err - } - - return nil - }, + RunE: func(cmd *cobra.Command, args []string) error { + buildFlag, err := cmd.Flags().GetBool("build") + if err != nil { + return err + } + + debugFlag, err := cmd.Flags().GetBool("debug") + if err != nil { + return err + } + + detachFlag, err := cmd.Flags().GetBool("detach") + if err != nil { + return err + } + + fmt.Println("Starting Cardinal game shard...") + fmt.Println("This may take a few minutes to rebuild the Docker images.") + fmt.Println("Use `world cardinal dev` to run Cardinal faster/easier in development mode.") + + err = teacmd.DockerStartAll(buildFlag, debugFlag, detachFlag, -1) + if err != nil { + return err + } + + return nil + }, + } + startCmd.Flags().Bool("build", true, "Rebuild Docker images before starting") + startCmd.Flags().Bool("debug", false, "Run in debug mode") + startCmd.Flags().Bool("detach", false, "Run in detached mode") + return startCmd } diff --git a/cmd/world/cardinal/stop.go b/cmd/world/cardinal/stop.go index e8ee814..4611bc7 100644 --- a/cmd/world/cardinal/stop.go +++ b/cmd/world/cardinal/stop.go @@ -2,7 +2,6 @@ package cardinal import ( "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/tea_cmd" ) ///////////////// @@ -22,7 +21,7 @@ This will stop the following Docker services: - Redis (Cardinal dependency) - Postgres (Nakama dependency)`, RunE: func(cmd *cobra.Command, args []string) error { - err := tea_cmd.DockerStopAll() + err := teacmd.DockerStopAll() if err != nil { return err } diff --git a/cmd/world/evm/evm.go b/cmd/world/evm/evm.go new file mode 100644 index 0000000..5c21d4b --- /dev/null +++ b/cmd/world/evm/evm.go @@ -0,0 +1,79 @@ +package evm + +import ( + "fmt" + "github.com/spf13/cobra" + "os" + "pkg.world.dev/world-cli/common/teacmd" +) + +func EVMCmds() *cobra.Command { + evmRootCmd := &cobra.Command{ + Use: "evm", + Short: "EVM base shard commands.", + Long: "Commands for provisioning the EVM Base Shard.", + } + evmRootCmd.AddGroup(&cobra.Group{ + ID: "EVM", + Title: "EVM Base Shard Commands", + }) + evmRootCmd.AddCommand( + StartEVM(), + ) + return evmRootCmd +} + +const ( + FlagDoNotSetToken = "dont-set-token" + FlagDAAuthToken = "da-auth-token" + EnvDAAuthToken = "DA_AUTH_TOKEN" +) + +func StartDA() *cobra.Command { + cmd := &cobra.Command{ + Use: "start-da", + Short: "Start the data availability layer client.", + Long: fmt.Sprintf("Start the data availability layer client. This command will automatically set the %s environment variable unless --%s is used.", EnvDAAuthToken, FlagDoNotSetToken), + RunE: func(cmd *cobra.Command, args []string) error { + dontSetToken, err := cmd.Flags().GetBool(FlagDoNotSetToken) + if err != nil { + return err + } + err = teacmd.DockerStart(true, false, false, 0, teacmd.DockerServiceDA) + if err != nil { + fmt.Errorf("error starting %s docker container: %w", teacmd.DockerServiceDA, err) + } + return nil + }, + } + cmd.Flags().Bool(FlagDoNotSetToken, false, fmt.Sprintf("When set to true, this command will not write to the %s environment variable.", EnvDAAuthToken)) +} + +func StartEVM() *cobra.Command { + cmd := &cobra.Command{ + Use: "start", + 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, args []string) error { + daToken, err := cmd.Flags().GetString(FlagDAAuthToken) + if err != nil { + return err + } + if daToken != "" { + os.Setenv(EnvDAAuthToken, daToken) + } + if os.Getenv(EnvDAAuthToken) == "" { + return fmt.Errorf("the environment variable %q was not set and the --%s flag was not used. "+ + "please supply the token by either the environment variable, or the flag", + EnvDAAuthToken, FlagDAAuthToken) + } + err = teacmd.DockerStart(true, false, false, 0, teacmd.DockerServiceEVM) + if err != nil { + fmt.Errorf("error starting %s docker container: %w", teacmd.DockerServiceEVM, err) + } + return nil + }, + } + cmd.Flags().String(FlagDAAuthToken, "", "DA Auth Token that allows the rollup to communicate with the Celestia client.") + return cmd +} diff --git a/cmd/world/root/doctor.go b/cmd/world/root/doctor.go index c687150..c63be7f 100644 --- a/cmd/world/root/doctor.go +++ b/cmd/world/root/doctor.go @@ -3,16 +3,15 @@ package root import ( tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" - "pkg.world.dev/world-cli/common/tea_cmd" "pkg.world.dev/world-cli/tea/style" ) -var DoctorDeps = []tea_cmd.Dependency{ - tea_cmd.GitDependency, - tea_cmd.GoDependency, - tea_cmd.DockerDependency, - tea_cmd.DockerComposeDependency, - tea_cmd.DockerDaemonDependency, +var DoctorDeps = []teacmd.Dependency{ + teacmd.GitDependency, + teacmd.GoDependency, + teacmd.DockerDependency, + teacmd.DockerComposeDependency, + teacmd.DockerDaemonDependency, } ///////////////// @@ -45,7 +44,7 @@ World CLI requires the following dependencies to be installed: ////////////////////// type WorldDoctorModel struct { - DepStatus []tea_cmd.DependencyStatus + DepStatus []teacmd.DependencyStatus DepStatusErr error } @@ -59,7 +58,7 @@ func NewWorldDoctorModel() WorldDoctorModel { // Init returns an initial command for the application to run func (m WorldDoctorModel) Init() tea.Cmd { - return tea_cmd.CheckDependenciesCmd(DoctorDeps) + return teacmd.CheckDependenciesCmd(DoctorDeps) } // Update handles incoming events and updates the model accordingly @@ -70,7 +69,7 @@ func (m WorldDoctorModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyCtrlC: return m, tea.Quit } - case tea_cmd.CheckDependenciesMsg: + case teacmd.CheckDependenciesMsg: m.DepStatus = msg.DepStatus m.DepStatusErr = msg.Err return m, tea.Quit @@ -80,7 +79,7 @@ func (m WorldDoctorModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // View renders the model to the screen func (m WorldDoctorModel) View() string { - depList, help := tea_cmd.PrintDependencyStatus(m.DepStatus) + depList, help := teacmd.PrintDependencyStatus(m.DepStatus) out := style.Container.Render("--- World CLI Doctor ---") + "\n\n" out += "Checking dependencies...\n" out += depList + "\n" + help + "\n" diff --git a/cmd/world/root/root.go b/cmd/world/root/root.go index ca94c09..9f97886 100644 --- a/cmd/world/root/root.go +++ b/cmd/world/root/root.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "os" "pkg.world.dev/world-cli/cmd/world/cardinal" + "pkg.world.dev/world-cli/cmd/world/evm" "pkg.world.dev/world-cli/tea/style" ) @@ -20,7 +21,9 @@ func init() { rootCmd.AddCommand(doctorCmd, versionCmd) // Register subcommands - rootCmd.AddCommand(cardinal.BaseCmd) + rootCmd.AddCommand(cardinal.BaseCmd()) + + rootCmd.AddCommand(evm.EVMCmds()) } // rootCmd represents the base command diff --git a/common/tea_cmd/dependencies.go b/common/teacmd/dependencies.go similarity index 99% rename from common/tea_cmd/dependencies.go rename to common/teacmd/dependencies.go index af3cddb..dea4c30 100644 --- a/common/tea_cmd/dependencies.go +++ b/common/teacmd/dependencies.go @@ -1,4 +1,4 @@ -package tea_cmd +package teacmd import ( "errors" diff --git a/common/tea_cmd/docker.go b/common/teacmd/docker.go similarity index 93% rename from common/tea_cmd/docker.go rename to common/teacmd/docker.go index 030e347..63622d6 100644 --- a/common/tea_cmd/docker.go +++ b/common/teacmd/docker.go @@ -1,4 +1,4 @@ -package tea_cmd +package teacmd import ( "fmt" @@ -15,6 +15,8 @@ const ( DockerServiceNakama DockerService = "nakama" DockerServicePostgres DockerService = "postgres" DockerServiceRedis DockerService = "redis" + DockerServiceEVM DockerService = "evm" + DockerServiceDA DockerService = "celestia-devnet" ) type DockerOp int @@ -54,7 +56,7 @@ func DockerCmd(action DockerCmdArgs) tea.Cmd { return DockerFinishMsg{Err: err, Operation: DockerOpBuild} case DockerOpStart: - err := DockerStart(action.Build, action.Debug, action.Detach, action.Timeout, action.Services) + err := DockerStart(action.Build, action.Debug, action.Detach, action.Timeout, action.Services...) return DockerFinishMsg{Err: err, Operation: DockerOpStart} case DockerOpRestart: @@ -89,7 +91,7 @@ func DockerBuild() error { // Rebuilds the image if `build` is true // Runs in detach mode if `detach` is true // Runs with the debug docker compose, if `debug` is true -func DockerStart(build bool, debug bool, detach bool, timeout int, services []DockerService) error { +func DockerStart(build bool, debug bool, detach bool, timeout int, services ...DockerService) error { if services == nil { return fmt.Errorf("no service names provided") } @@ -124,7 +126,7 @@ func DockerStart(build bool, debug bool, detach bool, timeout int, services []Do // DockerStartAll starts both cardinal and nakama func DockerStartAll(build bool, debug bool, detach bool, timeout int) error { return DockerStart(build, debug, detach, timeout, - []DockerService{DockerServiceCardinal, DockerServiceNakama, DockerServicePostgres, DockerServiceRedis}) + DockerServiceCardinal, DockerServiceNakama, DockerServicePostgres, DockerServiceRedis) } // DockerRestart restarts a given docker container by name, rebuilds the image if `build` is true @@ -136,7 +138,7 @@ func DockerRestart(build bool, services []DockerService) error { if err := DockerStop(services); err != nil { return err } - if err := DockerStart(build, false, false, -1, services); err != nil { + if err := DockerStart(build, false, false, -1, services...); err != nil { return err } } else { diff --git a/common/tea_cmd/git.go b/common/teacmd/git.go similarity index 98% rename from common/tea_cmd/git.go rename to common/teacmd/git.go index 2615c29..eb09c6c 100644 --- a/common/tea_cmd/git.go +++ b/common/teacmd/git.go @@ -1,4 +1,4 @@ -package tea_cmd +package teacmd import ( "bytes" diff --git a/go.mod b/go.mod index bd54d2f..918bc9e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module pkg.world.dev/world-cli -go 1.21 +go 1.21.0 require ( github.com/charmbracelet/bubbles v0.16.1 @@ -9,15 +9,14 @@ require ( github.com/magefile/mage v1.15.0 github.com/rs/zerolog v1.31.0 github.com/spf13/cobra v1.7.0 - github.com/stretchr/testify v1.7.0 ) require ( github.com/atotto/clipboard v0.1.4 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/stretchr/testify v1.8.4 // indirect ) require ( @@ -28,16 +27,16 @@ require ( github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-localereader v0.0.1 // indirect - github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.1 // indirect - github.com/rivo/uniseg v0.2.0 // indirect + github.com/rivo/uniseg v0.4.4 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/sync v0.1.0 // indirect + golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.13.0 // indirect - golang.org/x/term v0.6.0 // indirect - golang.org/x/text v0.3.8 // indirect + golang.org/x/term v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect gopkg.in/gookit/color.v1 v1.1.6 // indirect ) diff --git a/go.sum b/go.sum index 8280ff1..6ff58f8 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,8 @@ github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:Yyn github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/guumaster/logsymbols v0.3.1 h1:bnCE484dAQFvMWt2EfZAzF1oCgu8yo/Vp1QGQ0EmaAA= github.com/guumaster/logsymbols v0.3.1/go.mod h1:1M5/1js2Z7Yo8DRB3QrPURwqsXeOfgsJv1Utjookknw= @@ -33,8 +33,8 @@ github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= -github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= -github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= @@ -44,11 +44,13 @@ github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKt github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs= github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= @@ -59,10 +61,10 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -70,15 +72,13 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= -golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/gookit/color.v1 v1.1.6 h1:5fB10p6AUFjhd2ayq9JgmJWr9WlTrguFdw3qlYtKNHk= gopkg.in/gookit/color.v1 v1.1.6/go.mod h1:IcEkFGaveVShJ+j8ew+jwe9epHyGpJ9IrptHmW3laVY= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 4bcec502fbf7f0263f2c58f67cd0fd3d6bce4ddf Mon Sep 17 00:00:00 2001 From: tyler <48813565+technicallyty@users.noreply.github.com> Date: Wed, 22 Nov 2023 15:13:15 -0800 Subject: [PATCH 2/2] wip --- cmd/world/cardinal/create.go | 1 + cmd/world/cardinal/purge.go | 1 + cmd/world/cardinal/restart.go | 1 + cmd/world/cardinal/start.go | 1 + cmd/world/cardinal/stop.go | 1 + cmd/world/evm/evm.go | 75 +++++++++++++++++++++++++++++++++- cmd/world/root/doctor.go | 1 + common/teacmd/docker.go | 76 +++-------------------------------- 8 files changed, 84 insertions(+), 73 deletions(-) diff --git a/cmd/world/cardinal/create.go b/cmd/world/cardinal/create.go index edbe7f2..e2a9ab1 100644 --- a/cmd/world/cardinal/create.go +++ b/cmd/world/cardinal/create.go @@ -4,6 +4,7 @@ import ( "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" + "pkg.world.dev/world-cli/common/teacmd" "pkg.world.dev/world-cli/tea/component/steps" "pkg.world.dev/world-cli/tea/style" "strings" diff --git a/cmd/world/cardinal/purge.go b/cmd/world/cardinal/purge.go index 0ea492b..d0e0cd8 100644 --- a/cmd/world/cardinal/purge.go +++ b/cmd/world/cardinal/purge.go @@ -2,6 +2,7 @@ package cardinal import ( "github.com/spf13/cobra" + "pkg.world.dev/world-cli/common/teacmd" ) ///////////////// diff --git a/cmd/world/cardinal/restart.go b/cmd/world/cardinal/restart.go index 7b50cca..493c321 100644 --- a/cmd/world/cardinal/restart.go +++ b/cmd/world/cardinal/restart.go @@ -2,6 +2,7 @@ package cardinal import ( "github.com/spf13/cobra" + "pkg.world.dev/world-cli/common/teacmd" ) ///////////////// diff --git a/cmd/world/cardinal/start.go b/cmd/world/cardinal/start.go index 0f93a31..66f9a7d 100644 --- a/cmd/world/cardinal/start.go +++ b/cmd/world/cardinal/start.go @@ -3,6 +3,7 @@ package cardinal import ( "fmt" "github.com/spf13/cobra" + "pkg.world.dev/world-cli/common/teacmd" ) ///////////////// diff --git a/cmd/world/cardinal/stop.go b/cmd/world/cardinal/stop.go index 4611bc7..64109dd 100644 --- a/cmd/world/cardinal/stop.go +++ b/cmd/world/cardinal/stop.go @@ -2,6 +2,7 @@ package cardinal import ( "github.com/spf13/cobra" + "pkg.world.dev/world-cli/common/teacmd" ) ///////////////// diff --git a/cmd/world/evm/evm.go b/cmd/world/evm/evm.go index 5c21d4b..568b1a2 100644 --- a/cmd/world/evm/evm.go +++ b/cmd/world/evm/evm.go @@ -1,10 +1,13 @@ package evm import ( + "bytes" "fmt" "github.com/spf13/cobra" "os" + "os/exec" "pkg.world.dev/world-cli/common/teacmd" + "time" ) func EVMCmds() *cobra.Command { @@ -19,6 +22,7 @@ func EVMCmds() *cobra.Command { }) evmRootCmd.AddCommand( StartEVM(), + StartDA(), ) return evmRootCmd } @@ -39,14 +43,30 @@ func StartDA() *cobra.Command { if err != nil { return err } - err = teacmd.DockerStart(true, false, false, 0, teacmd.DockerServiceDA) + daService := teacmd.DockerServiceDA + fmt.Println("starting DA docker service...") + err = teacmd.DockerStart(true, false, true, -1, daService) if err != nil { - fmt.Errorf("error starting %s docker container: %w", teacmd.DockerServiceDA, err) + fmt.Errorf("error starting %s docker container: %w", daService, err) + } + time.Sleep(3 * time.Second) + fmt.Println("started DA service...") + if !dontSetToken { // yes, we want to set it! + fmt.Println("attempting to set DA token...") + daToken, err := getDAToken() + if err != nil { + return err + } + err = os.Setenv(EnvDAAuthToken, daToken) + if err != nil { + return fmt.Errorf("error setting environment variable %q to %q: %w", EnvDAAuthToken, daToken, err) + } } return nil }, } cmd.Flags().Bool(FlagDoNotSetToken, false, fmt.Sprintf("When set to true, this command will not write to the %s environment variable.", EnvDAAuthToken)) + return cmd } func StartEVM() *cobra.Command { @@ -77,3 +97,54 @@ func StartEVM() *cobra.Command { cmd.Flags().String(FlagDAAuthToken, "", "DA Auth Token that allows the rollup to communicate with the Celestia client.") return cmd } + +func getDAToken() (string, error) { + // Create a new command + cmd := exec.Command("docker", "logs", "celestia_devnet") + retry := 0 + for ; retry < 10; func() { + time.Sleep(2 * time.Second) + retry++ + }() { + fmt.Println("attempting to get DA token...") + // Create a buffer to store the command output + var outBuffer bytes.Buffer + cmd.Stdout = &outBuffer + + // Run the command + err := cmd.Run() + if err != nil { + fmt.Println("error running command docker logs: ", err) + continue + } + + // Convert the buffer to string + output := outBuffer.String() + + // Find the line containing CELESTIA_NODE_AUTH_TOKEN + var authTokenLine string + lines := bytes.Split([]byte(output), []byte("\n")) + for i, line := range lines { + if bytes.Contains(line, []byte("CELESTIA_NODE_AUTH_TOKEN")) { + // Get the next 5 lines after the match + endIndex := i + 6 + if endIndex > len(lines) { + endIndex = len(lines) + } + authTokenLines := lines[i:endIndex] + + // Concatenate the lines to get the final output + authTokenLine = string(bytes.Join(authTokenLines, []byte("\n"))) + break + } + } + + // Print the final output + if authTokenLine != "" { + fmt.Println("token found") + return authTokenLine, nil + } + fmt.Println("failed... trying again") + } + return "", fmt.Errorf("timed out while getting DA token") +} diff --git a/cmd/world/root/doctor.go b/cmd/world/root/doctor.go index c63be7f..6d11e6f 100644 --- a/cmd/world/root/doctor.go +++ b/cmd/world/root/doctor.go @@ -3,6 +3,7 @@ package root import ( tea "github.com/charmbracelet/bubbletea" "github.com/spf13/cobra" + "pkg.world.dev/world-cli/common/teacmd" "pkg.world.dev/world-cli/tea/style" ) diff --git a/common/teacmd/docker.go b/common/teacmd/docker.go index 63622d6..d9cf021 100644 --- a/common/teacmd/docker.go +++ b/common/teacmd/docker.go @@ -2,7 +2,6 @@ package teacmd import ( "fmt" - tea "github.com/charmbracelet/bubbletea" "github.com/magefile/mage/sh" "os" "strings" @@ -19,83 +18,18 @@ const ( DockerServiceDA DockerService = "celestia-devnet" ) -type DockerOp int - -const ( - DockerOpBuild DockerOp = iota - DockerOpStart - DockerOpRestart - DockerOpPurge - DockerOpStop -) - -type DockerCmdArgs struct { - Op DockerOp - Build bool - Debug bool - Detach bool - Timeout int - Services []DockerService -} - -type DockerFinishMsg struct { - Err error - Operation DockerOp -} - var dockerCompose = sh.RunCmd("docker", "compose") var dockerComposeDebug = sh.RunCmd("docker", "compose -f ./.run/docker-compose-debug.yml") -// DockerCmd returns a tea.Cmd that runs a docker command -func DockerCmd(action DockerCmdArgs) tea.Cmd { - return func() tea.Msg { - switch action.Op { - - case DockerOpBuild: - err := DockerBuild() - return DockerFinishMsg{Err: err, Operation: DockerOpBuild} - - case DockerOpStart: - err := DockerStart(action.Build, action.Debug, action.Detach, action.Timeout, action.Services...) - return DockerFinishMsg{Err: err, Operation: DockerOpStart} - - case DockerOpRestart: - err := DockerRestart(action.Build, action.Services) - return DockerFinishMsg{Err: err, Operation: DockerOpRestart} - - case DockerOpStop: - err := DockerStop(action.Services) - return DockerFinishMsg{Err: err, Operation: DockerOpStop} - - case DockerOpPurge: - err := DockerPurge() - return DockerFinishMsg{Err: err, Operation: DockerOpPurge} - } - - return nil - } -} - -// DockerBuild builds all docker images -func DockerBuild() error { - if err := prepareDirs("cardinal"); err != nil { - return err - } - if err := dockerCompose("build"); err != nil { - return err - } - return nil -} - // DockerStart starts a given docker container by name. // Rebuilds the image if `build` is true // Runs in detach mode if `detach` is true // Runs with the debug docker compose, if `debug` is true func DockerStart(build bool, debug bool, detach bool, timeout int, services ...DockerService) error { - if services == nil { + if len(services) == 0 { return fmt.Errorf("no service names provided") } - if err := prepareDirs("cardinal"); err != nil { + if err := tidyAndVendorDirs("cardinal"); err != nil { return err } @@ -192,16 +126,16 @@ func dockerArgs(args string, services []DockerService, flags ...string) []string return res } -func prepareDirs(dirs ...string) error { +func tidyAndVendorDirs(dirs ...string) error { for _, dir := range dirs { - if err := prepareDir(dir); err != nil { + if err := tidyAndVendorDir(dir); err != nil { return fmt.Errorf("failed to prepare dir %s: %w", dir, err) } } return nil } -func prepareDir(dir string) error { +func tidyAndVendorDir(dir string) error { if err := os.Chdir(dir); err != nil { return err }