From eb98632f1d5f3be6b64cc28a9a938261895137f8 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Tue, 14 Jul 2020 13:32:31 -0700 Subject: [PATCH] update command --- cmd/commands.go | 42 ++++++++++++++++++++++++++++++++++++++++++ cmd/main.go | 1 + 2 files changed, 43 insertions(+) diff --git a/cmd/commands.go b/cmd/commands.go index df7629f7..6ad5a650 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -1,10 +1,13 @@ package main import ( + "bytes" "fmt" "github.com/pkg/errors" "github.com/spf13/cobra" + "io" "os" + "os/exec" ) var rootCmd = &cobra.Command{ @@ -15,6 +18,45 @@ var rootCmd = &cobra.Command{ }, } +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update to the latest version", + RunE: func(cmd *cobra.Command, args []string) error { + log.Infoln("Updating Statping to the latest version...") + curl, err := exec.LookPath("curl") + if err != nil { + return err + } + bash, err := exec.LookPath("bash") + if err != nil { + return err + } + + ree := bytes.NewBuffer(nil) + + c1 := exec.Command(curl, "-o-", "-L", "https://statping.com/install.sh") + c2 := exec.Command(bash) + + r, w := io.Pipe() + c1.Stdout = w + c2.Stdin = r + + var b2 bytes.Buffer + c2.Stdout = &b2 + + c1.Start() + c2.Start() + c1.Wait() + w.Close() + c2.Wait() + io.Copy(ree, &b2) + + log.Infoln(ree.String()) + os.Exit(0) + return nil + }, +} + var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of Statping", diff --git a/cmd/main.go b/cmd/main.go index 056202fe..10caa9b8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -33,6 +33,7 @@ func init() { utils.InitEnvs() rootCmd.AddCommand(versionCmd) + rootCmd.AddCommand(updateCmd) rootCmd.AddCommand(assetsCmd) rootCmd.AddCommand(exportCmd) rootCmd.AddCommand(importCmd)