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

Merge pull request #3 from thomasf/master

Add command line flags, fix bug, add docs
This commit is contained in:
Mark 2015-01-08 09:11:01 -08:00
commit 25a9691172
3 changed files with 92 additions and 64 deletions

48
main.go
View File

@ -5,17 +5,18 @@ import (
"compress/gzip" "compress/gzip"
"crypto/sha256" "crypto/sha256"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"github.com/kr/binarydist"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
//"runtime" "runtime"
//"encoding/base64"
"github.com/kr/binarydist"
) )
var plat, appPath, version, genDir string var version, genDir string
type current struct { type current struct {
Version string Version string
@ -124,34 +125,41 @@ func createUpdate(path string, platform string) {
} }
func printUsage() { func printUsage() {
fmt.Println("Go-Selfupdate - Enable your Golang applications to self update.\n\n") fmt.Println("")
fmt.Println("Usage:\n") fmt.Println("Positional arguments:")
fmt.Println("\tSingle platform: go-selfupdate myapp 1.2") fmt.Println("\tSingle platform: go-selfupdate myapp 1.2")
fmt.Println("\tCross platform: go-selfupdate /tmp/mybinares/ 1.2") fmt.Println("\tCross platform: go-selfupdate /tmp/mybinares/ 1.2")
} }
func isArgsPresent() bool {
if len(os.Args) < 2 {
return false
}
return true
}
func createBuildDir() { func createBuildDir() {
os.MkdirAll(genDir, 0755) os.MkdirAll(genDir, 0755)
} }
func main() { func main() {
if isArgsPresent() == false { outputDirFlag := flag.String("o", "public", "Output directory for writing updates")
var defaultPlatform string
goos := os.Getenv("GOOS")
goarch := os.Getenv("GOARCH")
if goos != "" && goarch != "" {
defaultPlatform = goos + "-" + goarch
} else {
defaultPlatform = runtime.GOOS + "-" + runtime.GOARCH
}
platformFlag := flag.String("platform", defaultPlatform,
"Target platform in the form OS-ARCH. Defaults to running os/arch or the combination of the environment variables GOOS and GOARCH if both are set.")
flag.Parse()
if flag.NArg() < 2 {
flag.Usage()
printUsage() printUsage()
os.Exit(0) os.Exit(0)
} }
plat = os.Getenv("GOOS") + "-" + os.Getenv("GOARCH") platform := *platformFlag
appPath = os.Args[1] appPath := flag.Arg(0)
version = os.Args[2] version = flag.Arg(1)
genDir = "public" genDir = *outputDirFlag
createBuildDir() createBuildDir()
@ -164,5 +172,5 @@ func main() {
os.Exit(0) os.Exit(0)
} }
createUpdate(appPath, plat) createUpdate(appPath, platform)
} }

View File

@ -1,37 +1,4 @@
package selfupdate // Update protocol:
import (
"bitbucket.org/kardianos/osext"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"github.com/inconshreveable/go-update"
"github.com/kr/binarydist"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
)
const (
upcktimePath = "cktime"
plat = runtime.GOOS + "-" + runtime.GOARCH
)
const devValidTime = 7 * 24 * time.Hour
var ErrHashMismatch = errors.New("new file hash mismatch after patch")
var up = update.New()
// Update protocol.
// //
// GET hk.heroku.com/hk/linux-amd64.json // GET hk.heroku.com/hk/linux-amd64.json
// //
@ -54,13 +21,66 @@ var up = update.New()
// //
// 200 ok // 200 ok
// [gzipped executable data] // [gzipped executable data]
//
//
package selfupdate
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"bitbucket.org/kardianos/osext"
"github.com/inconshreveable/go-update"
"github.com/kr/binarydist"
)
const (
upcktimePath = "cktime"
plat = runtime.GOOS + "-" + runtime.GOARCH
)
const devValidTime = 7 * 24 * time.Hour
var ErrHashMismatch = errors.New("new file hash mismatch after patch")
var up = update.New()
// Updater is the configuration and runtime data for doing an update.
//
// Note that ApiURL, BinURL and DiffURL should have the same value if all files are available at the same location.
//
// Example:
//
// updater := &selfupdate.Updater{
// CurrentVersion: version,
// ApiURL: "http://updates.yourdomain.com/",
// BinURL: "http://updates.yourdownmain.com/",
// DiffURL: "http://updates.yourdomain.com/",
// Dir: "update/",
// CmdName: "myapp", // app name
// }
// if updater != nil {
// go updater.BackgroundRun()
// }
type Updater struct { type Updater struct {
CurrentVersion string CurrentVersion string // Currently running version.
ApiURL string ApiURL string // Base URL for API requests (json files).
CmdName string CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary.
BinURL string BinURL string // Base URL for full binary downloads.
DiffURL string DiffURL string // Base URL for diff downloads.
Dir string Dir string // Directory to store selfupdate state.
Info struct { Info struct {
Version string Version string
Sha256 []byte Sha256 []byte
@ -74,6 +94,7 @@ func (u *Updater) getExecRelativeDir(dir string) string {
return path return path
} }
// BackgroundRun starts the update check and apply cycle.
func (u *Updater) BackgroundRun() { func (u *Updater) BackgroundRun() {
os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777) os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777)
if u.wantUpdate() { if u.wantUpdate() {

View File

@ -1,8 +1,7 @@
package selfupdate package selfupdate
import ( import "testing"
"testing"
) func TestUpdater(t *testing.T) {
func TestUpdater() {
} }