upgrade logic

This commit is contained in:
cupcakearmy 2021-04-17 17:13:03 +02:00
parent ec2377287e
commit 976a3beab5
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9

View File

@ -57,44 +57,48 @@ func Uninstall(restic bool) error {
return nil return nil
} }
func downloadAndInstallAsset(body GithubRelease, name string) error {
ending := fmt.Sprintf("_%s_%s.bz2", runtime.GOOS, runtime.GOARCH)
for _, asset := range body.Assets {
if strings.HasSuffix(asset.Name, ending) {
// Download archive
colors.Faint.Println("Downloading:", asset.Link)
resp, err := http.Get(asset.Link)
if err != nil {
return err
}
defer resp.Body.Close()
// Uncompress
bz := bzip2.NewReader(resp.Body)
// Save binary
file, err := os.Create(path.Join(INSTALL_PATH, name))
if err != nil {
return err
}
file.Chmod(0755)
defer file.Close()
io.Copy(file, bz)
colors.Success.Printf("Successfully installed '%s' under %s\n", name, INSTALL_PATH)
return nil
}
}
return errors.New("could not find right binary for your system, please install restic manually. https://bit.ly/2Y1Rzai")
}
func InstallRestic() error { func InstallRestic() error {
installed := internal.CheckIfCommandIsCallable("restic") installed := internal.CheckIfCommandIsCallable("restic")
if installed { if installed {
colors.Body.Println("restic already installed") colors.Body.Println("restic already installed")
return nil return nil
} else { } else {
body, err := dlJSON("https://api.github.com/repos/restic/restic/releases/latest") if body, err := dlJSON("https://api.github.com/repos/restic/restic/releases/latest"); err != nil {
if err != nil {
return err return err
} else {
return downloadAndInstallAsset(body, "restic")
} }
ending := fmt.Sprintf("_%s_%s.bz2", runtime.GOOS, runtime.GOARCH)
for _, asset := range body.Assets {
if strings.HasSuffix(asset.Name, ending) {
// Download archive
colors.Faint.Println("Downloading:", asset.Link)
resp, err := http.Get(asset.Link)
if err != nil {
return err
}
defer resp.Body.Close()
// Uncompress
bz := bzip2.NewReader(resp.Body)
// Save binary
file, err := os.Create(path.Join(INSTALL_PATH, "restic"))
if err != nil {
return err
}
file.Chmod(0755)
defer file.Close()
io.Copy(file, bz)
colors.Success.Printf("Successfully installed restic under %s\n", INSTALL_PATH)
return nil
}
}
return errors.New("could not find right binary for your system, please install restic manually. https://bit.ly/2Y1Rzai")
} }
} }
@ -127,7 +131,7 @@ func Upgrade(restic bool) error {
return err return err
} }
if current.GT(latest) { if current.GT(latest) {
// TODO: Actually download and install downloadAndInstallAsset(body, "autorestic")
colors.Success.Println("Updated autorestic") colors.Success.Println("Updated autorestic")
} else { } else {
colors.Body.Println("Already up to date") colors.Body.Println("Already up to date")