-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wip: update cmd, check, fetch & extract release asset
- Loading branch information
1 parent
03bd334
commit d5d7439
Showing
6 changed files
with
282 additions
and
15 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,118 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Privado-Inc/privado/pkg/config" | ||
"github.com/Privado-Inc/privado/pkg/utils" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
var updateCmd = &cobra.Command{ | ||
Use: "update", | ||
Short: "Check for latest release and update to the latest version Privado CLI", | ||
Long: "Check for latest release and update to the latest version Privado CLI", | ||
Args: cobra.ExactArgs(0), | ||
Run: update, | ||
} | ||
|
||
func update(cmd *cobra.Command, args []string) { | ||
version(cmd, args) | ||
fmt.Println() | ||
|
||
// if Version == "dev" { | ||
// exit( | ||
// fmt.Sprint("Cannot perform an update on the dev build. Kindly use a release build or update manually\nFor more information, visit ", config.AppConfig.PrivadoRepository), | ||
// false, | ||
// ) | ||
// } | ||
fmt.Println("Fetching latest release..") | ||
releaseInfo, err := utils.GetLatestReleaseFromGitHub(config.AppConfig.PrivadoRepositoryName) | ||
if releaseInfo.TagName == "" || releaseInfo.PublishedAt == "" || err != nil { | ||
exit("Could not fetch latest release. Some error occurred", true) | ||
} | ||
// Version = "v0.1" | ||
if semver.Compare(releaseInfo.TagName, Version) < 1 { | ||
exit(fmt.Sprint("You are already using the latest version of Privado CLI: ", Version), false) | ||
} | ||
|
||
// timeSinceRelease := int(time.Since(publishedAtTime.Local()).Hours() / 24) | ||
|
||
// fmt.Printf("New release found: %s (Released %d days ago)\n", releaseInfo.TagName, timeSinceRelease) | ||
time.Sleep(config.AppConfig.SlowdownTime) | ||
|
||
// create temporary directory for update assets | ||
temporaryDirectory, err := ioutil.TempDir("", "privado-update-") | ||
if err != nil { | ||
exit("Could not create temporary download file. Terminating..", true) | ||
} | ||
|
||
// get download url | ||
replacer := strings.NewReplacer( | ||
"${REPO_NAME}", config.AppConfig.PrivadoRepositoryName, | ||
"${REPO_TAG}", "latest", | ||
"${REPO_RELEASE_FILE}", config.AppConfig.PrivadoRepositoryReleaseFilename, | ||
) | ||
githubReleaseDownloadURL := replacer.Replace(config.ExtConfig.GitHubReleaseDownloadURL) | ||
|
||
// download to file | ||
downloadedFilePath := filepath.Join(temporaryDirectory, config.AppConfig.PrivadoRepositoryReleaseFilename) | ||
err = utils.DownloadToFile(githubReleaseDownloadURL, downloadedFilePath) | ||
if err != nil { | ||
exit(fmt.Sprint("Could not download release asset: ", githubReleaseDownloadURL), true) | ||
} | ||
fmt.Println("Downloaded release asset:", githubReleaseDownloadURL) | ||
time.Sleep(config.AppConfig.SlowdownTime) | ||
|
||
// extract .tar.gz | ||
fmt.Println() | ||
fmt.Println("Extracting release asset..") | ||
err = utils.ExtractTarGzFile(downloadedFilePath, temporaryDirectory) | ||
if err != nil { | ||
exit(fmt.Sprint("Could not extract release asset: ", githubReleaseDownloadURL, err), true) | ||
} | ||
|
||
fmt.Println("Extracted release asset:", temporaryDirectory) | ||
time.Sleep(config.AppConfig.SlowdownTime) | ||
|
||
fmt.Println("githubReleaseFileURL", githubReleaseDownloadURL, temporaryDirectory) | ||
|
||
printVersion := Version | ||
if Version == "dev" { | ||
printVersion = "Nightly" | ||
} | ||
fmt.Printf("Privado CLI: Version %s (%s-%s) \n", printVersion, runtime.GOOS, runtime.GOARCH) | ||
fmt.Println("For more information, visit", config.AppConfig.PrivadoRepository) | ||
|
||
file, _ := os.Executable() | ||
fmt.Println(file) | ||
final, _ := filepath.EvalSymlinks(file) | ||
fmt.Println(final) | ||
|
||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(updateCmd) | ||
} | ||
|
||
// update logic | ||
// Check for latest version tag | ||
// Get the latest tag | ||
// Compare if new available | ||
// Prompt user | ||
// if yes: download binary for respective os & arch | ||
// Get location of current binary | ||
// for UNIX: | ||
// Check if permissions available to replace the binary | ||
// If yes, replace | ||
// If not, prompt? or what? sudo ?? > Prompt with command | ||
// for non-UNIX i.e. windows: | ||
// not possible to replace files during runtime | ||
// prompt command |
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
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,97 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Privado-Inc/privado/pkg/config" | ||
"github.com/codeclysm/extract" | ||
"github.com/schollz/progressbar/v3" | ||
) | ||
|
||
type gitHubReleaseType struct { | ||
TagName string `json:"tag_name"` | ||
PublishedAt string `json:"published_at"` | ||
} | ||
|
||
func GetLatestReleaseFromGitHub(repoName string) (*gitHubReleaseType, error) { | ||
endpoint := strings.Replace(config.ExtConfig.GitHubReleasesEndpoint, "${REPO_NAME}", config.AppConfig.PrivadoRepositoryName, 1) | ||
url := fmt.Sprintf("%s%s", config.ExtConfig.GitHubAPIHost, endpoint) | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Accept", "application/vnd.github.v3+json") | ||
|
||
defaultClient := &http.Client{} | ||
response, err := defaultClient.Do(req) | ||
if response.StatusCode != 200 || err != nil { | ||
return nil, err | ||
} | ||
|
||
responseData, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
releaseResponse := gitHubReleaseType{} | ||
err = json.Unmarshal(responseData, &releaseResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &releaseResponse, nil | ||
} | ||
|
||
func DownloadToFile(downloadURL, filePath string) error { | ||
req, err := http.NewRequest("GET", downloadURL, nil) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
bar := progressbar.DefaultBytes( | ||
resp.ContentLength, | ||
"Downloading..", | ||
) | ||
|
||
_, err = io.Copy(io.MultiWriter(file, bar), resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ExtractTarGzFile(sourceFile, target string) error { | ||
ctx := context.Background() | ||
data, err := ioutil.ReadFile(sourceFile) | ||
if err != nil { | ||
return err | ||
} | ||
buffer := bytes.NewBuffer(data) | ||
err = extract.Gz(ctx, buffer, target, func(s string) string { return s }) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |