Skip to content

Commit 00909c3

Browse files
committed
add tools for caching
1 parent 1cac76d commit 00909c3

File tree

7 files changed

+329
-16
lines changed

7 files changed

+329
-16
lines changed

cache.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright (C) 2018 Storj Labs, Inc.
2+
// See LICENSE for copying information.
3+
4+
package main
5+
6+
import (
7+
"errors"
8+
"flag"
9+
"fmt"
10+
"io/ioutil"
11+
"os"
12+
"path/filepath"
13+
)
14+
15+
type Cache struct {
16+
Common
17+
}
18+
19+
func (cmd *Cache) Name() string { return "cache" }
20+
21+
func (cmd *Cache) Parse(args []string) error {
22+
set := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
23+
return set.Parse(args)
24+
}
25+
26+
func (cmd *Cache) Exec() {
27+
fmt.Fprintf(os.Stderr, "# Caching\n")
28+
repodir := cmd.RepoDir()
29+
30+
hash, err := HashFiles(
31+
filepath.Join(repodir, "go.mod"),
32+
filepath.Join(repodir, "go.sum"))
33+
ErrFatal(err)
34+
35+
fmt.Println("HASH:", hash)
36+
37+
cmd.VendorModules()
38+
zipdata, err := Zip(filepath.Join(repodir, "vendor"))
39+
ErrFatal(err)
40+
41+
ioutil.WriteFile(filepath.Join(repodir, "vendor.zip"), zipdata, 0755)
42+
43+
err = Unzip(zipdata, filepath.Join(repodir, "vendor2"))
44+
ErrFatal(err)
45+
}
46+
47+
type Hash struct {
48+
Common
49+
}
50+
51+
func (cmd *Hash) Name() string { return "hash" }
52+
53+
func (cmd *Hash) Parse(args []string) error {
54+
set := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
55+
return set.Parse(args)
56+
}
57+
58+
func (cmd *Hash) Exec() {
59+
repodir := cmd.RepoDir()
60+
61+
hash, err := HashFiles(
62+
filepath.Join(repodir, "go.mod"),
63+
filepath.Join(repodir, "go.sum"))
64+
ErrFatal(err)
65+
66+
fmt.Println(hash)
67+
}
68+
69+
type ZipVendor struct {
70+
Common
71+
Destination string
72+
}
73+
74+
func (cmd *ZipVendor) Name() string { return "zip-vendor" }
75+
76+
func (cmd *ZipVendor) Parse(args []string) error {
77+
set := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
78+
err := set.Parse(args)
79+
if err != nil {
80+
return err
81+
}
82+
83+
cmd.Destination = set.Arg(0)
84+
if cmd.Destination == "" {
85+
return errors.New("destination file unspecified")
86+
}
87+
88+
return nil
89+
}
90+
91+
func (cmd *ZipVendor) Exec() {
92+
fmt.Fprintf(os.Stderr, "# Zipping vendor\n")
93+
94+
cmd.VendorModules()
95+
96+
zipdata, err := Zip(filepath.Join(cmd.RepoDir(), "vendor"))
97+
ErrFatalf(err, "unable to zip vendor: %v", err)
98+
99+
cmd.DeleteVendor()
100+
101+
writeErr := ioutil.WriteFile(cmd.Destination, zipdata, 0644)
102+
ErrFatalf(writeErr, "unable to write zip: %v", writeErr)
103+
}
104+
105+
type UnzipVendor struct {
106+
Common
107+
Source string
108+
}
109+
110+
func (cmd *UnzipVendor) Name() string { return "unzip-vendor" }
111+
112+
func (cmd *UnzipVendor) Parse(args []string) error {
113+
set := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
114+
err := set.Parse(args)
115+
if err != nil {
116+
return err
117+
}
118+
119+
cmd.Source = set.Arg(0)
120+
if cmd.Source == "" {
121+
return errors.New("source file unspecified")
122+
}
123+
124+
return nil
125+
}
126+
127+
func (cmd *UnzipVendor) Exec() {
128+
fmt.Fprintf(os.Stderr, "# Unzipping vendor\n")
129+
zipdata, err := ioutil.ReadFile(cmd.Source)
130+
ErrFatalf(err, "unable to read zip: %v", err)
131+
132+
cmd.DeleteVendor()
133+
134+
unzipErr := Unzip(zipdata, filepath.Join(cmd.RepoDir(), "vendor"))
135+
ErrFatalf(unzipErr, "unable to unzip: %v", unzipErr)
136+
}
137+
138+
type FlattenVendor struct {
139+
Common
140+
Source string
141+
}
142+
143+
func (cmd *FlattenVendor) Name() string { return "flatten-vendor" }
144+
145+
func (cmd *FlattenVendor) Parse(args []string) error {
146+
set := flag.NewFlagSet(cmd.Name(), flag.ExitOnError)
147+
return set.Parse(args)
148+
}
149+
150+
func (cmd *FlattenVendor) Exec() {
151+
cmd.DeleteNonRepos()
152+
cmd.FlattenVendor()
153+
}

