-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from ipfs/fix/forward-args
forward the remaining of the stdin args to the server
- Loading branch information
Showing
7 changed files
with
237 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cmds | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
) | ||
|
||
// StdinArguments is used to iterate through arguments piped through stdin. | ||
// | ||
// It closely mimics the bufio.Scanner interface but also implements the | ||
// ReadCloser interface. | ||
type StdinArguments interface { | ||
io.ReadCloser | ||
|
||
// Scan reads in the next argument and returns true if there is an | ||
// argument to read. | ||
Scan() bool | ||
|
||
// Argument returns the next argument. | ||
Argument() string | ||
|
||
// Err returns any errors encountered when reading in arguments. | ||
Err() error | ||
} | ||
|
||
type arguments struct { | ||
argument string | ||
err error | ||
reader *bufio.Reader | ||
closer io.Closer | ||
} | ||
|
||
func newArguments(r io.ReadCloser) *arguments { | ||
return &arguments{ | ||
reader: bufio.NewReader(r), | ||
closer: r, | ||
} | ||
} | ||
|
||
// Read implements the io.Reader interface. | ||
func (a *arguments) Read(b []byte) (int, error) { | ||
return a.reader.Read(b) | ||
} | ||
|
||
// Close implements the io.Closer interface. | ||
func (a *arguments) Close() error { | ||
return a.closer.Close() | ||
} | ||
|
||
// WriteTo implements the io.WriterTo interface. | ||
func (a *arguments) WriteTo(w io.Writer) (int64, error) { | ||
return a.reader.WriteTo(w) | ||
} | ||
|
||
// Err returns any errors encountered when reading in arguments. | ||
func (a *arguments) Err() error { | ||
if a.err == io.EOF { | ||
return nil | ||
} | ||
return a.err | ||
} | ||
|
||
// Argument returns the last argument read in. | ||
func (a *arguments) Argument() string { | ||
return a.argument | ||
} | ||
|
||
// Scan reads in the next argument and returns true if there is an | ||
// argument to read. | ||
func (a *arguments) Scan() bool { | ||
if a.err != nil { | ||
return false | ||
} | ||
|
||
s, err := a.reader.ReadString('\n') | ||
if err != nil { | ||
a.err = err | ||
if err == io.EOF && len(s) > 0 { | ||
a.argument = s | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
l := len(s) | ||
if l >= 2 && s[l-2] == '\r' { | ||
a.argument = s[:l-2] | ||
} else { | ||
a.argument = s[:l-1] | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package cmds | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestArguments(t *testing.T) { | ||
var testCases = []struct { | ||
input string | ||
arguments []string | ||
}{ | ||
{ | ||
input: "", | ||
arguments: []string{}, | ||
}, | ||
{ | ||
input: "\n", | ||
arguments: []string{""}, | ||
}, | ||
{ | ||
input: "\r\n", | ||
arguments: []string{""}, | ||
}, | ||
{ | ||
input: "\r", | ||
arguments: []string{"\r"}, | ||
}, | ||
{ | ||
input: "one", | ||
arguments: []string{"one"}, | ||
}, | ||
{ | ||
input: "one\n", | ||
arguments: []string{"one"}, | ||
}, | ||
{ | ||
input: "one\r\n", | ||
arguments: []string{"one"}, | ||
}, | ||
{ | ||
input: "one\r", | ||
arguments: []string{"one\r"}, | ||
}, | ||
{ | ||
input: "one\n\ntwo", | ||
arguments: []string{"one", "", "two"}, | ||
}, | ||
{ | ||
input: "first\nsecond\nthird", | ||
arguments: []string{"first", "second", "third"}, | ||
}, | ||
{ | ||
input: "first\r\nsecond\nthird", | ||
arguments: []string{"first", "second", "third"}, | ||
}, | ||
{ | ||
input: "first\nsecond\nthird\n", | ||
arguments: []string{"first", "second", "third"}, | ||
}, | ||
{ | ||
input: "first\r\nsecond\r\nthird\r\n", | ||
arguments: []string{"first", "second", "third"}, | ||
}, | ||
{ | ||
input: "first\nsecond\nthird\n\n", | ||
arguments: []string{"first", "second", "third", ""}, | ||
}, | ||
{ | ||
input: "\nfirst\nsecond\nthird\n", | ||
arguments: []string{"", "first", "second", "third"}, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
for cut := 0; cut <= len(tc.arguments); cut++ { | ||
args := newArguments(ioutil.NopCloser(bytes.NewBufferString(tc.input))) | ||
for j, arg := range tc.arguments[:cut] { | ||
if !args.Scan() { | ||
t.Errorf("in test case %d, missing argument %d", i, j) | ||
continue | ||
} | ||
got := args.Argument() | ||
if got != arg { | ||
t.Errorf("in test case %d, expected argument %d to be %s, got %s", i, j, arg, got) | ||
} | ||
if args.Err() != nil { | ||
t.Error(args.Err()) | ||
} | ||
} | ||
args = newArguments(args) | ||
// Tests stopping in the middle. | ||
for j, arg := range tc.arguments[cut:] { | ||
if !args.Scan() { | ||
t.Errorf("in test case %d, missing argument %d", i, j+cut) | ||
continue | ||
} | ||
got := args.Argument() | ||
if got != arg { | ||
t.Errorf("in test case %d, expected argument %d to be %s, got %s", i, j+cut, arg, got) | ||
} | ||
if args.Err() != nil { | ||
t.Error(args.Err()) | ||
} | ||
} | ||
if args.Scan() { | ||
t.Errorf("in test case %d, got too many arguments", i) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters