changed log.fatal to error return
used http client with timeout
@@ -3,10 +3,10 @@ package cmd
import (
"io"
"io/ioutil"
- "log"
"net/http"
"os"
"os/exec"
+ "time"
"github.com/spf13/cobra"
)
@@ -18,28 +18,33 @@ var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "upgrade gnmic to latest available version",
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
f, err := ioutil.TempFile("", "gnmic")
defer os.Remove(f.Name())
if err != nil {
- log.Fatalf("Failed to create temp file %s\n", err)
+ return err
+ }
+ err = downloadFile(downloadURL, f)
+ if err != nil {
+ return err
}
- downloadFile(downloadURL, f)
c := exec.Command("bash", f.Name())
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
- log.Fatalf("Upgrade failed: %s\n", err)
+ return err
}
+ return nil
},
}
// downloadFile will download a file from a URL and write its content to a file
func downloadFile(url string, file *os.File) error {
+ client := http.Client{Timeout: 10 * time.Second}
// Get the data
- resp, err := http.Get(url)
+ resp, err := client.Get(url)
if err != nil {
return err
}
