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

Add command line flags, fix bug

* Add -o flag for setting output directory
* Add -platform flag for setting platform manually
* Fixes #2
This commit is contained in:
Thomas Frössman 2015-01-07 12:10:56 +01:00
parent 14a25c83a5
commit 19468084c4
2 changed files with 22 additions and 23 deletions

38
main.go
View File

@ -5,17 +5,19 @@ 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"
"github.com/kr/binarydist"
//"encoding/base64" //"encoding/base64"
) )
var plat, appPath, version, genDir string var version, genDir string
type current struct { type current struct {
Version string Version string
@ -124,34 +126,32 @@ 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")
platformFlag := flag.String("platform", runtime.GOOS+"-"+runtime.GOARCH,
"Target platform in the form OS-ARCH. Defaults to running os/arch.")
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 +164,5 @@ func main() {
os.Exit(0) os.Exit(0)
} }
createUpdate(appPath, plat) createUpdate(appPath, platform)
} }

View File

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