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

go-selfupdate binary uses os env to detect arch/platform so you can crosscompile

This commit is contained in:
Mark Sanborn 2013-11-15 12:34:34 -08:00
parent f1263af1c0
commit 74aa055b82

30
main.go
View File

@ -1,23 +1,21 @@
package main package main
import ( import (
"fmt"
"os"
"encoding/json"
"io/ioutil"
"io"
"path/filepath"
"compress/gzip"
"bytes" "bytes"
"compress/gzip"
"crypto/sha256" "crypto/sha256"
"encoding/json"
"fmt"
"github.com/kr/binarydist" "github.com/kr/binarydist"
"runtime" "io"
"io/ioutil"
"os"
"path/filepath"
//"runtime"
//"encoding/base64" //"encoding/base64"
) )
const ( var plat string
plat = runtime.GOOS + "-" + runtime.GOARCH
)
type current struct { type current struct {
Version string Version string
@ -61,6 +59,8 @@ func newGzReader(r io.ReadCloser) io.ReadCloser {
} }
func main() { func main() {
plat = os.Getenv("GOOS") + "-" + os.Getenv("GOARCH")
appPath := os.Args[1] appPath := os.Args[1]
version := os.Args[2] version := os.Args[2]
genDir := "public" genDir := "public"
@ -72,7 +72,7 @@ func main() {
if err != nil { if err != nil {
fmt.Println("error:", err) fmt.Println("error:", err)
} }
err = ioutil.WriteFile(filepath.Join(genDir, plat + ".json"), b, 0755) err = ioutil.WriteFile(filepath.Join(genDir, plat+".json"), b, 0755)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -87,7 +87,7 @@ func main() {
} }
w.Write(f) w.Write(f)
w.Close() // You must close this first to flush the bytes to the buffer. w.Close() // You must close this first to flush the bytes to the buffer.
err = ioutil.WriteFile(filepath.Join(genDir, version, plat + ".gz"), buf.Bytes(), 0755) err = ioutil.WriteFile(filepath.Join(genDir, version, plat+".gz"), buf.Bytes(), 0755)
files, err := ioutil.ReadDir(genDir) files, err := ioutil.ReadDir(genDir)
if err != nil { if err != nil {
@ -104,14 +104,14 @@ func main() {
os.Mkdir(filepath.Join(genDir, file.Name(), version), 0755) os.Mkdir(filepath.Join(genDir, file.Name(), version), 0755)
fName := filepath.Join(genDir, file.Name(), plat + ".gz") fName := filepath.Join(genDir, file.Name(), plat+".gz")
old, err := os.Open(fName) old, err := os.Open(fName)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err) fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err)
os.Exit(1) os.Exit(1)
} }
fName = filepath.Join(genDir, version, plat + ".gz") fName = filepath.Join(genDir, version, plat+".gz")
newF, err := os.Open(fName) newF, err := os.Open(fName)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err) fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err)