|
| 1 | +# go-updater |
| 2 | + |
| 3 | +A cross-platform updater library for Go applications. |
| 4 | +On Linux and macOS, it replaces the executable (or .app for macOS) and launches the new version, while on Windows, it runs the installer with the required privileges. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +```bash |
| 9 | +go get github.com/arduino/go-updater |
| 10 | +``` |
| 11 | + |
| 12 | +## Quick Start |
| 13 | + |
| 14 | +### 1. Basic Example |
| 15 | + |
| 16 | +Here's a simple example of how to integrate auto-updates into your Go application. |
| 17 | + |
| 18 | +Add a `version` variable in the main package. Note that this variable will be overwritten during the build process through ldflags flag. |
| 19 | + |
| 20 | +```go |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + "os" |
| 27 | + |
| 28 | + "github.com/arduino/go-updater/updater" |
| 29 | + "github.com/arduino/go-updater/releaser" |
| 30 | +) |
| 31 | + |
| 32 | +var version = "0.0.0" |
| 33 | + |
| 34 | +func main() { |
| 35 | + // Your current application version |
| 36 | + currentVersion := releaser.Version(version) |
| 37 | + |
| 38 | + // Create HTTP client for your update server |
| 39 | + // The base URL should point to the parent directory containing platform-specific folders |
| 40 | + // The client will automatically discover the correct manifest.json based on the current platform |
| 41 | + // e.g., running on Linux will look for: https://releases.example.com/path/to/release/linux-amd64.json |
| 42 | + client := releaser.NewClient("https://releases.example.com/", "path/to/release/") |
| 43 | + |
| 44 | + fmt.Println("Checking for updates...") |
| 45 | + executablePath, err := os.Executable() |
| 46 | + if err != nil { |
| 47 | + panic("could not get executable path") |
| 48 | + } |
| 49 | + confirmUpdate := func(current, target releaser.Version) bool { |
| 50 | + return true |
| 51 | + } |
| 52 | + err := updater.CheckForUpdates( |
| 53 | + executablePath, // Path to current executable |
| 54 | + currentVersion, // Current version |
| 55 | + client, // HTTP releaser client |
| 56 | + confirmUpdate, // Auto-confirm updates |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + panic(err) |
| 60 | + } |
| 61 | + |
| 62 | + // Note: If an update was found and applied, the application will be restarted |
| 63 | + // with the new version and the code below will never be executed. |
| 64 | + // Your application logic should be placed here only for cases where: |
| 65 | + // - No update was available (err == nil) |
| 66 | + // - Update check failed (e.g., error fetching the release) |
| 67 | + // - User declined the update (err == nil) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +### 2. Build Your Application |
| 73 | + |
| 74 | +Build your application with a specific version by following these requirements: |
| 75 | +- Use the LDFLAGS `-X` flag to set the version at build time |
| 76 | +- Include the version in the output filename (this is mandatory for the releaser tool to function correctly) |
| 77 | +```bash |
| 78 | +GOOS=linux GOARCH=amd64 go build -o myapp-linux-amd64-1.0.0 -ldflags="-X 'main.version=1.0.0'" ./cmd/myapp |
| 79 | +``` |
| 80 | + |
| 81 | +### 3. Create Release Manifest |
| 82 | + |
| 83 | +```bash |
| 84 | +go run github.com/arduino/go-updater/cmd/releaser ./myapp-linux-amd64-1.0.0 1.0.0 -platform linux-amd64 -o ./releases/ |
| 85 | + |
| 86 | +Release created successfully! |
| 87 | +{ |
| 88 | + "name": "myapp-linux-amd64-1.0.0", |
| 89 | + "version": "1.0.0", |
| 90 | + "sha256": "6238F9cnMS8ete3kfDnD9Yk7iDFMWLBX31HXHmii734=" |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +where: |
| 95 | + - `name` is the name of the executable/archive |
| 96 | + - `version` is the version of the release |
| 97 | + - `sha256` is the sha256 of the executable/archive |
| 98 | + |
| 99 | +### 4. Server Setup |
| 100 | + |
| 101 | +Set up an HTTP server to serve your releases. The updater expects this structure where each platform has its own json file: |
| 102 | + |
| 103 | +``` |
| 104 | +https://releases.example.com/ <- Base URL used in NewClient() |
| 105 | +├── /path/to/release/ |
| 106 | + ├── myapp-linux-amd64-1.0.0.tar.gz <- Actual executable/archive |
| 107 | + ├── myapp-windows-1.0.0-installer.exe |
| 108 | + ├── myapp-darwin-1.0.0.zip |
| 109 | + | |
| 110 | + └── darwin-amd64.json |
| 111 | + └── linux-amd64.json <- Platform manifest |
| 112 | + └── windows-amd64.json |
| 113 | +``` |
| 114 | + |
| 115 | +## License |
| 116 | + |
| 117 | +This software is released under the GNU General Public License version 3. See the [LICENSE](LICENSE) file for complete details. |
| 118 | + |
| 119 | +For commercial licensing options, please contact license@arduino.cc. |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +Made with ❤️ by [Arduino](https://www.arduino.cc/) |
0 commit comments