Skip to content

Commit

Permalink
clean up previous work a bit
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Jul 8, 2016
1 parent 96433cc commit a6af6c5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 54 deletions.
37 changes: 37 additions & 0 deletions commands/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"bufio"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -80,6 +81,7 @@ type Request interface {
Command() *Command
Values() map[string]interface{}
Stdin() io.Reader
VarArgs(func(string) error) error

ConvertOptions() error
}
Expand Down Expand Up @@ -187,6 +189,41 @@ func (r *request) Context() context.Context {
return r.rctx
}

func (r *request) VarArgs(f func(string) error) error {
var i int
for i = 0; i < len(r.cmd.Arguments); i++ {
if r.cmd.Arguments[i].Variadic {
break
}
}

args := r.arguments[i:]
if len(args) > 0 {
for _, arg := range args {
err := f(arg)
if err != nil {
return err
}
}

return nil
} else {
fi, err := r.files.NextFile()
if err != nil {
return err
}

scan := bufio.NewScanner(fi)
for scan.Scan() {
err := f(scan.Text())
if err != nil {
return err
}
}
return nil
}
}

func getContext(base context.Context, req Request) (context.Context, error) {
tout, found, err := req.Option("timeout").String()
if err != nil {
Expand Down
113 changes: 59 additions & 54 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"bufio"
"bytes"
"fmt"
"io"
Expand Down Expand Up @@ -62,37 +61,25 @@ var addPinCmd = &cmds.Command{
return
}

if len(req.Arguments()) > 0 {
added, err := corerepo.Pin(n, req.Context(), req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
out := make(chan interface{})
go func(ctx context.Context) {
defer close(out)
err := req.VarArgs(func(arg string) error {
added, err := corerepo.Pin(n, ctx, []string{arg}, recursive)
if err != nil {
return err
}

out <- &PinOutput{added}
return nil
})

res.SetOutput(&PinOutput{added})
} else {
fi, err := req.Files().NextFile()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

out := make(chan interface{})
go func(ctx context.Context) {
defer close(out)
scan := bufio.NewScanner(fi)
for scan.Scan() {
added, err := corerepo.Pin(n, ctx, []string{scan.Text()}, recursive)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

out <- &PinOutput{added}
}
}(req.Context())
res.SetOutput((<-chan interface{})(out))
}
}(req.Context())
res.SetOutput((<-chan interface{})(out))
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
Expand All @@ -112,23 +99,20 @@ var addPinCmd = &cmds.Command{
return buf
}

switch out := res.Output().(type) {
case *PinOutput:
return marshalPinOutput(out), nil
case <-chan interface{}:

marshal := func(i interface{}) (io.Reader, error) {
return marshalPinOutput(i.(*PinOutput)), nil
}

return &cmds.ChannelMarshaler{
Res: res,
Marshaler: marshal,
Channel: out,
}, nil
default:
out, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}

marshal := func(i interface{}) (io.Reader, error) {
return marshalPinOutput(i.(*PinOutput)), nil
}

return &cmds.ChannelMarshaler{
Res: res,
Marshaler: marshal,
Channel: out,
}, nil
},
},
}
Expand All @@ -143,7 +127,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
},

Arguments: []cmds.Argument{
cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned."),
cmds.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned.").EnableStdin(),
},
Options: []cmds.Option{
cmds.BoolOption("recursive", "r", "Recursively unpin the object linked to by the specified object(s).").Default(true),
Expand All @@ -163,26 +147,47 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
return
}

removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
out := make(chan interface{})
go func() {
defer close(out)
err = req.VarArgs(func(arg string) error {
removed, err := corerepo.Unpin(n, req.Context(), req.Arguments(), recursive)
if err != nil {
return err
}

res.SetOutput(&PinOutput{removed})
out <- &PinOutput{removed}
return nil
})
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}()

res.SetOutput((<-chan interface{})(out))
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
added, ok := res.Output().(*PinOutput)
outch, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}

buf := new(bytes.Buffer)
for _, k := range added.Pins {
fmt.Fprintf(buf, "unpinned %s\n", k)
marshal := func(i interface{}) (io.Reader, error) {
added := i.(*PinOutput)
buf := new(bytes.Buffer)
for _, k := range added.Pins {
fmt.Fprintf(buf, "unpinned %s\n", k)
}
return buf, nil
}
return buf, nil

return &cmds.ChannelMarshaler{
Res: res,
Marshaler: marshal,
Channel: outch,
}, nil
},
},
}
Expand Down

0 comments on commit a6af6c5

Please sign in to comment.