Skip to content

Commit

Permalink
print
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Feb 24, 2024
1 parent 061d693 commit b040170
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
5 changes: 4 additions & 1 deletion trainings/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func (h *Handlers) Clone(ctx context.Context, executionID string) error {
return errors.Wrap(err, "can't write training config")
}

_ = addModuleToWorkspace(pwd, resp.Dir)
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,
Expand Down
23 changes: 14 additions & 9 deletions trainings/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import (
"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 @@ -79,7 +77,10 @@ func (h *Handlers) startTraining(ctx context.Context, trainingName string) error
)
}
} else {
_ = createGoWorkspace(trainingRoot)
err = createGoWorkspace(trainingRoot)
if err != nil {
logrus.WithError(err).Warn("Could not create go workspace")
}
}

_, err = h.newGrpcClient(ctx).StartTraining(
Expand Down Expand Up @@ -132,6 +133,8 @@ 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")
}
Expand All @@ -149,8 +152,10 @@ func addModuleToWorkspace(trainingRoot string, modulePath string) error {
return nil
}

cmd := exec.Command("go", "work", "use", ".")
cmd.Dir = path.Join(trainingRoot, modulePath)
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")
Expand Down
5 changes: 4 additions & 1 deletion trainings/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (h *Handlers) nextExercise(ctx context.Context, currentExerciseID string) (
resp.ExerciseId,
)
} else {
_ = addModuleToWorkspace(trainingRoot, resp.Dir)
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 b040170

Please sign in to comment.