Skip to content

Commit

Permalink
files2.0 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Nov 16, 2018
1 parent 15ec1a5 commit c987725
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
12 changes: 6 additions & 6 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
}

stringArgs := make([]string, 0, numInputs)
fileArgs := make(map[string]files.File)
fileArgs := make(map[string]files.Node)

// the index of the current argument definition
iArgDef := 0
Expand Down Expand Up @@ -255,7 +255,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
// treat stringArg values as file paths
fpath := inputs[0]
inputs = inputs[1:]
var file files.File
var file files.Node
if fpath == "-" {
r, err := maybeWrapStdin(stdin, msgStdinInfo)
if err != nil {
Expand Down Expand Up @@ -422,17 +422,17 @@ func (st *parseState) parseLongOpt(optDefs map[string]cmdkit.Option) (string, in
optval, err := parseOpt(k, v, optDefs)
return k, optval, err
}
func filesMapToSortedArr(fs map[string]files.File) []files.FileEntry {
func filesMapToSortedArr(fs map[string]files.Node) []files.DirEntry {
var names []string
for name, _ := range fs {
names = append(names, name)
}

sort.Strings(names)

var out []files.FileEntry
var out []files.DirEntry
for _, f := range names {
out = append(out, files.FileEntry{File: fs[f], Name: f})
out = append(out, files.FileEntry(f, fs[f]))
}

return out
Expand All @@ -455,7 +455,7 @@ func getArgDef(i int, argDefs []cmdkit.Argument) *cmdkit.Argument {
const notRecursiveFmtStr = "'%s' is a directory, use the '-%s' flag to specify directories"
const dirNotSupportedFmtStr = "invalid path '%s', argument '%s' does not support directories"

func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (files.File, error) {
func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (files.Node, error) {
fpath = filepath.ToSlash(filepath.Clean(fpath))
if fpath == "." {
cwd, err := os.Getwd()
Expand Down
19 changes: 10 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package cmds
import (
"errors"
"fmt"
"io"
"strings"

"github.com/ipfs/go-ipfs-cmdkit"
Expand Down Expand Up @@ -229,16 +228,18 @@ func (c *Command) CheckArguments(req *Request) error {
lastArg.Type == cmdkit.ArgString &&
req.Files != nil {

_, fi, err := req.Files.NextFile()
switch err {
case io.EOF:
case nil:
req.bodyArgs = newArguments(fi)
it, err := req.Files.Entries()
if err != nil {
return err
}
if it.Next() {
req.bodyArgs = newArguments(it.File())
// Can't pass files and stdin arguments.
req.Files = nil
default:
// io error.
return err
} else {
if it.Err() != nil {
return it.Err()
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,16 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
if bodyArgs := req.BodyArgs(); bodyArgs != nil {
// In the end, this wraps a file reader in a file reader.
// However, such is life.
fileReader = files.NewMultiFileReader(files.NewSliceFile([]files.FileEntry{
{File: files.NewReaderFile(bodyArgs, nil), Name: "stdin"},
}), true)
fileReader, err = files.NewMultiFileReader(files.NewSliceFile([]files.DirEntry{files.FileEntry("stdin", files.NewReaderFile(bodyArgs, nil))}), true)
if err != nil {
return nil, err
}
reader = fileReader
} else if req.Files != nil {
fileReader = files.NewMultiFileReader(req.Files, true)
fileReader, err = files.NewMultiFileReader(req.Files, true)
if err != nil {
return nil, err
}
reader = fileReader
}

Expand Down
2 changes: 1 addition & 1 deletion http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func parseRequest(ctx context.Context, r *http.Request, root *cmds.Command) (*cm
contentType := r.Header.Get(contentTypeHeader)
mediatype, _, _ := mime.ParseMediaType(contentType)

var f files.File
var f files.Directory
if mediatype == "multipart/form-data" {
reader, err := r.MultipartReader()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ type Request struct {
Arguments []string
Options cmdkit.OptMap

Files files.File
Files files.Directory

bodyArgs *arguments
}

// NewRequest returns a request initialized with given arguments
// An non-nil error will be returned if the provided option values are invalid
func NewRequest(ctx context.Context, path []string, opts cmdkit.OptMap, args []string, file files.File, root *Command) (*Request, error) {
func NewRequest(ctx context.Context, path []string, opts cmdkit.OptMap, args []string, file files.Directory, root *Command) (*Request, error) {
if opts == nil {
opts = make(cmdkit.OptMap)
}
Expand Down

0 comments on commit c987725

Please sign in to comment.