Skip to content

Commit

Permalink
Return error from pkg instead of using atexit
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmit committed May 10, 2017
1 parent 61fd312 commit b4771f6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 49 deletions.
7 changes: 6 additions & 1 deletion cmd/minishift/cmd/update.go
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package cmd

import (
"fmt"
"os"

"github.com/minishift/minishift/pkg/minikube/update"
"github.com/minishift/minishift/pkg/util/os/atexit"

"github.com/spf13/cobra"
)
Expand All @@ -32,7 +34,10 @@ var updateCmd = &cobra.Command{
}

func runUpdate(cmd *cobra.Command, args []string) {
update.MaybeUpdateFromGithub(os.Stdout)
err := update.MaybeUpdateFromGithub(os.Stdout)
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Update failed: %s", err))
}
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/constants/constants_darwin.go
Expand Up @@ -26,3 +26,4 @@ var SupportedVMDrivers = [...]string{

const DefaultVMDriver = "xhyve"
const OC_BINARY_NAME = "oc"
const BINARY_NAME = "minishift"
1 change: 1 addition & 0 deletions pkg/minikube/constants/constants_gendocs.go
Expand Up @@ -28,3 +28,4 @@ var SupportedVMDrivers = [...]string{

const DefaultVMDriver = "kvm"
const OC_BINARY_NAME = "oc"
const BINARY_NAME = "minishift"
1 change: 1 addition & 0 deletions pkg/minikube/constants/constants_linux.go
Expand Up @@ -25,3 +25,4 @@ var SupportedVMDrivers = [...]string{

const DefaultVMDriver = "kvm"
const OC_BINARY_NAME = "oc"
const BINARY_NAME = "minishift"
1 change: 1 addition & 0 deletions pkg/minikube/constants/constants_windows.go
Expand Up @@ -25,3 +25,4 @@ var SupportedVMDrivers = [...]string{

const DefaultVMDriver = "hyperv"
const OC_BINARY_NAME = "oc.exe"
const BINARY_NAME = "minishift.exe"
94 changes: 46 additions & 48 deletions pkg/minikube/update/update.go
Expand Up @@ -54,63 +54,53 @@ const (
githubRepo = "minishift"
)

var (
lastUpdateCheckFilePath = constants.MakeMiniPath("last_update_check")
)

func MaybeUpdateFromGithub(output io.Writer) {
func MaybeUpdateFromGithub(output io.Writer) error {
localVersion, err := version.GetSemverVersion()
if err != nil {
glog.Errorln(err)
return
return err
}
MaybeUpdate(output, githubOwner, githubRepo, githubRepo, lastUpdateCheckFilePath, localVersion)
}

func MaybeUpdate(output io.Writer, githubOwner, githubRepo, binaryName, lastUpdatePath string, localVersion semver.Version) {

latestVersion, err := getLatestVersionFromGitHub(githubOwner, githubRepo)
if err != nil {
atexit.ExitWithMessage(1, "Cannot get latest version from github.com\nPlease check your internet connection or try again later")
return
return err
}

var downloadArchive string
if checkLatestVersion(localVersion, latestVersion) {
return MaybeUpdate(output, githubOwner, githubRepo, latestVersion)
}
return nil
}

if runtime.GOOS == "windows" {
downloadArchive = fmt.Sprintf("%s-%s-%s-%s.zip", binaryName, latestVersion, runtime.GOOS, runtime.GOARCH)
func checkLatestVersion(localVersion, latestVersion semver.Version) bool {
if localVersion.Compare(latestVersion) < 0 {
return true
} else {
downloadArchive = fmt.Sprintf("%s-%s-%s-%s.tgz", binaryName, latestVersion, runtime.GOOS, runtime.GOARCH)
return false
}
updateLinkPrefix := "https://github.com/" + githubOwner + "/" + githubRepo + "/releases/tag/" + version.VersionPrefix
downloadLinkFormat := "https://github.com/" + githubOwner + "/" + githubRepo + "/releases/download/v%s/%s"
}

if !shouldCheckURLVersion(lastUpdatePath) {
return
}
func MaybeUpdate(output io.Writer, githubOwner, githubRepo string, latestVersion semver.Version) error {

if localVersion.Compare(latestVersion) < 0 {
err := writeTimeToFile(lastUpdatePath, time.Now().UTC())
if err != nil {
fmt.Println("Failed to update last update time")
}
fmt.Fprintf(output, `A newer version of %s is available. Do you want to
automatically update from %s%s to %s%s now? [y/N] `,
binaryName, version.VersionPrefix, localVersion, version.VersionPrefix, latestVersion)
var extName string

var confirm string
fmt.Scanln(&confirm)
if runtime.GOOS == "windows" {
extName = "zip"
} else {
extName = "tgz"
}

if strings.ToLower(confirm) == "y" {
fmt.Printf("Updating to version %s\n", latestVersion)
updateBinary(latestVersion, downloadArchive, updateLinkPrefix, downloadLinkFormat)
return
}
archiveName := fmt.Sprintf("minishift-%s-%s-%s.%s", latestVersion, runtime.GOOS, runtime.GOARCH, extName)
updateLinkPrefix := "https://github.com/" + githubOwner + "/" + githubRepo + "/releases/tag/" + version.VersionPrefix
downloadLinkFormat := "https://github.com/" + githubOwner + "/" + githubRepo + "/releases/download/v%s/%s"

fmt.Println("Skipping update.")
} else {
fmt.Println("Local minishift is at latest version:", latestVersion)
fmt.Printf("Updating to version %s\n", latestVersion)
err := updateBinary(latestVersion, archiveName, updateLinkPrefix, downloadLinkFormat)
if err != nil {
return err
}
return nil
}

func shouldCheckURLVersion(filePath string) bool {
Expand Down Expand Up @@ -164,8 +154,8 @@ func getTimeFromFileIfExists(path string) time.Time {
return timeInFile
}

func updateBinary(v semver.Version, downloadArchive, updateLinkPrefix, downloadLinkFormat string) {
checksum, err := downloadChecksum(v, downloadArchive, downloadLinkFormat)
func updateBinary(v semver.Version, archiveName, updateLinkPrefix, downloadLinkFormat string) error {
checksum, err := downloadChecksum(v, archiveName, downloadLinkFormat)
if err != nil {
glog.Errorf("Cannot download checksum: %s", err)
os.Exit(1)
Expand All @@ -177,8 +167,11 @@ func updateBinary(v semver.Version, downloadArchive, updateLinkPrefix, downloadL
os.Exit(1)
}

url := fmt.Sprintf(downloadLinkFormat, v, downloadArchive)
updateBinaryFile(url, checksum)
url := fmt.Sprintf(downloadLinkFormat, v, archiveName)
err = updateBinaryFile(url, checksum)
if err != nil {
return err
}

env := os.Environ()
args := []string{currentBinary, "version"}
Expand All @@ -187,9 +180,10 @@ func updateBinary(v semver.Version, downloadArchive, updateLinkPrefix, downloadL
glog.Errorf("Failed to execute updated binary %s: %s", currentBinary, err)
os.Exit(1)
}
return nil
}

func updateBinaryFile(url string, checksum []byte) {
func updateBinaryFile(url string, checksum []byte) error {
var binary string
fmt.Println("Downloading updated archive")
httpResp, err := http.Get(url)
Expand Down Expand Up @@ -220,10 +214,12 @@ func updateBinaryFile(url string, checksum []byte) {
os.Exit(1)
}

// Create a temporary directory to store archive contents
dir, err := ioutil.TempDir("", "minishift")
// Create a temporary directory inside minishift directory to store archive contents

dir, err := ioutil.TempDir(constants.Minipath, "tmp/download")
if err != nil {
atexit.ExitWithMessage(1, "Could not create a temporary directory to store archive contents")
return err
// atexit.ExitWithMessage(1, "Could not create a temporary directory to store archive contents")
}

defer os.RemoveAll(dir) // clean up
Expand All @@ -247,11 +243,12 @@ func updateBinaryFile(url string, checksum []byte) {
}
os.Exit(1)
}
return nil
}

func downloadChecksum(v semver.Version, downloadArchive, downloadLinkFormat string) ([]byte, error) {
func downloadChecksum(v semver.Version, archiveName, downloadLinkFormat string) ([]byte, error) {
fmt.Println("Downloading updated archive checksum to validate updated archive")
u := fmt.Sprintf(downloadLinkFormat, v, downloadArchive+".sha256")
u := fmt.Sprintf(downloadLinkFormat, v, archiveName+".sha256")
checksumResp, err := http.Get(u)
if err != nil {
return nil, err
Expand Down Expand Up @@ -323,5 +320,6 @@ func extractAndReplace(goos, dir string, archiveBytes []byte) string {
atexit.ExitWithMessage(1, "Could not Untar the gzip archive")
}
}

return binary
}

0 comments on commit b4771f6

Please sign in to comment.