replaceExecutable safely replaces the current executable with the new one
(currentPath, newPath string)
| 166 | |
| 167 | // replaceExecutable safely replaces the current executable with the new one |
| 168 | func replaceExecutable(currentPath, newPath string) error { |
| 169 | // Rename current executable to .old (backup) |
| 170 | oldFile := currentPath + ".old" |
| 171 | if err := os.Rename(currentPath, oldFile); err != nil { |
| 172 | return fmt.Errorf("error backing up current version: %v", err) |
| 173 | } |
| 174 | |
| 175 | // Move new executable into place |
| 176 | if err := os.Rename(newPath, currentPath); err != nil { |
| 177 | // Try to restore old version |
| 178 | os.Rename(oldFile, currentPath) |
| 179 | return fmt.Errorf("error installing new version: %v", err) |
| 180 | } |
| 181 | |
| 182 | // Clean up old version |
| 183 | os.Remove(oldFile) |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | func getLatestRelease() (*GithubRelease, error) { |
| 188 | resp, err := http.Get("https://api.github.com/repos/cronitorio/cronitor-cli/releases/latest") |