Skip to content

Commit

Permalink
Create workspace in the training root
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Feb 24, 2024
1 parent 49b7528 commit 0e58f42
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions trainings/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (h *Handlers) Clone(ctx context.Context, executionID string) error {
return errors.Wrap(err, "can't write training config")
}

_ = addModuleToWorkspace(pwd, resp.Dir)

files := &genproto.NextExerciseResponse{
TrainingStatus: genproto.NextExerciseResponse_IN_PROGRESS,
Dir: resp.Dir,
Expand Down
35 changes: 35 additions & 0 deletions trainings/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path"
"strings"

"github.com/ThreeDotsLabs/cli/trainings/files"
Expand Down Expand Up @@ -76,6 +78,8 @@ func (h *Handlers) startTraining(ctx context.Context, trainingName string) error
cfg.TrainingName,
)
}
} else {
_ = createGoWorkspace(trainingRoot)
}

_, err = h.newGrpcClient(ctx).StartTraining(
Expand Down Expand Up @@ -124,6 +128,37 @@ func writeGitignore(trainingRootFs *afero.BasePathFs) error {
return nil
}

func createGoWorkspace(trainingRoot string) error {
cmd := exec.Command("go", "work", "init")
cmd.Dir = trainingRoot

if err := cmd.Run(); err != nil {
return errors.Wrap(err, "can't run go work init")
}

return nil
}

func hasGoWorkspace(trainingRoot string) bool {
_, err := os.Stat(path.Join(trainingRoot, "go.work"))
return err == nil
}

func addModuleToWorkspace(trainingRoot string, modulePath string) error {
if !hasGoWorkspace(trainingRoot) {
return nil
}

cmd := exec.Command("go", "work", "use", ".")
cmd.Dir = path.Join(trainingRoot, modulePath)

if err := cmd.Run(); err != nil {
return errors.Wrap(err, "can't run go work use")
}

return nil
}

func (h *Handlers) showTrainingStartPrompt() error {
pwd, err := os.Getwd()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions trainings/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func (h *Handlers) nextExercise(ctx context.Context, currentExerciseID string) (
return false, err
}

_ = addModuleToWorkspace(trainingRoot, resp.Dir)

if resp.IsTextOnly {
printTextOnlyExerciseInfo(
h.config.TrainingConfig(trainingRootFs).TrainingName,
Expand Down

0 comments on commit 0e58f42

Please sign in to comment.