Skip to content

Commit

Permalink
Merge pull request #13 from ThreeDotsLabs/workspace
Browse files Browse the repository at this point in the history
Add support for workspaces
  • Loading branch information
m110 authored Feb 24, 2024
2 parents 31211a8 + b040170 commit 6f8cbe4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
5 changes: 5 additions & 0 deletions trainings/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func (h *Handlers) Clone(ctx context.Context, executionID string) error {
return errors.Wrap(err, "can't write training config")
}

err = addModuleToWorkspace(pwd, resp.Dir)
if err != nil {
logrus.WithError(err).Warn("Failed to add module to workspace")
}

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

"github.com/ThreeDotsLabs/cli/internal"
"github.com/ThreeDotsLabs/cli/trainings/config"
"github.com/ThreeDotsLabs/cli/trainings/files"
"github.com/ThreeDotsLabs/cli/trainings/genproto"
"github.com/fatih/color"
"github.com/spf13/afero"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/ThreeDotsLabs/cli/internal"
"github.com/ThreeDotsLabs/cli/trainings/config"
"github.com/ThreeDotsLabs/cli/trainings/genproto"
"github.com/spf13/afero"
)

func (h *Handlers) Init(ctx context.Context, trainingName string) error {
Expand Down Expand Up @@ -76,6 +76,11 @@ func (h *Handlers) startTraining(ctx context.Context, trainingName string) error
cfg.TrainingName,
)
}
} else {
err = createGoWorkspace(trainingRoot)
if err != nil {
logrus.WithError(err).Warn("Could not create go workspace")
}
}

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

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

printlnCommand(".", "go work init")

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", modulePath)
cmd.Dir = trainingRoot

printlnCommand(".", fmt.Sprintf("go work use %v", 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
10 changes: 7 additions & 3 deletions trainings/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package trainings
import (
"context"

"github.com/ThreeDotsLabs/cli/trainings/config"
"github.com/ThreeDotsLabs/cli/trainings/files"
"github.com/ThreeDotsLabs/cli/trainings/genproto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"

"github.com/ThreeDotsLabs/cli/trainings/config"
"github.com/ThreeDotsLabs/cli/trainings/genproto"
)

func (h *Handlers) nextExercise(ctx context.Context, currentExerciseID string) (finished bool, err error) {
Expand Down Expand Up @@ -50,6 +49,11 @@ func (h *Handlers) nextExercise(ctx context.Context, currentExerciseID string) (
h.config.TrainingConfig(trainingRootFs).TrainingName,
resp.ExerciseId,
)
} else {
err = addModuleToWorkspace(trainingRoot, resp.Dir)
if err != nil {
logrus.WithError(err).Warn("Failed to add module to workspace")
}
}

return false, nil
Expand Down
10 changes: 9 additions & 1 deletion trainings/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (h *Handlers) runExercise(ctx context.Context, trainingRootFs *afero.BasePa
}

if len(response.Command) > 0 {
fmt.Print(color.CyanString(fmt.Sprintf("••• %s ➜ ", terminalPath)) + response.Command)
printCommand(terminalPath, response.Command)
}
if len(response.Stdout) > 0 {
fmt.Print(response.Stdout)
Expand Down Expand Up @@ -266,6 +266,14 @@ func (h *Handlers) runExercise(ctx context.Context, trainingRootFs *afero.BasePa
}
}

func printCommand(root string, command string) {
fmt.Print(color.CyanString(fmt.Sprintf("••• %s ➜ ", root)) + command)
}

func printlnCommand(root string, command string) {
printCommand(root, command+"\n")
}

func (h *Handlers) generateRunTerminalPath(trainingRootFs *afero.BasePathFs) string {
exerciseConfig := h.config.ExerciseConfig(trainingRootFs)

Expand Down

0 comments on commit 6f8cbe4

Please sign in to comment.