Skip to content

Commit

Permalink
Merge pull request #11 from ThreeDotsLabs/added-tdl-clone
Browse files Browse the repository at this point in the history
Added tdl clone
  • Loading branch information
roblaszczak authored Feb 23, 2024
2 parents 8415c85 + 2192c53 commit 599b48a
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 209 deletions.
3 changes: 2 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ builds:
- "amd64"
- "arm64"
- "arm"
- "386"
goarm:
- "6"
- "7"
Expand All @@ -30,6 +29,8 @@ builds:
goarm: "7"
- goos: windows
goarch: arm
- goos: freebsd
goarch: arm
flags:
- -trimpath
ldflags:
Expand Down
29 changes: 17 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
module github.com/ThreeDotsLabs/cli

go 1.17
go 1.20

require (
github.com/BurntSushi/toml v0.4.1
github.com/fatih/color v1.12.0
github.com/golang/protobuf v1.5.2
github.com/golang/protobuf v1.5.3
github.com/hexops/gotextdiff v1.0.3
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.6.0
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.27.1
golang.org/x/crypto v0.18.0
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
173 changes: 43 additions & 130 deletions go.sum

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions tdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ var app = &cli.App{
return newHandlers(c).List(c.Context)
},
},
{
Name: "clone",
Usage: "clone solution files to current directory",
ArgsUsage: fmt.Sprintf(
"[executionID from 'Share your solution' at %s]",
internal.WebsiteAddress,
),
Action: func(c *cli.Context) error {
executionID := c.Args().First()
if executionID == "" {
return missingArgumentError{"Missing executionID argument"}
}

return newHandlers(c).Clone(c.Context, executionID)
},
},
},
},
{
Expand Down
13 changes: 13 additions & 0 deletions trainings/api/protobuf/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ service Trainings {
rpc NextExercise(NextExerciseRequest) returns (NextExerciseResponse) {};

rpc VerifyExercise(VerifyExerciseRequest) returns (stream VerifyExerciseResponse) {};

rpc GetSolutionFiles(GetSolutionFilesRequest) returns (GetSolutionFilesResponse) {};
}

message InitRequest {
Expand Down Expand Up @@ -91,3 +93,14 @@ message VerifyExerciseResponse {

map<string, string> metadata = 7;
}

message GetSolutionFilesRequest {
string execution_id = 1;
}

message GetSolutionFilesResponse {
string training_name = 1;
string dir = 2;
string exercise_id = 3;
repeated File files_to_create = 4;
}
56 changes: 56 additions & 0 deletions trainings/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package trainings

import (
"context"
"fmt"
"os"

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

func (h *Handlers) Clone(ctx context.Context, executionID string) error {
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}

resp, err := h.newGrpcClient(ctx).GetSolutionFiles(ctx, &genproto.GetSolutionFilesRequest{
ExecutionId: executionID,
})
if err != nil {
return fmt.Errorf("failed to get solution files: %w", err)
}

logrus.WithFields(logrus.Fields{
"resp": resp,
"err": err,
}).Debug("Received exercise from server")

if err := h.startTraining(ctx, resp.TrainingName); err != nil {
return err
}

trainingRootFs := afero.NewBasePathFs(afero.NewOsFs(), pwd).(*afero.BasePathFs)

if err := h.config.WriteTrainingConfig(config.TrainingConfig{TrainingName: resp.TrainingName}, trainingRootFs); err != nil {
return errors.Wrap(err, "can't write training config")
}

files := &genproto.NextExerciseResponse{
TrainingStatus: genproto.NextExerciseResponse_IN_PROGRESS,
Dir: resp.Dir,
ExerciseId: resp.ExerciseId,
FilesToCreate: resp.FilesToCreate,
IsTextOnly: false,
}

if err := h.writeExerciseFiles(files, trainingRootFs); err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions trainings/files/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (f Files) WriteExerciseFiles(filesToCreate []*genproto.File, trainingRootFs
}
}

logrus.WithFields(logrus.Fields{
"exercise_dir": exerciseDir,
"files_num": len(filesToCreate),
}).Debugf("Writing exercise files to %s", exerciseDir)

var savedFiles []savedFile

for _, fileFromServer := range filesToCreate {
Expand Down
Loading

0 comments on commit 599b48a

Please sign in to comment.