common.go

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (C) 2018 Storj Labs, Inc.
2+
// See LICENSE for copying information.
13
package main
24

35
import (
@@ -11,6 +13,8 @@ import (
1113
)
1214

1315
func (cmd Common) DeleteNonRepos() {
16+
fmt.Fprintf(os.Stderr, "# Cleaning up src\n")
17+
1418
var err error
1519
srcdir := cmd.Path("src")
1620

@@ -29,7 +33,7 @@ func (cmd Common) DeleteNonRepos() {
2933
}
3034
return nil
3135
})
32-
ErrFatalf(err, "collect failed: %q", err)
36+
ErrFatalf(err, "collect failed: %v", err)
3337

3438
err = filepath.Walk(srcdir,
3539
func(path string, info os.FileInfo, err error) error {
@@ -58,44 +62,55 @@ func (cmd Common) DeleteNonRepos() {
5862
}
5963
return filepath.SkipDir
6064
})
61-
ErrFatalf(err, "remove failed: %q", err)
65+
ErrFatalf(err, "remove failed: %v", err)
6266
}
6367

6468
func (cmd Common) VendorModules() {
65-
fmt.Fprintf(os.Stdout, "# Vendoring modules\n")
69+
fmt.Fprintf(os.Stderr, "# Vendoring modules\n")
6670

6771
workdir, err := os.Getwd()
68-
ErrFatalf(err, "unable to get working directory: %q\n", err)
72+
ErrFatalf(err, "unable to get working directory: %v\n", err)
6973

7074
defer func() {
7175
err = os.Chdir(workdir)
72-
ErrFatalf(err, "unable to change directory: %q\n", err)
76+
ErrFatalf(err, "unable to change directory: %v\n", err)
7377
}()
7478

7579
err = os.Chdir(cmd.RepoDir())
76-
ErrFatalf(err, "unable to change directory: %q\n", err)
80+
ErrFatalf(err, "unable to change directory: %v\n", err)
7781

7882
err = os.RemoveAll("vendor")
7983
if os.IsNotExist(err) {
8084
err = nil
8185
}
82-
ErrFatalf(err, "unable to delete vendor: %q\n", err)
86+
ErrFatalf(err, "unable to delete vendor: %v\n", err)
8387

8488
for repeat := 2; repeat > 0; repeat-- {
8589
gomod := exec.Command("go", "mod", "vendor", "-v")
8690
gomod.Env = append(os.Environ(), "GO111MODULE=on")
87-
gomod.Stdout, gomod.Stderr = os.Stdout, os.Stderr
91+
gomod.Stdout, gomod.Stderr = os.Stderr, os.Stderr
8892
err = gomod.Run()
89-
Errf(err, "go mod vendor failed, retrying: %q\n", err)
93+
Errf(err, "go mod vendor failed, retrying: %v\n", err)
9094
if err == nil {
9195
break
9296
}
9397
}
94-
ErrFatalf(err, "go mod vendor failed: %q\n", err)
98+
ErrFatalf(err, "go mod vendor failed: %v\n", err)
99+
}
100+
101+
func (cmd Common) DeleteVendor() {
102+
fmt.Fprintf(os.Stderr, "# Deleting vendor\n")
103+
104+
vendordir := filepath.Join(cmd.RepoDir(), "vendor")
105+
removeErr := os.RemoveAll(vendordir)
106+
if os.IsNotExist(removeErr) {
107+
removeErr = nil
108+
}
109+
ErrFatalf(removeErr, "unable to delete vendor: %v\n", removeErr)
95110
}
96111

