Skip to content

Commit

Permalink
Adding method for getting piped input data
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Dec 2, 2017
1 parent d85a5a3 commit 115f92b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions user_interface/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package user_interface

import (
"bufio"
"errors"
"flag"
"fmt"
Expand All @@ -20,6 +21,8 @@ var (
programName string
outputDefault io.Writer
setup SetUp

inputReader *bufio.Reader
)

func init() {
Expand All @@ -32,6 +35,8 @@ func init() {
return inputInfo.Mode()
}

inputReader = bufio.NewReader(os.Stdin)

flag.Usage = customUsage

flag.BoolVar(&setup.ListSubfolders, "subfolders", false, "List all subfolders from a remote folder.")
Expand Down Expand Up @@ -60,6 +65,22 @@ func HasPipedInput() bool {
return getInputFileMode() & os.ModeNamedPipe != 0
}

func GetPipedInputData() string {
var data []rune

for {
rune, _, err := inputReader.ReadRune()

if err != nil && err == io.EOF {
break
}

data = append(data, rune)
}

return string(data)
}

func customUsage() {
usageHeader := "Usage: %s <remote-folder>\n"

Expand Down
17 changes: 17 additions & 0 deletions user_interface/default_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package user_interface

import (
"bufio"
"bytes"
"flag"
"os"
Expand Down Expand Up @@ -174,3 +175,19 @@ func TestHasPipedInput_NotReceivedPipedInput_MustReturnFalse(t *testing.T) {
t.Errorf("Expected [false] but got [true]")
}
}

func TestGetPipedInputData_MustReturnExpectedInput(t *testing.T) {
oldInputReader := inputReader

defer func() { inputReader = oldInputReader }()

expectedInput := "Hello world"

inputReader = bufio.NewReader(bytes.NewBufferString(expectedInput))

got := GetPipedInputData()

if got != expectedInput {
t.Errorf("Expected [%s] but got [%s]", expectedInput, got);
}
}

0 comments on commit 115f92b

Please sign in to comment.