diff --git a/main.go b/main.go index 5046f39..bb2f60f 100644 --- a/main.go +++ b/main.go @@ -5,17 +5,18 @@ import ( "compress/gzip" "crypto/sha256" "encoding/json" + "flag" "fmt" - "github.com/kr/binarydist" "io" "io/ioutil" "os" "path/filepath" - //"runtime" - //"encoding/base64" + "runtime" + + "github.com/kr/binarydist" ) -var plat, appPath, version, genDir string +var version, genDir string type current struct { Version string @@ -124,34 +125,41 @@ func createUpdate(path string, platform string) { } func printUsage() { - fmt.Println("Go-Selfupdate - Enable your Golang applications to self update.\n\n") - fmt.Println("Usage:\n") + fmt.Println("") + fmt.Println("Positional arguments:") fmt.Println("\tSingle platform: go-selfupdate myapp 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() { os.MkdirAll(genDir, 0755) } 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() os.Exit(0) } - plat = os.Getenv("GOOS") + "-" + os.Getenv("GOARCH") - appPath = os.Args[1] - version = os.Args[2] - genDir = "public" + platform := *platformFlag + appPath := flag.Arg(0) + version = flag.Arg(1) + genDir = *outputDirFlag createBuildDir() @@ -164,5 +172,5 @@ func main() { os.Exit(0) } - createUpdate(appPath, plat) + createUpdate(appPath, platform) } diff --git a/selfupdate/selfupdate.go b/selfupdate/selfupdate.go index f6e4dde..23a8321 100644 --- a/selfupdate/selfupdate.go +++ b/selfupdate/selfupdate.go @@ -1,37 +1,4 @@ -package selfupdate - -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. +// Update protocol: // // GET hk.heroku.com/hk/linux-amd64.json // @@ -54,13 +21,66 @@ var up = update.New() // // 200 ok // [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 { - CurrentVersion string - ApiURL string - CmdName string - BinURL string - DiffURL string - Dir string + CurrentVersion string // Currently running version. + ApiURL string // Base URL for API requests (json files). + CmdName string // Command name is appended to the ApiURL like http://apiurl/CmdName/. This represents one binary. + BinURL string // Base URL for full binary downloads. + DiffURL string // Base URL for diff downloads. + Dir string // Directory to store selfupdate state. Info struct { Version string Sha256 []byte @@ -74,6 +94,7 @@ func (u *Updater) getExecRelativeDir(dir string) string { return path } +// BackgroundRun starts the update check and apply cycle. func (u *Updater) BackgroundRun() { os.MkdirAll(u.getExecRelativeDir(u.Dir), 0777) if u.wantUpdate() { diff --git a/selfupdate/selfupdate_test.go b/selfupdate/selfupdate_test.go index a679db3..84bac00 100644 --- a/selfupdate/selfupdate_test.go +++ b/selfupdate/selfupdate_test.go @@ -1,8 +1,7 @@ package selfupdate -import ( - "testing" -) +import "testing" + +func TestUpdater(t *testing.T) { -func TestUpdater() { }