97112
func (cmd Common) FlattenVendor() {
98-
fmt.Fprintf(os.Stdout, "# Flattening vendor\n")
113+
fmt.Fprintf(os.Stderr, "# Flattening vendor\n")
99114

100115
vendordir := filepath.Join(cmd.RepoDir(), "vendor")
101116
srcdir := cmd.Path("src")
@@ -116,10 +131,10 @@ func (cmd Common) FlattenVendor() {
116131
}
117132
return filepath.SkipDir
118133
})
119-
ErrFatalf(err, "rename failed: %q", err)
134+
ErrFatalf(err, "rename failed: %v", err)
120135

121136
err = os.Remove(vendordir)
122-
ErrFatalf(err, "unable to delete vendor: %q", err)
137+
ErrFatalf(err, "unable to delete vendor: %v", err)
123138
}
124139

125140
func ReadModules(path string) []string {
@@ -129,7 +144,7 @@ func ReadModules(path string) []string {
129144
}
130145

131146
if err != nil {
132-
ErrFatalf(err, "unable to read modules.txt: %q", err)
147+
ErrFatalf(err, "unable to read modules.txt: %v", err)
133148
}
134149

135150
unsorted := []string{}

main.go

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (C) 2018 Storj Labs, Inc.
2+
// See LICENSE for copying information.
3+
14
package main
25

36
import (
@@ -75,6 +78,12 @@ func main() {
7578
cmds := []Command{
7679
&Setup{Common: common},
7780
&Update{Common: common},
81+
// &Cache{Common: common},
82+
83+
&Hash{Common: common},
84+
&ZipVendor{Common: common},
85+
&UnzipVendor{Common: common},
86+
&FlattenVendor{Common: common},
7887
}
7988

8089
for _, cmd := range cmds {

setup.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (C) 2018 Storj Labs, Inc.
2+
// See LICENSE for copying information.
3+
14
package main
25

36
import (
@@ -22,7 +25,7 @@ func (cmd *Setup) Parse(args []string) error {
2225

2326
func (cmd *Setup) Exec() {
2427
if !cmd.Overwrite && !Exists(cmd.Path("src")) {
25-
fmt.Fprintf(os.Stderr, "src directory %q already setup", cmd.Path("src"))
28+
fmt.Fprintf(os.Stderr, "src directory %v already setup", cmd.Path("src"))
2629
os.Exit(1)
2730
}
2831

update.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (C) 2018 Storj Labs, Inc.
2+
// See LICENSE for copying information.
3+
14
package main
25

36
import (
@@ -18,7 +21,7 @@ func (cmd *Update) Parse(args []string) error {
1821
func (cmd *Update) Exec() {
1922
srcdir := cmd.Path("src")
2023
if !Exists(srcdir) {
21-
Fatalf("src directory %q missing, run setup", srcdir)
24+
Fatalf("src directory %v missing, run setup", srcdir)
2225
}
2326

2427
cmd.DeleteNonRepos()

util.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (C) 2018 Storj Labs, Inc.
2+
// See LICENSE for copying information.
3+
14
package main
25

36
import (

0 commit comments

Comments
 (0)