forked from gopasspw/gopass
/
fsck.go
68 lines (58 loc) · 1.69 KB
/
fsck.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
package action
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/gopasspw/gopass/pkg/config"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/fsutil"
"github.com/gopasspw/gopass/pkg/out"
"github.com/muesli/goprogressbar"
"github.com/urfave/cli"
)
// Fsck checks the store integrity
func (s *Action) Fsck(ctx context.Context, c *cli.Context) error {
out.Print(ctx, "Checking store integrity ...")
// make sure config is in the right place
// we may have loaded it from one of the fallback locations
if err := s.cfg.Save(); err != nil {
return ExitError(ctx, ExitConfig, err, "failed to save config: %s", err)
}
// clean up any previous config locations
oldCfg := filepath.Join(config.Homedir(), ".gopass.yml")
if fsutil.IsFile(oldCfg) {
if err := os.Remove(oldCfg); err != nil {
out.Error(ctx, "Failed to remove old gopass config %s: %s", oldCfg, err)
}
}
// display progress bar
t, err := s.Store.Tree(ctx)
if err != nil {
return ExitError(ctx, ExitUnknown, err, "failed to list stores: %s", err)
}
pwList := t.List(0)
bar := &goprogressbar.ProgressBar{
Total: int64(len(pwList) * 2),
Width: 120,
}
if out.IsHidden(ctx) {
old := goprogressbar.Stdout
goprogressbar.Stdout = ioutil.Discard
defer func() {
goprogressbar.Stdout = old
}()
}
ctx = ctxutil.WithProgressCallback(ctx, func() {
bar.Current++
bar.Text = fmt.Sprintf("%d of %d objects checked", bar.Current, bar.Total)
bar.LazyPrint()
})
ctx = out.AddPrefix(ctx, "\n")
// the main work in done by the sub stores
if err := s.Store.Fsck(ctx, c.Args().Get(0)); err != nil {
return ExitError(ctx, ExitFsck, err, "fsck found errors: %s", err)
}
return nil
}