forked from folbricht/desync
/
prune.go
99 lines (84 loc) · 2.46 KB
/
prune.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"github.com/folbricht/desync"
)
const pruneUsage = `desync prune [options] <index> [<index>..]
Read chunk IDs in from index files and delete any chunks from a local (or s3)
store that are not referenced in the index files. Use '-' to read a single index
from STDIN.`
func prune(ctx context.Context, args []string) error {
var (
storeLocation string
accepted bool
clientCert string
clientKey string
)
flags := flag.NewFlagSet("prune", flag.ExitOnError)
flags.Usage = func() {
fmt.Fprintln(os.Stderr, pruneUsage)
flags.PrintDefaults()
}
flags.StringVar(&storeLocation, "s", "", "local or s3 store")
flags.BoolVar(&accepted, "y", false, "do not ask for confirmation")
flags.StringVar(&clientCert, "clientCert", "", "Path to Client Certificate for TLS authentication")
flags.StringVar(&clientKey, "clientKey", "", "Path to Client Key for TLS authentication")
flags.Parse(args)
if flags.NArg() < 1 {
return errors.New("Not enough arguments. See -h for help.")
}
if storeLocation == "" {
return errors.New("No store provided.")
}
if clientKey != "" && clientCert == "" || clientCert != "" && clientKey == "" {
return errors.New("-clientKey and -clientCert options need to be provided together.")
}
// Parse the store locations, open the stores and add a cache is requested
opts := storeOptions{
clientCert: clientCert,
clientKey: clientKey,
}
// Open the target store
sr, err := storeFromLocation(storeLocation, storeOptions{})
if err != nil {
return err
}
defer sr.Close()
// Make sure this store can be used for pruning
s, ok := sr.(desync.PruneStore)
if !ok {
return fmt.Errorf("store '%s' does not support pruning", storeLocation)
}
// Read the input files and merge all chunk IDs in a map to de-dup them
ids := make(map[desync.ChunkID]struct{})
for _, name := range flags.Args() {
c, err := readCaibxFile(name, opts)
if err != nil {
return err
}
for _, c := range c.Chunks {
ids[c.ID] = struct{}{}
}
}
// If the -y option wasn't provided, ask the user to confirm
if !accepted {
fmt.Printf("Warning: The provided index files reference %d unique chunks. Are you sure\nyou want to delete all other chunks from '%s'?\n", len(ids), s)
acceptance:
for {
var a string
fmt.Printf("[y/N]: ")
fmt.Fscanln(os.Stdin, &a)
switch a {
case "y", "Y":
break acceptance
case "n", "N", "":
return nil
}
}
}
return s.Prune(ctx, ids)
}