forked from sanbornm/go-selfupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (148 loc) · 3.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/kr/binarydist"
)
var version, genDir string
type current struct {
Version string
Sha256 []byte
}
func generateSha256(path string) []byte {
h := sha256.New()
b, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
}
h.Write(b)
sum := h.Sum(nil)
return sum
//return base64.URLEncoding.EncodeToString(sum)
}
type gzReader struct {
z, r io.ReadCloser
}
func (g *gzReader) Read(p []byte) (int, error) {
return g.z.Read(p)
}
func (g *gzReader) Close() error {
g.z.Close()
return g.r.Close()
}
func newGzReader(r io.ReadCloser) io.ReadCloser {
var err error
g := new(gzReader)
g.r = r
g.z, err = gzip.NewReader(r)
if err != nil {
panic(err)
}
return g
}
func createUpdate(path string, platform string) {
c := current{Version: version, Sha256: generateSha256(path)}
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
fmt.Println("error:", err)
}
err = ioutil.WriteFile(filepath.Join(genDir, platform+".json"), b, 0755)
if err != nil {
panic(err)
}
os.MkdirAll(filepath.Join(genDir, version), 0755)
var buf bytes.Buffer
w := gzip.NewWriter(&buf)
f, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
w.Write(f)
w.Close() // You must close this first to flush the bytes to the buffer.
err = ioutil.WriteFile(filepath.Join(genDir, version, platform+".gz"), buf.Bytes(), 0755)
files, err := ioutil.ReadDir(genDir)
if err != nil {
fmt.Println(err)
}
for _, file := range files {
if file.IsDir() == false {
continue
}
if file.Name() == version {
continue
}
os.Mkdir(filepath.Join(genDir, file.Name(), version), 0755)
fName := filepath.Join(genDir, file.Name(), platform+".gz")
old, err := os.Open(fName)
if err != nil {
// Don't have an old release for this os/arch, continue on
continue
}
fName = filepath.Join(genDir, version, platform+".gz")
newF, err := os.Open(fName)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err)
os.Exit(1)
}
ar := newGzReader(old)
defer ar.Close()
br := newGzReader(newF)
defer br.Close()
patch := new(bytes.Buffer)
if err := binarydist.Diff(ar, br, patch); err != nil {
panic(err)
}
ioutil.WriteFile(filepath.Join(genDir, file.Name(), version, platform), patch.Bytes(), 0755)
}
}
func printUsage() {
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 createBuildDir() {
os.MkdirAll(genDir, 0755)
}
func main() {
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)
}
platform := *platformFlag
appPath := flag.Arg(0)
version = flag.Arg(1)
genDir = *outputDirFlag
createBuildDir()
// If dir is given create update for each file
files, err := ioutil.ReadDir(appPath)
if err == nil {
for _, file := range files {
createUpdate(filepath.Join(appPath, file.Name()), file.Name())
}
os.Exit(0)
}
createUpdate(appPath, platform)
}