repo gc: use cmds2
License: MIT Signed-off-by: keks <keks@cryptoscope.co>
keks committed
Sep 19, 2018 at 13:20 UTC
aa7494b0b730bed131c54b24c18899178e86f0ab
1 file changed
+41
-65
core/commands/repo.go
+41
-65
@@ -38,7 +38,7 @@ var RepoCmd = &cmds.Command{
38
39
Subcommands: map[string]*cmds.Command{
40
"stat": repoStatCmd,
41
- "gc": lgc.NewCommand(repoGcCmd),
41
+ "gc": repoGcCmd,
42
"fsck": lgc.NewCommand(RepoFsckCmd),
43
"version": lgc.NewCommand(repoVersionCmd),
44
"verify": lgc.NewCommand(repoVerifyCmd),
@@ -51,7 +51,7 @@ type GcResult struct {
51
Error string `json:",omitempty"`
52
}
53
54
-var repoGcCmd = &oldcmds.Command{
54
+var repoGcCmd = &cmds.Command{
55
Helptext: cmdkit.HelpText{
56
Tagline: "Perform a garbage collection sweep on the repo.",
57
ShortDescription: `
@@ -64,87 +64,63 @@ order to reclaim hard disk space.
64
cmdkit.BoolOption("stream-errors", "Stream errors."),
65
cmdkit.BoolOption("quiet", "q", "Write minimal output."),
66
},
67
- Run: func(req oldcmds.Request, res oldcmds.Response) {
68
- n, err := req.InvocContext().GetNode()
67
+ Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
68
+ n, err := cmdenv.GetNode(env)
69
if err != nil {
70
- res.SetError(err, cmdkit.ErrNormal)
71
- return
70
+ return err
71
}
72
74
- streamErrors, _, _ := res.Request().Option("stream-errors").Bool()
75
-
76
- gcOutChan := corerepo.GarbageCollectAsync(n, req.Context())
77
-
78
- outChan := make(chan interface{})
79
- res.SetOutput(outChan)
80
-
81
- go func() {
82
- defer close(outChan)
83
-
84
- if streamErrors {
85
- errs := false
86
- for res := range gcOutChan {
87
- if res.Error != nil {
88
- select {
89
- case outChan <- &GcResult{Error: res.Error.Error()}:
90
- case <-req.Context().Done():
91
- return
92
- }
93
- errs = true
94
- } else {
95
- select {
96
- case outChan <- &GcResult{Key: res.KeyRemoved}:
97
- case <-req.Context().Done():
98
- return
99
- }
100
- }
101
- }
102
- if errs {
103
- res.SetError(errors.New("encountered errors during gc run"), cmdkit.ErrNormal)
104
- }
105
- } else {
106
- err := corerepo.CollectResult(req.Context(), gcOutChan, func(k cid.Cid) {
107
- select {
108
- case outChan <- &GcResult{Key: k}:
109
- case <-req.Context().Done():
110
- }
111
- })
112
- if err != nil {
113
- res.SetError(err, cmdkit.ErrNormal)
73
+ streamErrors, _ := req.Options["stream-errors"].(bool)
74
+
75
+ gcOutChan := corerepo.GarbageCollectAsync(n, req.Context)
76
+
77
+ if streamErrors {
78
+ errs := false
79
+ for res := range gcOutChan {
80
+ if res.Error != nil {
81
+ re.Emit(&GcResult{Error: res.Error.Error()})
82
+ errs = true
83
+ } else {
84
+ re.Emit(&GcResult{Key: res.KeyRemoved})
85
}
86
}
116
- }()
117
- },
118
- Type: GcResult{},
119
- Marshalers: oldcmds.MarshalerMap{
120
- oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
121
- v, err := unwrapOutput(res.Output())
122
- if err != nil {
123
- return nil, err
87
+ if errs {
88
+ return errors.New("encountered errors during gc run")
89
}
125
-
126
- quiet, _, err := res.Request().Option("quiet").Bool()
90
+ } else {
91
+ err := corerepo.CollectResult(req.Context, gcOutChan, func(k cid.Cid) {
92
+ re.Emit(&GcResult{Key: k})
93
+ })
94
if err != nil {
128
- return nil, err
95
+ return err
96
}
97
+ }
98
+
99
+ return nil
100
+ },
101
+ Type: GcResult{},
102
+ Encoders: cmds.EncoderMap{
103
+ cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
104
+ quiet, _ := req.Options["quiet"].(bool)
105
106
obj, ok := v.(*GcResult)
107
if !ok {
133
- return nil, e.TypeErr(obj, v)
108
+ return e.TypeErr(obj, v)
109
}
110
111
if obj.Error != "" {
137
- fmt.Fprintf(res.Stderr(), "Error: %s\n", obj.Error)
138
- return nil, nil
112
+ _, err := fmt.Fprintf(w, "Error: %s\n", obj.Error)
113
+ return err
114
}
115
141
- msg := obj.Key.String() + "\n"
142
- if !quiet {
143
- msg = "removed " + msg
116
+ prefix := "removed "
117
+ if quiet {
118
+ prefix = ""
119
}
120
146
- return bytes.NewBufferString(msg), nil
147
- },
121
+ _, err := fmt.Fprintf(w, "%s%s\n", prefix, obj.Key)
122
+ return err
123
+ }),
124
},
125
}
126