Skip to content

Commit

Permalink
fix find world.toml on parent dir
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed May 2, 2024
1 parent 0ea5303 commit a835212
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
27 changes: 14 additions & 13 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"))
Expand All @@ -98,7 +104,7 @@ var devCmd = &cobra.Command{
fmt.Println()

// Run Redis container
err := runRedis()
err = runRedis()
if err != nil {
return err
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/world/cardinal/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/world/root/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
2 changes: 1 addition & 1 deletion common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions common/teacmd/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -58,7 +58,7 @@ type Release struct {
Assets []Asset `json:"assets"`
}

func SetupCardinalEditor() error {
func SetupCardinalEditor(rootDir string) error {

Check warning on line 61 in common/teacmd/editor.go

View check run for this annotation

Codecov / codecov/patch

common/teacmd/editor.go#L61

Added line #L61 was not covered by tests
// Get the version map
cardinalVersionMap, err := getVersionMap(versionMapURL)
if err != nil {
Expand All @@ -67,7 +67,7 @@ func SetupCardinalEditor() error {
}

// Check version
cardinalVersion, err := getModuleVersion(cardinalGomodPath, cardinalPkgPath)
cardinalVersion, err := getModuleVersion(filepath.Join(rootDir, cardinalGomodPath), cardinalPkgPath)

Check warning on line 70 in common/teacmd/editor.go

View check run for this annotation

Codecov / codecov/patch

common/teacmd/editor.go#L70

Added line #L70 was not covered by tests
if err != nil {
return eris.Wrap(err, "failed to get cardinal version")
}
Expand Down

0 comments on commit a835212

Please sign in to comment.