-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
90 lines (82 loc) · 1.72 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
package main
import (
"errors"
"fmt"
"github.com/docopt/docopt-go"
"github.com/eugene-eeo/psync/blockfs"
"os"
"os/user"
"path/filepath"
)
func checkErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "psync: ", err)
os.Exit(1)
}
}
func export(fs *blockfs.FS) {
hashlist, err := fs.Export(os.Stdin)
checkErr(err)
hashlist.WriteTo(os.Stdout)
}
func glue(fs *blockfs.FS, verify bool) {
hashlist, err := blockfs.NewHashList(os.Stdin)
checkErr(err)
for _, checksum := range hashlist {
cat(fs, checksum, verify)
}
}
func cat(fs *blockfs.FS, checksum blockfs.Checksum, verify bool) {
block, err := fs.GetBlock(blockfs.Checksum(checksum))
checkErr(err)
if verify {
if block.Checksum != blockfs.NewChecksum(block.Data) {
checkErr(errors.New("invalid block: " + string(block.Checksum)))
}
}
block.WriteTo(os.Stdout)
}
func main() {
usage := `Block and hashlist tool.
Usage:
psync export
psync glue [--verify]
psync cat [--verify] <checksum>
psync up <addr>
psync get <addr> [--force]
Options:
--force Redownload blocks even if they exist.
--verify Verify block contents.
-h --help Show this screen.`
args, _ := docopt.Parse(usage, nil, true, "psync 0.1.0", false)
user, err := user.Current()
checkErr(err)
fs, err := blockfs.NewFS(filepath.Join(user.HomeDir, ".psync"))
checkErr(err)
if args["export"].(bool) {
export(fs)
}
if args["glue"].(bool) {
glue(fs, args["--verify"].(bool))
}
if args["cat"].(bool) {
cat(
fs,
blockfs.Checksum(args["<checksum>"].(string)),
args["--verify"].(bool),
)
}
if args["up"].(bool) {
Serve(
fs,
args["<addr>"].(string),
)
}
if args["get"].(bool) {
Get(
fs,
args["<addr>"].(string),
args["--force"].(bool),
)
}
}