-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ThreeDotsLabs/region-select
Add --region flag
- Loading branch information
Showing
10 changed files
with
454 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) - recommended | ||
|
||
```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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package internal | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func GlobalConfigDir() string { | ||
userConfigDir, err := os.UserConfigDir() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return filepath.Join(userConfigDir, "three-dots-labs") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/fatih/color" | ||
"net/http" | ||
"os" | ||
"path" | ||
"time" | ||
) | ||
|
||
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) { | ||
if currentVersion == "" || currentVersion == "dev" { | ||
return | ||
} | ||
|
||
lastUpdate, _ := LastUpdateCheckTime() | ||
|
||
if time.Since(lastUpdate) < 24*time.Hour { | ||
return | ||
} | ||
|
||
defer func() { | ||
_ = StoreUpdateCheckTime(time.Now().UTC()) | ||
}() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, releasesURL, nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
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() | ||
} | ||
} | ||
|
||
func lastUpdateCheckPath() string { | ||
return path.Join(GlobalConfigDir(), "last-update-check") | ||
} | ||
|
||
func LastUpdateCheckTime() (time.Time, error) { | ||
if !fileExists(lastUpdateCheckPath()) { | ||
return time.Time{}, nil | ||
} | ||
|
||
content, err := os.ReadFile(lastUpdateCheckPath()) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
t, err := time.Parse(time.RFC3339, string(content)) | ||
if err != nil { | ||
return time.Time{}, err | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func StoreUpdateCheckTime(t time.Time) error { | ||
return os.WriteFile(lastUpdateCheckPath(), []byte(t.Format(time.RFC3339)), 0644) | ||
} | ||
|
||
func fileExists(path string) bool { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.