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 8, 2024
1 parent 0ea5303 commit 271a9de
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
29 changes: 15 additions & 14 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, cfg.GameDir, 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, cfg.GameDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -213,10 +219,10 @@ 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, gameDir string) error {
err := os.Chdir(filepath.Join(rootDir, gameDir))
if err != nil {
return errors.New("can't find cardinal directory. Are you in the root of a World Engine project")
return errors.New("can't find game directory. Are you in the root of a World Engine project")
}

env := map[string]string{
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, gameDir string, port int, prepChan chan struct{}) error {
cardinalEditorDir := filepath.Join(rootDir, teacmd.TargetEditorDir)

// Setup cardinal editor
err = teacmd.SetupCardinalEditor()
err := teacmd.SetupCardinalEditor(rootDir, gameDir)
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, cfg.GameDir, 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(".", "cardinal")
teaCmd := func() tea.Msg {
return teacmd.GitCloneFinishMsg{Err: err}
}
Expand Down
13 changes: 12 additions & 1 deletion common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (

type Config struct {
RootDir string
GameDir string
Detach bool
Build bool
Debug bool
Expand Down Expand Up @@ -101,7 +102,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 All @@ -118,6 +119,16 @@ func loadConfigFromFile(filename string) (*Config, error) {
cfg.RootDir, _ = filepath.Split(filename)
}

// Load the game directory.
if gameDir, ok := data["game_dir"]; ok {
cfg.GameDir, ok = gameDir.(string)
if !ok {
return nil, errors.New("game_dir must be a string")

Check warning on line 126 in common/config/config.go

View check run for this annotation

Codecov / codecov/patch

common/config/config.go#L124-L126

Added lines #L124 - L126 were not covered by tests
}
} else {
cfg.GameDir = "cardinal"
}

for _, header := range dockerEnvHeaders {
m, ok := data[header]
if !ok {
Expand Down
6 changes: 2 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 @@ -58,7 +56,7 @@ type Release struct {
Assets []Asset `json:"assets"`
}

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

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

View check run for this annotation

Codecov / codecov/patch

common/teacmd/editor.go#L59

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

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

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

View check run for this annotation

Codecov / codecov/patch

common/teacmd/editor.go#L68

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

0 comments on commit 271a9de

Please sign in to comment.