-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate.go
113 lines (94 loc) · 2.19 KB
/
update.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
package main
import (
"archive/zip"
"fmt"
"github.com/codegangsta/cli"
"io"
"os"
"path"
"strings"
)
const ScriptDirName = "scripts"
var CMDUpdate = cli.Command{
Name: "update",
Description: "Update remote fix scripts from $source to $cache",
Action: updateAction,
Flags: []cli.Flag{},
}
func updateAction(c *cli.Context) error {
cacheDir := c.GlobalString("cache")
baseUrl := c.GlobalString("source")
fingerprint, err := remoteCatLine(baseUrl + "/index")
if err != nil {
return err
}
os.MkdirAll(cacheDir, 0755)
err = updateCache(baseUrl, fingerprint, cacheDir)
if err != nil {
return err
}
fmt.Printf("Updated to newest PSet.\nYou can use \"fixme show\" to check the result.\n")
return nil
}
func updateCache(baseUrl string, fingerprint string, cacheDir string) error {
const NAME = "master.zip"
zipFile := path.Join(cacheDir, NAME)
if !checkFileChanged(zipFile, fingerprint) {
fmt.Printf("cache is newest --> %q\n", fingerprint)
return nil
}
err := downloadFile(baseUrl+"/"+fingerprint, zipFile, fingerprint)
if err != nil {
return err
}
scriptDir := path.Join(cacheDir, ScriptDirName)
os.RemoveAll(scriptDir)
return uncompress(scriptDir, zipFile)
}
func uncompress(destDir string, zipFile string) error {
r, err := zip.OpenReader(zipFile)
if err != nil {
return err
}
write := func(name string, z *zip.File) error {
os.MkdirAll(path.Dir(name), 0755)
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer f.Close()
r, err := z.Open()
if err != nil {
return err
}
defer r.Close()
_, err = io.Copy(f, r)
return err
}
pathShift := func(name string) string {
fs := strings.Split(name, "/")
if len(fs) > 1 {
return strings.Join(fs[1:], string(os.PathSeparator))
}
return strings.Join(fs, string(os.PathSeparator))
}
for _, f := range r.File {
name := pathShift(f.Name)
dname := path.Join(destDir, name)
if name == "functions" {
err = write(dname, f)
if err != nil {
return err
}
continue
}
if f.FileInfo().IsDir() || path.Base(name) != ScriptFix {
continue
}
err := write(dname, f)
if err != nil {
return err
}
}
return nil
}