Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Jun 19, 2018
1 parent abf45ff commit f28ad2e
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ Example:
Options: []cmdkit.Option{
cmdkit.StringOption("type", "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\".").WithDefault("all"),
cmdkit.BoolOption("quiet", "q", "Write just hashes of objects."),
cmdkit.BoolOption("stream", "Don't buffer pins before sending."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
n, err := GetNode(env)
Expand All @@ -291,6 +292,7 @@ Example:
}

typeStr, _ := req.Options["type"].(string)
stream, _ := req.Options["stream"].(bool)

switch typeStr {
case "all", "direct", "indirect", "recursive":
Expand All @@ -300,30 +302,61 @@ Example:
return
}

emit := res.Emit
lgcList := map[string]RefObject{}
if !stream {
emit = func(v interface{}) error {
obj := v.(*RefKeyObject)
lgcList[obj.Cid] = RefObject{Type: obj.Type}
return nil
}
}

if len(req.Arguments) > 0 {
err = pinLsKeys(req.Context, req.Arguments, typeStr, n, res.Emit)
err = pinLsKeys(req.Context, req.Arguments, typeStr, n, emit)
} else {
err = pinLsAll(req.Context, typeStr, n, res.Emit)
err = pinLsAll(req.Context, typeStr, n, emit)
}

if !stream {
res.Emit(&RefKeyList{lgcList})
}

if err != nil {
res.SetError(err, cmdkit.ErrNormal)
}
},
Type: RefKeyObject{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
quiet, _ := req.Options["quiet"].(bool)
stream, _ := req.Options["stream"].(bool)

obj, ok := v.(*RefKeyObject)
if stream {
obj, ok := v.(*RefKeyObject)
if !ok {
return e.TypeErr(obj, v)
}
if quiet {
fmt.Fprintf(w, "%s\n", obj.Cid)
} else {
fmt.Fprintf(w, "%s %s\n", obj.Cid, obj.Type)
}
return nil
}

keys, ok := v.(*RefKeyList)
if !ok {
return e.TypeErr(obj, v)
return e.TypeErr(keys, v)
}
if quiet {
fmt.Fprintf(w, "%s\n", obj.Cid)
} else {
fmt.Fprintf(w, "%s %s\n", obj.Cid, obj.Type)

for k, v := range keys.Keys {
if quiet {
fmt.Fprintf(w, "%s\n", k)
} else {
fmt.Fprintf(w, "%s %s\n", k, v.Type)
}
}

return nil
}),
},
Expand Down Expand Up @@ -472,15 +505,21 @@ type RefKeyObject struct {
Type string
}

type RefObject struct {
Type string
}

type RefKeyList struct {
Keys map[string]RefObject
}

func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsNode, emit func(value interface{}) error) error {

mode, ok := pin.StringToMode(typeStr)
if !ok {
return fmt.Errorf("invalid pin mode '%s'", typeStr)
}

keys := make(map[string]struct{})

r := &resolver.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
Expand Down Expand Up @@ -511,7 +550,6 @@ func pinLsKeys(ctx context.Context, args []string, typeStr string, n *core.IpfsN
default:
pinType = "indirect through " + pinType
}
keys[c.String()] = struct{}{}

emit(&RefKeyObject{
Type: pinType,
Expand Down

0 comments on commit f28ad2e

Please sign in to comment.