error handling for upgrade and uninstall

This commit is contained in:
cupcakearmy 2021-10-30 14:50:27 +02:00
parent 11d1da7468
commit 8c30134f7c
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
2 changed files with 19 additions and 11 deletions

View File

@ -9,13 +9,13 @@ var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade autorestic and restic",
Run: func(cmd *cobra.Command, args []string) {
noRestic, _ := cmd.Flags().GetBool("no-restic")
err := bins.Upgrade(!noRestic)
restic, _ := cmd.Flags().GetBool("restic")
err := bins.Upgrade(restic)
CheckErr(err)
},
}
func init() {
rootCmd.AddCommand(upgradeCmd)
upgradeCmd.Flags().Bool("no-restic", false, "also update restic")
upgradeCmd.Flags().Bool("restic", true, "also update restic")
}

View File

@ -47,11 +47,11 @@ func dlJSON(url string) (GithubRelease, error) {
func Uninstall(restic bool) error {
if err := os.Remove(path.Join(INSTALL_PATH, "autorestic")); err != nil {
colors.Error.Println(err)
return err
}
if restic {
if err := os.Remove(path.Join(INSTALL_PATH, "restic")); err != nil {
colors.Error.Println(err)
return err
}
}
return nil
@ -79,11 +79,15 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
return err
}
defer tmp.Close()
tmp.Chmod(0755)
io.Copy(tmp, bz)
if err := tmp.Chmod(0755); err != nil {
return err
}
if _, err := io.Copy(tmp, bz); err != nil {
return err
}
to := path.Join(INSTALL_PATH, name)
os.Remove(to) // Delete if current, ignore error if file does not exits.
defer os.Remove(to) // Delete if current, ignore error if file does not exits.
if err := os.Rename(tmp.Name(), to); err != nil {
return nil
}
@ -121,9 +125,11 @@ func Upgrade(restic bool) error {
// Upgrade restic
if restic {
if err := InstallRestic(); err != nil {
colors.Error.Println(err)
return err
}
if err := upgradeRestic(); err != nil {
return err
}
upgradeRestic()
}
// Upgrade self
@ -140,7 +146,9 @@ func Upgrade(restic bool) error {
return err
}
if current.LT(latest) {
downloadAndInstallAsset(body, "autorestic")
if err := downloadAndInstallAsset(body, "autorestic"); err != nil {
return err
}
colors.Success.Println("Updated autorestic")
} else {
colors.Body.Println("Already up to date")