1
0
mirror of https://github.com/taigrr/go-selfupdate synced 2025-01-18 04:33:12 -08:00

Merge pull request #22 from deinspanjer/feature/example

Example demonstrating how go-selfupdate works
This commit is contained in:
Mark 2016-12-16 09:22:40 -08:00 committed by GitHub
commit bf295275fb
5 changed files with 120 additions and 0 deletions

4
.gitignore vendored
View File

@ -12,3 +12,7 @@ syntax: glob
*.log *.log
public/* public/*
go-selfupdate go-selfupdate
example/example-server
example/hello-updater
example/public/*
example/deployment/*

View File

@ -0,0 +1,3 @@
<!DOCTYPE html>
<title>Hello Updater Example Server</title>
I serve updates of the hello-updater program!

69
example/run-example.sh Executable file
View File

@ -0,0 +1,69 @@
#!/bin/bash
echo "This example will compile the hello-updater application a few times with different version strings and demonstrate go-selfupdate's functionality."
echo "If the version is 'dev', no update checking will be performed."
echo "I'm not sure why go-selfupdate doesn't put the files under the <appname> folder since that seems to be required for checking"
echo
rm -rf deployment/update deployment/hello* public/hello-updater
echo "Building example-server"; echo
go build -o example-server src/example-server/main.go
echo "Running example server"; echo
killall example-server
./example-server &
read -n 1 -p "Press any key to start." ignored; echo
echo "Building dev version of hello-updater"; echo
go build -ldflags="-X main.version=dev" -o hello-updater src/hello-updater/main.go
echo "Copying it to deployment folder"; echo
cp hello-updater deployment/
echo "Running deployment/hello-updater"
deployment/hello-updater
read -n 1 -p "Press any key to continue." ignored; echo
echo; echo "=========="; echo
for (( minor=0; minor<=2; minor++ )); do
echo "Building hello-updater with version set to 1.$minor"
go build -ldflags="-X main.version=1.$minor" -o hello-updater src/hello-updater/main.go
echo "Running go-update to make update available via example-server"; echo
go-selfupdate -o public/hello-updater/ hello-updater 1.$minor
if (( $minor == 0 )); then
echo "Copying version 1.0 to deployment so it can self-update"; echo
cp hello-updater deployment/
cp hello-updater deployment/hello-updater-1.0
fi
echo "Running deployment/hello-updater"
deployment/hello-updater
read -n 1 -p "Press any key to continue." ignored; echo
echo; echo "=========="; echo
done
echo "Running deployment/hello-updater-1.0 backup copy"
deployment/hello-updater-1.0
read -n 1 -p "Press any key to continue." ignored; echo
echo; echo "=========="; echo
echo "Building unknown version of hello-updater"; echo
go build -ldflags="-X main.version=unknown" -o hello-updater src/hello-updater/main.go
echo "Copying unknown version to deployment so it can self-update"; echo
cp hello-updater deployment/
echo "Running deployment/hello-updater"
deployment/hello-updater
sleep 5
echo; echo "Re-running deployment/hello-updater"
deployment/hello-updater
sleep 5
echo; echo
echo "Shutting down example-server"
killall example-server

View File

@ -0,0 +1,20 @@
package main
import (
"log"
"net/http"
)
type logHandler struct {
handler http.Handler
}
func (lh *logHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
log.Printf("\n\texample-server received request %s\n", r.URL.RequestURI())
lh.handler.ServeHTTP(rw,r)
}
func main() {
// Simple static webserver with logging:
log.Fatal(http.ListenAndServe(":8080", &logHandler{handler:http.FileServer(http.Dir("./public"))}))
}

View File

@ -0,0 +1,24 @@
package main
import (
"log"
"github.com/sanbornm/go-selfupdate/selfupdate"
)
var version string
var updater = &selfupdate.Updater{
CurrentVersion: version, // Manually update the const, or set it using `go build -ldflags="-X main.VERSION=<newver>" -o hello-updater src/hello-updater/main.go`
ApiURL: "http://localhost:8080/", // The server hosting `$CmdName/$GOOS-$ARCH.json` which contains the checksum for the binary
BinURL: "http://localhost:8080/", // The server hosting the zip file containing the binary application which is a fallback for the patch method
DiffURL: "http://localhost:8080/", // The server hosting the binary patch diff for incremental updates
Dir: "update/", // The directory created by the app when run which stores the cktime file
CmdName: "hello-updater", // The app name which is appended to the ApiURL to look for an update
ForceCheck: true, // For this example, always check for an update unless the version is "dev"
}
func main() {
log.Printf("Hello world I am currently version %v", updater.CurrentVersion)
updater.BackgroundRun()
log.Printf("Next run, I should be %v", updater.Info.Version)
}