Skip to content

Commit

Permalink
Add support for region selection
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Feb 21, 2024
1 parent 57242d5 commit 062e743
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 195 deletions.
39 changes: 39 additions & 0 deletions README.md
@@ -0,0 +1,39 @@
# tdl

This is the `tdl` CLI tool used for running the interactive trainings on the [Three Dots Labs Academy](https://academy.threedots.tech).

## Install

### Script (macOS, Linux)

```sh
sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/ThreeDotsLabs/cli/master/install.sh)" -- -b /usr/local/bin```
```
### Script (Windows)
Install to your home directory:
```sh
iwr https://raw.githubusercontent.com/ThreeDotsLabs/cli/master/install.ps1 -useb | iex
```

Or install in any chosen path:

```sh
$env:TDL_INSTALL = 'bin\'
iwr https://raw.githubusercontent.com/ThreeDotsLabs/cli/master/install.ps1 -useb | iex
```

### Binaries

Download the latest binary from GitHub and move it to a directory in your `$PATH`.

[See Releases](https://github.com/ThreeDotsLabs/cli/releases)

### From source

```sh
go install github.com/ThreeDotsLabs/cli/tdl@latest
```

42 changes: 42 additions & 0 deletions internal/update.go
@@ -0,0 +1,42 @@
package internal

import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"net/http"
)

type releaseResponse struct {
TagName string `json:"tag_name"`
}

const repoURL = "https://github.com/ThreeDotsLabs/cli"
const releasesURL = "https://api.github.com/repos/ThreeDotsLabs/cli/releases/latest"

func CheckForUpdate(currentVersion string) {
currentVersion = "v0.1.26"
if currentVersion == "" || currentVersion == "dev" {
return
}

resp, err := http.Get(releasesURL)
if err != nil {
return
}
defer func() {
_ = resp.Body.Close()
}()

var release releaseResponse
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return
}

if release.TagName != currentVersion {
c := color.New(color.FgHiYellow)
_, _ = c.Printf("A new version is available: %s (current: %s)\n", release.TagName, currentVersion)
_, _ = c.Printf("Visit %v to update\n", repoURL)
fmt.Println()
}
}
8 changes: 7 additions & 1 deletion tdl/main.go
Expand Up @@ -50,7 +50,10 @@ var app = &cli.App{
return
}

fmt.Printf("%+v\n", err)
if err != nil {
fmt.Printf("%+v\n", err)
}

os.Exit(1)
},
Flags: []cli.Flag{
Expand All @@ -67,6 +70,9 @@ var app = &cli.App{
} else {
logrus.SetLevel(logrus.WarnLevel)
}

internal.CheckForUpdate(version)

return nil
},
Commands: []*cli.Command{
Expand Down
6 changes: 5 additions & 1 deletion trainings/api/protobuf/server.proto
Expand Up @@ -4,7 +4,7 @@ import "google/protobuf/empty.proto";
option go_package = "github.com/ThreeDotsLabs/cli/tdl-cli/trainings/genproto";

service Trainings {
rpc Init(InitRequest) returns (google.protobuf.Empty) {};
rpc Init(InitRequest) returns (InitResponse) {};

rpc GetTrainings(google.protobuf.Empty) returns (GetTrainingsResponse) {};
rpc StartTraining(StartTrainingRequest) returns (google.protobuf.Empty) {};
Expand All @@ -18,6 +18,10 @@ message InitRequest {
string token = 1;
}

message InitResponse {
string region = 1;
}

message Training {
string id = 1;
}
Expand Down
9 changes: 7 additions & 2 deletions trainings/configure.go
Expand Up @@ -19,15 +19,20 @@ func (h *Handlers) ConfigureGlobally(ctx context.Context, token, serverAddr, reg
}
}

if _, err := h.newGrpcClientWithAddr(ctx, serverAddr, region, insecure).Init(
resp, err := h.newGrpcClientWithAddr(ctx, serverAddr, region, insecure).Init(
ctxWithRequestHeader(ctx, h.cliMetadata),
&genproto.InitRequest{
Token: token,
},
); err != nil {
)
if err != nil {
return err
}

if region == "" {
region = resp.Region
}

return h.config.WriteGlobalConfig(config.GlobalConfig{
Token: token,
ServerAddr: serverAddr,
Expand Down

0 comments on commit 062e743

Please sign in to comment.