Skip to content

Commit

Permalink
chore: setup cardinal editor on dev mode (#55)
Browse files Browse the repository at this point in the history
Closes: WORLD-1017

## Overview

Setup cardinal editor if `.editor` is not exist

## Brief Changelog
- add channel for waiting cardinal editor set up
- add step for checking `.editor` dir and setup cardinal editor

## Testing and Verifying

manually tested using `world cardinal dev` command
  • Loading branch information
zulkhair committed Apr 2, 2024
1 parent 3d39862 commit 0dfd522
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -292,6 +307,9 @@ func runCardinalEditor(port int) error {
ReadTimeout: ceReadTimeout,
}

// Preparation done
prepChan <- struct{}{}

// Start the server
return server.ListenAndServe()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0dfd522

Please sign in to comment.