Skip to content

Commit

Permalink
add check for version (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhindia committed Oct 19, 2021
1 parent f99b0a7 commit 93cd36a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/apoorvam/goterminal v0.0.0-20180523175556-614d345c47e5
github.com/aws/aws-sdk-go v1.40.29
github.com/heroku/docker-registry-client v0.0.0-20190909225348-afc9e1acc3d5
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/manifoldco/promptui v0.8.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/imdario/mergo v0.3.10 h1:6q5mVkdH/vYmqngx7kZQTjJ5HRsx+ImorDIEQ+beJgc=
github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imkira/go-interpol v1.1.0/go.mod h1:z0h2/2T3XF8kyEPpRgJ3kmNv+C43p+I/CoI+jC3w2iA=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
Expand Down
113 changes: 112 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ limitations under the License.
package cmd

import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"

"github.com/inconshreveable/go-update"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand All @@ -42,7 +52,7 @@ var rootCmd = &cobra.Command{
SilenceErrors: true,
RunE: func(_ *cobra.Command, args []string) error {
if versionFlag {
fmt.Printf("ridectl version %s\n", version)
pterm.Success.Printf("ridectl version %s\n", version)
} else if len(args) == 0 {

return fmt.Errorf("No command specified.")
Expand All @@ -58,6 +68,17 @@ func init() {
}
rootCmd.PersistentFlags().StringVar(&kubeconfigFlag, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
rootCmd.Flags().BoolVar(&versionFlag, "version", false, "--version")
// check version and update if not latest
if !isLatestVersion() {
updatePrompt := promptui.Prompt{
Label: "Do you want to update to latest version",
IsConfirm: true,
}
shouldUpdate, _ := updatePrompt.Run()
if shouldUpdate == "y" {
selfUpdate()
}
}
// Register all types from summon-operator and ridecell-controllers secrets
_ = summonv1beta2.AddToScheme(scheme.Scheme)
_ = secretsv1beta2.AddToScheme(scheme.Scheme)
Expand All @@ -70,3 +91,93 @@ func Execute() {
os.Exit(1)
}
}

func isLatestVersion() bool {

resp, err := http.Get("https://api.github.com/repos/Ridecell/ridectl/releases/latest")
if err != nil {
log.Fatalln(err)
}

body, _ := io.ReadAll(resp.Body)
resp.Body.Close()

var data interface{}
err = json.Unmarshal(body, &data)
if err != nil {
panic(err.Error())
}

if version != data.(map[string]interface{})["tag_name"].(string) {
pterm.Warning.Printf("You are running older version of ridectl %s\n", version)
return false
}

return true
}

func selfUpdate() {
var url string
p := pterm.DefaultProgressbar.WithTotal(3)
p.ShowElapsedTime = false

switch runtime.GOOS {

case "darwin":
url = "https://github.com/Ridecell/ridectl/releases/latest/download/ridectl_macos.zip"

case "linux":
url = "https://github.com/Ridecell/ridectl/releases/latest/download/ridectl_linux.zip"

}

_, _ = p.Start()
p.Title = "Downloading"
res, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer res.Body.Close()
p.Increment()

p.Title = "Extracting"
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
pterm.Error.Printf("Failed to create buffer for zip file: %s\n", err)
}

r, err := getBinary(buf)
if err != nil {
log.Fatalln(err)
}
p.Increment()

executable, err := os.Executable()
if err != nil {
panic(err)
}

p.Title = "Installing"
cmdPath := filepath.Join(executable)
err = update.Apply(r, update.Options{TargetPath: cmdPath})
if err != nil {
pterm.Error.Printf("Failed to update binary: %s\n", err)
pterm.Info.Printf("If you are linux user, then please rerun ridectl with sudo privileges to update")
os.Exit(1)
}
p.Increment()
_, _ = p.Stop()

}

func getBinary(src []byte) (io.Reader, error) {
r := bytes.NewReader(src)
z, err := zip.NewReader(r, r.Size())
if err != nil {
return nil, fmt.Errorf("failed to uncompress zip file: %s", err)
}
for _, file := range z.File {
return file.Open()
}
return nil, fmt.Errorf("failed to find binary in zip file")
}

0 comments on commit 93cd36a

Please sign in to comment.