diff --git a/cmd/world/cardinal/dev.go b/cmd/world/cardinal/dev.go index b590090..5a7c7f1 100644 --- a/cmd/world/cardinal/dev.go +++ b/cmd/world/cardinal/dev.go @@ -16,6 +16,7 @@ import ( "github.com/argus-labs/fresh/runner" "github.com/charmbracelet/lipgloss" "github.com/magefile/mage/sh" + "github.com/rotisserie/eris" "github.com/spf13/cobra" "pkg.world.dev/world-cli/common/logger" @@ -73,13 +74,22 @@ var devCmd = &cobra.Command{ // Run Cardinal Editor // Cardinal will not blocking the process if it's failed to run + // cePrepChan is channel for blocking process while setup cardinal editor + fmt.Println("Preparing Cardinal Editor...") + cePrepChan := make(chan struct{}) go func() { - err := runCardinalEditor(cardinalEditorPort) + err := runCardinalEditor(cardinalEditorPort, cePrepChan) if err != nil { cmdStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("11")) fmt.Println(cmdStyle.Render("Warning: Failed to run Cardinal Editor")) + logger.Error(eris.Wrap(err, "Failed to run Cardinal Editor")) + + // continue if error + cePrepChan <- struct{}{} } }() + // Waiting cardinal editor preparation + <-cePrepChan // Run Redis container err := runRedis() @@ -267,9 +277,10 @@ func cleanup() error { } // runCardinalEditor runs the Cardinal Editor -func runCardinalEditor(port int) error { +func runCardinalEditor(port int, prepChan chan struct{}) error { workingDir, err := os.Getwd() if err != nil { + prepChan <- struct{}{} return err } cardinalEditorDir := filepath.Join(workingDir, teacmd.TargetEditorDir) @@ -278,8 +289,12 @@ func runCardinalEditor(port int) error { // Check if the Cardinal Editor directory exists if _, err := os.Stat(cardinalEditorDir); os.IsNotExist(err) { - // TODO: setup cardinal editor - return errors.New("cardinal editor dir is not found") + // Setup cardinal editor + err = teacmd.SetupCardinalEditor() + if err != nil { + prepChan <- struct{}{} + return err + } } // Serve cardinal editor dir @@ -292,6 +307,9 @@ func runCardinalEditor(port int) error { ReadTimeout: ceReadTimeout, } + // Preparation done + prepChan <- struct{}{} + // Start the server return server.ListenAndServe() } diff --git a/go.mod b/go.mod index f20c5f2..baf7f00 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/charmbracelet/bubbletea v0.24.2 github.com/denisbrodbeck/machineid v1.0.1 github.com/getsentry/sentry-go v0.26.0 + github.com/google/uuid v1.3.0 github.com/guumaster/logsymbols v0.3.1 github.com/hashicorp/go-version v1.6.0 github.com/magefile/mage v1.15.0 @@ -23,7 +24,6 @@ require ( require ( github.com/atotto/clipboard v0.1.4 // indirect github.com/google/go-cmp v0.5.9 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/howeyc/fsnotify v0.9.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect