1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 19:44:01 +08:00

fix some tests

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2015-12-04 17:18:16 -08:00
parent d64f1494a3
commit b2b415b57d
9 changed files with 76 additions and 53 deletions

View File

@ -7,6 +7,7 @@ import (
"path"
"path/filepath"
"runtime"
"sort"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
@ -269,8 +270,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
}
stringArgs := make([]string, 0, numInputs)
fileArgs := make([]files.File, 0, numInputs)
fileArgs := make(map[string]files.File)
argDefIndex := 0 // the index of the current argument definition
for i := 0; i < numInputs; i++ {
argDef := getArgDef(argDefIndex, argDefs)
@ -305,18 +306,21 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
} else if argDef.Type == cmds.ArgFile {
if stdin == nil || !argDef.SupportsStdin {
// treat stringArg values as file paths
fileArgs, inputs, err = appendFile(fileArgs, inputs, argDef, recursive)
fpath := inputs[0]
inputs = inputs[1:]
file, err := appendFile(fpath, argDef, recursive)
if err != nil {
return nil, nil, err
}
fileArgs[fpath] = file
} else {
if len(inputs) > 0 {
// don't use stdin if we have inputs
stdin = nil
} else {
// if we have a stdin, create a file from it
fileArgs, stdin = appendStdinAsFile(fileArgs, stdin)
fileArgs[""] = files.NewReaderFile("", "", stdin, nil)
}
}
}
@ -333,7 +337,23 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
}
}
return stringArgs, fileArgs, nil
return stringArgs, filesMapToSortedArr(fileArgs), nil
}
func filesMapToSortedArr(fs map[string]files.File) []files.File {
var names []string
for name, _ := range fs {
names = append(names, name)
}
sort.Strings(names)
var out []files.File
for _, f := range names {
out = append(out, fs[f])
}
return out
}
func getArgDef(i int, argDefs []cmds.Argument) *cmds.Argument {
@ -366,44 +386,35 @@ func appendStdinAsString(args []string, stdin *os.File) ([]string, *os.File, err
return append(args, strings.Split(input, "\n")...), nil, nil
}
func appendFile(args []files.File, inputs []string, argDef *cmds.Argument, recursive bool) ([]files.File, []string, error) {
fpath := filepath.ToSlash(filepath.Clean(inputs[0]))
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 *cmds.Argument, recursive bool) (files.File, error) {
fpath = filepath.ToSlash(filepath.Clean(fpath))
if fpath == "." {
cwd, err := os.Getwd()
if err != nil {
return nil, nil, err
return nil, err
}
fpath = cwd
}
stat, err := os.Lstat(fpath)
if err != nil {
return nil, nil, err
return nil, err
}
if stat.IsDir() {
if !argDef.Recursive {
err = fmt.Errorf("Invalid path '%s', argument '%s' does not support directories",
fpath, argDef.Name)
return nil, nil, err
return nil, fmt.Errorf(dirNotSupportedFmtStr, fpath, argDef.Name)
}
if !recursive {
err = fmt.Errorf("'%s' is a directory, use the '-%s' flag to specify directories",
fpath, cmds.RecShort)
return nil, nil, err
return nil, fmt.Errorf(notRecursiveFmtStr, fpath, cmds.RecShort)
}
}
arg, err := files.NewSerialFile(path.Base(fpath), fpath, stat)
if err != nil {
return nil, nil, err
}
return append(args, arg), inputs[1:], nil
}
func appendStdinAsFile(args []files.File, stdin *os.File) ([]files.File, *os.File) {
arg := files.NewReaderFile("", "", stdin, nil)
return append(args, arg), nil
return files.NewSerialFile(path.Base(fpath), fpath, stat)
}
// isTerminal returns true if stdin is a Stdin pipe (e.g. `cat file | ipfs`),