Skip to content

Commit

Permalink
add automatic update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 9, 2020
1 parent 883a030 commit e93250c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ Create, control and connect to VirtualBox VM instances.

## Installation
#### Automatic installation:
To install or update for macos and linux:
To install or **update** for macos and linux:
```shell script
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/mhewedy/vermin/master/install.sh)"
```
To install or update on windows (PowerShell):
To install or **update** on windows (PowerShell):
```
# Should run as Adminstarator
C:\> iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/mhewedy/vermin/master/install.ps1'))
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $ vermin images
Then you can create a vm using:
$ vermin create <image>
`,
PersistentPreRun: preRun,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand Down
6 changes: 6 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"github.com/mhewedy/vermin/config"
"github.com/spf13/cobra"
"os"
)

Expand All @@ -11,3 +13,7 @@ func checkFilePath(path string) {
os.Exit(1)
}
}

func preRun(cmd *cobra.Command, args []string) {
config.CheckForUpdates(version)
}
49 changes: 49 additions & 0 deletions config/updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
"fmt"
"math/rand"
"net/http"
"strings"
"time"
)

const versionURL = "https://github.com/mhewedy/vermin/releases/latest"
const updateURL = "https://github.com/mhewedy/vermin#Automatic-installation"

var client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

// CheckForUpdates checks for updates at random times
func CheckForUpdates(currentVersion string) {
rand.Seed(time.Now().UnixNano())
r := rand.Intn(100)

if r == 0 {
req, err := http.NewRequest("HEAD", versionURL, nil)
if err != nil {
return
}

resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()

loc, err := resp.Location()
if err != nil {
return
}

s := strings.Split(loc.Path, "/")
newVersion := s[len(s)-1]

if currentVersion != newVersion {
fmt.Printf("\nNew version avaiable %s, check %s\n\n", newVersion, updateURL)
}
}
}

0 comments on commit e93250c

Please sign in to comment.