Skip to content

Commit

Permalink
Merge pull request #25 from camerondurham/dev/move-version-check
Browse files Browse the repository at this point in the history
Implement #24
  • Loading branch information
camerondurham authored Nov 21, 2021
2 parents 45a6fad + d277f7c commit 16a2321
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 101 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## v0.3.5

- remove "latest version" checks from other commands (`ch list` and `ch running`)
- add "latest version" check logic to `ch upgrade` command

## v0.3.4

- `ch update` command
Expand Down
2 changes: 0 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ var listCmd = &cobra.Command{

func ListCmd(cmd *cobra.Command, args []string) {

util.CheckLatestVersion()

envName := ""
if len(args) > 0 {
envName = args[0]
Expand Down
2 changes: 0 additions & 2 deletions cmd/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ To see all running containers, run:

func RunningCmd(cmd *cobra.Command, args []string) {

util.CheckLatestVersion()

cli, err := util.NewCliClient()
if err != nil {
fmt.Printf("error: cannot create new CLI ApiClient: %v\n", err)
Expand Down
83 changes: 61 additions & 22 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/camerondurham/ch/cmd/util"
"github.com/camerondurham/ch/version"
"github.com/spf13/cobra"
"runtime"
)
Expand All @@ -15,33 +16,71 @@ var upgradeCmd = &cobra.Command{
Run: UpgradeCmd,
}

const UnixUpgradeTerminalCommand = "bash <(curl -s https://raw.githubusercontent.com/camerondurham/ch/main/scripts/install-ch.sh)"
const WindowsUpgradeTerminalCommand = "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/camerondurham/ch/main/scripts/install-ch.ps1'))"
const (
UnixUpgradeTerminalCommand = "bash <(curl -s https://raw.githubusercontent.com/camerondurham/ch/main/scripts/install-ch.sh)"
WindowsUpgradeTerminalCommand = "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/camerondurham/ch/main/scripts/install-ch.ps1'))"
)

func UpgradeCmd(cmd *cobra.Command, args []string) {
type upgradeTuple struct {
OperatingSystem string
Command string
}

var upgradeCommand string
var os string

switch runtime.GOOS {
case "darwin":
os = "macOS"
fallthrough
case "linux":
// set upgrade command
os = "Linux"
upgradeCommand = UnixUpgradeTerminalCommand
case "windows":
// set upgrade command
os = "Windows"
upgradeCommand = WindowsUpgradeTerminalCommand
type versionInfo struct {
NewVersionAvailable bool
VersionInfoMessage string
}

func getUpgradeCommandMap() map[string] upgradeTuple {
return map[string] upgradeTuple{
"darwin" : {
OperatingSystem: "macOS",
Command: UnixUpgradeTerminalCommand,
},
"linux" : {
OperatingSystem: "Linux",
Command: UnixUpgradeTerminalCommand,
},
"windows": {
OperatingSystem: "Windows",
Command: WindowsUpgradeTerminalCommand,
},
}
}

fmt.Printf("You appear to be running a %s operating system."+
"\nPlease run the following upgrade command:"+
"\n\n %s"+
"\n\nFor more help, see the repository README: %s\n", os, upgradeCommand, util.RepositoryUrl)
func newVersionAvailable() versionInfo {
latestVersion, err := util.GetLatestVersion(util.GetRequest, util.GetGithubAPILatestReleaseURL(util.RepositoryName))
if err != nil {
util.DebugPrint(fmt.Sprintf("ignoring version check since error occured when retrieving latest version: %v\n", err))
return versionInfo{
NewVersionAvailable: false,
VersionInfoMessage: "could not check latest version from Github",
}
} else if version.PkgVersion != "" && latestVersion != version.PkgVersion {
return versionInfo{
NewVersionAvailable: true,
VersionInfoMessage: fmt.Sprintf( "current version %s, latest version %s", version.PkgVersion, latestVersion),
}

} else {
return versionInfo{
NewVersionAvailable: false,
VersionInfoMessage: fmt.Sprintf("up to date, latest version is: %s", latestVersion),
}
}
}

func UpgradeCmd(cmd *cobra.Command, args []string) {

latestVersionInfo := newVersionAvailable()
fmt.Printf("\n\t%s\n", latestVersionInfo.VersionInfoMessage)

if true {
upgradeInfo := getUpgradeCommandMap()[runtime.GOOS]
fmt.Printf( "\n\tPlease run the following upgrade command (detected %s operating system):"+
"\n\n\t%s"+
"\n\n\tFor more help, see the repository README: %s\n", upgradeInfo.OperatingSystem, upgradeInfo.Command, util.RepositoryUrl)
}
}

func init() {
Expand Down
55 changes: 55 additions & 0 deletions cmd/util/latest_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package util

import (
"encoding/json"
"fmt"
"io"
"net/http"
)

const (
RepositoryUrl = "https://github.com/camerondurham/ch"
RepositoryName = "camerondurham/ch"
)

type callback func(string) (map[string]interface{}, error)

func GetGithubAPILatestReleaseURL(repository string) string {
return fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repository)
}

func GetLatestVersion(getRequest callback, url string) (string, error) {

data, err := getRequest(url)
if err != nil {
DebugPrint(fmt.Sprintf("error making GET request: %v", err))
return "", err
}

tagName := data["tag_name"].(string)
return tagName, nil
}

func GetRequest(url string) (map[string]interface{}, error) {
res, err := http.Get(url)
if err != nil {
DebugPrint(fmt.Sprintf("error making API request for latest release: %v", err))
return nil, err
}

body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
DebugPrint(fmt.Sprintf("error reading API response body: %v", err))
return nil, err
}
var data map[string]interface{}

if err = json.Unmarshal(body, &data); err != nil {
DebugPrint(fmt.Sprintf("error parsing json: %v", err))
return nil, err
}

return data, nil
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TestLatestVersion(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LatestVersion(tt.args.repository); got != tt.want {
t.Errorf("LatestVersion() = %v, want %v", got, tt.want)
if got := GetGithubAPILatestReleaseURL(tt.args.repository); got != tt.want {
t.Errorf("GetGithubAPILatestReleaseURL() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestGetLatestVersion(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetLatestVersion(tt.args.getRequest)
got, err := GetLatestVersion(tt.args.getRequest, "https://github.com/camerondurham/ch")
if (err != nil) != tt.wantErr {
t.Errorf("GetLatestVersion() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
72 changes: 0 additions & 72 deletions cmd/util/version_check.go

This file was deleted.

0 comments on commit 16a2321

Please sign in to comment.