From a83521220f9fd4646d7227895c6907385cec4835 Mon Sep 17 00:00:00 2001 From: zulkhair Date: Mon, 29 Apr 2024 21:26:39 +0700 Subject: [PATCH] fix find world.toml on parent dir --- cmd/world/cardinal/dev.go | 27 ++++++++++++++------------- cmd/world/cardinal/start.go | 2 +- cmd/world/root/create.go | 2 +- common/config/config.go | 2 +- common/teacmd/editor.go | 8 ++++---- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/cmd/world/cardinal/dev.go b/cmd/world/cardinal/dev.go index 751e074..064b05b 100644 --- a/cmd/world/cardinal/dev.go +++ b/cmd/world/cardinal/dev.go @@ -19,6 +19,7 @@ import ( "github.com/rotisserie/eris" "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" "pkg.world.dev/world-cli/tea/style" @@ -56,6 +57,11 @@ var devCmd = &cobra.Command{ noEditor, _ := cmd.Flags().GetBool(flagNoEditor) logger.SetDebugMode(cmd) + cfg, err := config.GetConfig(cmd) + if err != nil { + return err + } + startingMessage := "Running Cardinal in dev mode" if watch { startingMessage += " with hot reload support" @@ -82,7 +88,7 @@ var devCmd = &cobra.Command{ fmt.Println("Preparing Cardinal Editor...") cePrepChan := make(chan struct{}) go func() { - err := runCardinalEditor(cardinalEditorPort, cePrepChan) + err := runCardinalEditor(cfg.RootDir, cardinalEditorPort, cePrepChan) if err != nil { cmdStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("11")) fmt.Println(cmdStyle.Render("Warning: Failed to run Cardinal Editor")) @@ -98,7 +104,7 @@ var devCmd = &cobra.Command{ fmt.Println() // Run Redis container - err := runRedis() + err = runRedis() if err != nil { return err } @@ -125,7 +131,7 @@ var devCmd = &cobra.Command{ signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) // Run Cardinal Preparation - err = runCardinalPrep() + err = runCardinalPrep(cfg.RootDir) if err != nil { return err } @@ -213,8 +219,8 @@ func runRedis() error { // runCardinalPrep preparation for runs cardinal in dev mode. // We run cardinal without docker to make it easier to debug and skip the docker image build step -func runCardinalPrep() error { - err := os.Chdir("cardinal") +func runCardinalPrep(rootDir string) error { + err := os.Chdir(filepath.Join(rootDir, "cardinal")) if err != nil { return errors.New("can't find cardinal directory. Are you in the root of a World Engine project") } @@ -299,16 +305,11 @@ func cleanup() error { } // runCardinalEditor runs the Cardinal Editor -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) +func runCardinalEditor(rootDir string, port int, prepChan chan struct{}) error { + cardinalEditorDir := filepath.Join(rootDir, teacmd.TargetEditorDir) // Setup cardinal editor - err = teacmd.SetupCardinalEditor() + err := teacmd.SetupCardinalEditor(rootDir) if err != nil { prepChan <- struct{}{} return err diff --git a/cmd/world/cardinal/start.go b/cmd/world/cardinal/start.go index 19d76aa..967d662 100644 --- a/cmd/world/cardinal/start.go +++ b/cmd/world/cardinal/start.go @@ -109,7 +109,7 @@ This will start the following Docker services and its dependencies: fmt.Println(cmdBlue.Render(fmt.Sprint("Cardinal Editor will be run on localhost:", cardinalEditorPort))) cePrepChan := make(chan struct{}) go func() { - err := runCardinalEditor(cardinalEditorPort, cePrepChan) + err := runCardinalEditor(cfg.RootDir, cardinalEditorPort, cePrepChan) if err != nil { cmdStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("11")) fmt.Println(cmdStyle.Render("Warning: Failed to run Cardinal Editor")) diff --git a/cmd/world/root/create.go b/cmd/world/root/create.go index 6084203..ea242c3 100644 --- a/cmd/world/root/create.go +++ b/cmd/world/root/create.go @@ -124,7 +124,7 @@ func (m WorldCreateModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { ) } if msg.Index == 2 { //nolint:gomnd - err := teacmd.SetupCardinalEditor() + err := teacmd.SetupCardinalEditor(".") teaCmd := func() tea.Msg { return teacmd.GitCloneFinishMsg{Err: err} } diff --git a/common/config/config.go b/common/config/config.go index 31b2a52..ec45444 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -101,7 +101,7 @@ func loadConfigFromFile(filename string) (*Config, error) { } file, err := os.Open(filename) if err != nil { - return nil, fmt.Errorf("failed to open %q: %w", filename, err) + return nil, err } defer file.Close() diff --git a/common/teacmd/editor.go b/common/teacmd/editor.go index 62849ac..215094b 100644 --- a/common/teacmd/editor.go +++ b/common/teacmd/editor.go @@ -31,8 +31,6 @@ const ( cardinalProjectIDPlaceholder = "__CARDINAL_PROJECT_ID__" - cardinalGomodPath = "cardinal/go.mod" - cardinalPkgPath = "pkg.world.dev/world-engine/cardinal" versionMapURL = "https://raw.githubusercontent.com/Argus-Labs/cardinal-editor/main/version_map.json" @@ -47,6 +45,8 @@ var ( "v1.2.4-beta": "v0.3.1", "v1.2.5-beta": "v0.3.1", } + + cardinalGomodPath = filepath.Join("cardinal", "go.mod") ) type Asset struct { @@ -58,7 +58,7 @@ type Release struct { Assets []Asset `json:"assets"` } -func SetupCardinalEditor() error { +func SetupCardinalEditor(rootDir string) error { // Get the version map cardinalVersionMap, err := getVersionMap(versionMapURL) if err != nil { @@ -67,7 +67,7 @@ func SetupCardinalEditor() error { } // Check version - cardinalVersion, err := getModuleVersion(cardinalGomodPath, cardinalPkgPath) + cardinalVersion, err := getModuleVersion(filepath.Join(rootDir, cardinalGomodPath), cardinalPkgPath) if err != nil { return eris.Wrap(err, "failed to get cardinal version") }