Skip to content

Commit

Permalink
Add support for auto update CLI (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
puthrayaharness authored Mar 16, 2023
1 parent 148c930 commit 503fcd8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,11 @@ service.name: <+project.name>

If not all the required flags are provided we will fall back to prompt based technique to capture all the required details.

## To update the CLI
To update to a new version of the CLI
```shell
harness-upgrade update
```

## Contact
If you face any issues please reach out to us or feel free to create a GitHub issue.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ func main() {
EnableBashCompletion: true,
Suggest: true,
Commands: []*cli.Command{
{
Name: "update",
Aliases: []string{"upgrade"},
Usage: "Check for updates and upgrade the CLI",
Action: func(context *cli.Context) error {
return cliWrapper(Update, context)
},
},
{
Name: "account-summary",
Usage: "Get a summary of an account",
Expand Down
27 changes: 18 additions & 9 deletions release.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ type GithubRelease struct {
TagName string `json:"tag_name"`
}

func CheckGithubForReleases() {
if Version == "development" {
return
}
func GetNewRelease() (newVersion string) {
resp, err := http.Get("https://api.github.com/repos/harness/migrator/releases")
if err != nil {
return
Expand Down Expand Up @@ -52,6 +49,9 @@ func CheckGithubForReleases() {
if !v.Prerelease && len(latestStableRelease.TagName) == 0 {
latestStableRelease = v
}
if v.TagName == Version && v.Prerelease {
isPreRelease = true
}
}
if Version == latest.TagName {
return
Expand All @@ -60,15 +60,24 @@ func CheckGithubForReleases() {
return
}
if !latest.Prerelease {
printUpgradeMessage(Version, latest.TagName)
return
return latest.TagName
}
if isPreRelease {
printUpgradeMessage(Version, latest.TagName)
return
return latest.TagName
}
if !isPreRelease && latestStableRelease.TagName != Version {
printUpgradeMessage(Version, latestStableRelease.TagName)
return latestStableRelease.TagName
}
return
}

func CheckGithubForReleases() {
if Version == "development" {
return
}
newRelease := GetNewRelease()
if len(newRelease) > 0 {
printUpgradeMessage(Version, newRelease)
}
}

Expand Down
108 changes: 108 additions & 0 deletions update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"archive/tar"
"compress/gzip"
"fmt"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"io"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
)

func Update(*cli.Context) (err error) {
newVersion := GetNewRelease()
if len(newVersion) == 0 {
fmt.Println("Already on latest version. Skipping update")
return
}
blue := color.New(color.FgHiBlue).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("New version %s is available.\n", green(newVersion))
confirm := ConfirmInput("Do you want to update?")
if !confirm {
return nil
}
extension := "tar.gz"
const GOOS = runtime.GOOS
const GOARCH = runtime.GOARCH
if GOOS == "windows" {
extension = "zip"
}
url := fmt.Sprintf("https://github.com/harness/migrator/releases/download/%s/harness-upgrade-%s-%s-%s.%s", newVersion, newVersion, GOOS, GOARCH, extension)

if GOOS != "darwin" {
fmt.Printf("%s\n", yellow("Auto update support is only available for MacOS right now"))
fmt.Printf("Download the following release - %s\n", blue(url))
return nil
}

ex, err := os.Executable()
if err != nil {
return err
}
dir, err := filepath.Abs(path.Dir(ex))
if err != nil {
return err
}

// Download the file
fmt.Printf("Downloading the following - %s\n", blue(url))
resp, err := http.Get(url)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)

fmt.Printf("Unpacking the contents and extracting to - %s\n", blue(dir))
err = readTar(resp.Body, dir)
if err == nil {
fmt.Printf("Successfully upgraded to - %s\n", green(newVersion))
}
return err
}

func readTar(body io.ReadCloser, dest string) error {
gzRead, err := gzip.NewReader(body)
if err != nil {
return err
}
reader := tar.NewReader(gzRead)
for true {
header, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if header.Typeflag == tar.TypeReg && header.Name == "harness-upgrade" {
execFile := path.Join(dest, header.Name)
green := color.New(color.FgGreen).SprintFunc()
fmt.Printf("Extracking harness-upgrade to - %s\n", green(execFile))
outFile, err := os.Create(execFile)
if err != nil {
return err
}
if _, err = io.Copy(outFile, reader); err != nil {
return err
}
err = outFile.Close()
if err != nil {
return err
}
return nil
}
}
return nil
}

0 comments on commit 503fcd8

Please sign in to comment.