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", Use: "upgrade",
Short: "Upgrade autorestic and restic", Short: "Upgrade autorestic and restic",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
noRestic, _ := cmd.Flags().GetBool("no-restic") restic, _ := cmd.Flags().GetBool("restic")
err := bins.Upgrade(!noRestic) err := bins.Upgrade(restic)
CheckErr(err) CheckErr(err)
}, },
} }
func init() { func init() {
rootCmd.AddCommand(upgradeCmd) 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 { func Uninstall(restic bool) error {
if err := os.Remove(path.Join(INSTALL_PATH, "autorestic")); err != nil { if err := os.Remove(path.Join(INSTALL_PATH, "autorestic")); err != nil {
colors.Error.Println(err) return err
} }
if restic { if restic {
if err := os.Remove(path.Join(INSTALL_PATH, "restic")); err != nil { if err := os.Remove(path.Join(INSTALL_PATH, "restic")); err != nil {
colors.Error.Println(err) return err
} }
} }
return nil return nil
@ -79,11 +79,15 @@ func downloadAndInstallAsset(body GithubRelease, name string) error {
return err return err
} }
defer tmp.Close() defer tmp.Close()
tmp.Chmod(0755) if err := tmp.Chmod(0755); err != nil {
io.Copy(tmp, bz) return err
}
if _, err := io.Copy(tmp, bz); err != nil {
return err
}
to := path.Join(INSTALL_PATH, name) 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 { if err := os.Rename(tmp.Name(), to); err != nil {
return nil return nil
} }
@ -121,9 +125,11 @@ func Upgrade(restic bool) error {
// Upgrade restic // Upgrade restic
if restic { if restic {
if err := InstallRestic(); err != nil { if err := InstallRestic(); err != nil {
colors.Error.Println(err) return err
}
if err := upgradeRestic(); err != nil {
return err
} }
upgradeRestic()
} }
// Upgrade self // Upgrade self
@ -140,7 +146,9 @@ func Upgrade(restic bool) error {
return err return err
} }
if current.LT(latest) { if current.LT(latest) {
downloadAndInstallAsset(body, "autorestic") if err := downloadAndInstallAsset(body, "autorestic"); err != nil {
return err
}
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")