pin: use new constants instead of literal values
License: MIT Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Christian Couder committed
May 15, 2016 at 17:13 UTC
39f23677f57f693d9909e095cda436d866af5e73
2 files changed
+27
-20
core/commands/pin.go
+7
-1
@@ -11,6 +11,7 @@ import (
11
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
12
dag "github.com/ipfs/go-ipfs/merkledag"
13
path "github.com/ipfs/go-ipfs/path"
14
+ pin "github.com/ipfs/go-ipfs/pin"
15
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
16
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
17
)
@@ -273,7 +274,12 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN
274
return nil, err
275
}
276
276
- pinType, pinned, err := n.Pinning.IsPinnedWithType(k, typeStr)
277
+ mode, ok := pin.StringToPinMode(typeStr)
278
+ if !ok {
279
+ return nil, fmt.Errorf("Invalid pin mode '%s'", typeStr)
280
+ }
281
+
282
+ pinType, pinned, err := n.Pinning.IsPinnedWithType(k, mode)
283
if err != nil {
284
return nil, err
285
}
pin/pin.go
+20
-19
@@ -71,7 +71,7 @@ func StringToPinMode(s string) (PinMode, bool) {
71
72
type Pinner interface {
73
IsPinned(key.Key) (string, bool, error)
74
- IsPinnedWithType(key.Key, string) (string, bool, error)
74
+ IsPinnedWithType(key.Key, PinMode) (string, bool, error)
75
Pin(context.Context, *mdag.Node, bool) error
76
Unpin(context.Context, key.Key, bool) error
77
@@ -164,7 +164,7 @@ var ErrNotPinned = fmt.Errorf("not pinned")
164
func (p *pinner) Unpin(ctx context.Context, k key.Key, recursive bool) error {
165
p.lock.Lock()
166
defer p.lock.Unlock()
167
- reason, pinned, err := p.isPinnedWithType(k, "all")
167
+ reason, pinned, err := p.isPinnedWithType(k, Any)
168
if err != nil {
169
return err
170
}
@@ -197,46 +197,47 @@ func (p *pinner) isInternalPin(key key.Key) bool {
197
func (p *pinner) IsPinned(k key.Key) (string, bool, error) {
198
p.lock.RLock()
199
defer p.lock.RUnlock()
200
- return p.isPinnedWithType(k, "all")
200
+ return p.isPinnedWithType(k, Any)
201
}
202
203
-func (p *pinner) IsPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
203
+func (p *pinner) IsPinnedWithType(k key.Key, mode PinMode) (string, bool, error) {
204
p.lock.RLock()
205
defer p.lock.RUnlock()
206
- return p.isPinnedWithType(k, typeStr)
206
+ return p.isPinnedWithType(k, mode)
207
}
208
209
// isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
210
// intended for use by other pinned methods that already take locks
211
-func (p *pinner) isPinnedWithType(k key.Key, typeStr string) (string, bool, error) {
212
- switch typeStr {
213
- case "all", "direct", "indirect", "recursive", "internal":
211
+func (p *pinner) isPinnedWithType(k key.Key, mode PinMode) (string, bool, error) {
212
+ switch mode {
213
+ case Any, Direct, Indirect, Recursive, Internal:
214
default:
215
- err := fmt.Errorf("Invalid type '%s', must be one of {direct, indirect, recursive, internal, all}", typeStr)
215
+ err := fmt.Errorf("Invalid Pin Mode '%d', must be one of {%d, %d, %d, %d, %d}",
216
+ mode, Direct, Indirect, Recursive, Internal, Any)
217
return "", false, err
218
}
218
- if (typeStr == "recursive" || typeStr == "all") && p.recursePin.HasKey(k) {
219
- return "recursive", true, nil
219
+ if (mode == Recursive || mode == Any) && p.recursePin.HasKey(k) {
220
+ return linkRecursive, true, nil
221
}
221
- if typeStr == "recursive" {
222
+ if mode == Recursive {
223
return "", false, nil
224
}
225
225
- if (typeStr == "direct" || typeStr == "all") && p.directPin.HasKey(k) {
226
- return "direct", true, nil
226
+ if (mode == Direct || mode == Any) && p.directPin.HasKey(k) {
227
+ return linkDirect, true, nil
228
}
228
- if typeStr == "direct" {
229
+ if mode == Direct {
230
return "", false, nil
231
}
232
232
- if (typeStr == "internal" || typeStr == "all") && p.isInternalPin(k) {
233
- return "internal", true, nil
233
+ if (mode == Internal || mode == Any) && p.isInternalPin(k) {
234
+ return linkInternal, true, nil
235
}
235
- if typeStr == "internal" {
236
+ if mode == Internal {
237
return "", false, nil
238
}
239
239
- // Default is "indirect"
240
+ // Default is Indirect
241
for _, rk := range p.recursePin.GetKeys() {
242
rnd, err := p.dserv.Get(context.Background(), rk)
243
if err != nil {