Skip to content

Commit

Permalink
Merge pull request #17 from jsedlace/files-output-to-stdout
Browse files Browse the repository at this point in the history
Get files command improved to be able to output to stdout.
  • Loading branch information
jsedlace authored Aug 16, 2018
2 parents e8b6f2e + 363f161 commit 1f6b069
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
19 changes: 14 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/ioutil"
"os"
)

const stdoutPlaceholder = "-"

var gp = struct {
source string
application string
Expand Down Expand Up @@ -86,11 +89,17 @@ func ExecuteGetFiles(args []string) error {
log.Debug("Config server response:")
log.Debug(string(resp))

if err = ioutil.WriteFile(mapping.destination, resp, 0644); err != nil {
return err
}
if mapping.destination == stdoutPlaceholder {
os.Stdout.Write(resp)
fmt.Println()
log.Debug("Response written to stdout")
} else {
if err = ioutil.WriteFile(mapping.destination, resp, 0644); err != nil {
return err
}

log.Debug("Response written to: ", mapping.destination)
log.Debug("Response written to: ", mapping.destination)
}
}
return nil
}
Expand All @@ -105,7 +114,7 @@ func init() {
getCmd.MarkFlagRequired("source")
getCmd.MarkFlagRequired("application")

getFilesCmd.Flags().VarP(&gp.fileMappings, "files", "f", "files to get in form of source:destination pairs, example '--files application.yaml:config.yaml'")
getFilesCmd.Flags().VarP(&gp.fileMappings, "files", "f", "files to get in form of source:destination pairs, you can use - as a output to stdout, example '--files application.yaml:config.yaml'")
getFilesCmd.MarkFlagRequired("files")

getValuesCmd.Flags().StringVarP(&gp.format, "format", "f", "yaml", "output format might be one of 'json|yaml|properties'")
Expand Down
26 changes: 24 additions & 2 deletions cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ func TestExecuteGetFiles(t *testing.T) {
"app.yaml",
"config.yaml",
"/app/prod/1.0.0/app.yaml"},
{"{\"foo\":\"bar\"}",
"app",
"default",
"master",
"src",
"-",
"/app/default/master/src"},
}

for _, tp := range testParams {
Expand All @@ -68,12 +75,27 @@ func TestExecuteGetFiles(t *testing.T) {
gp.fileMappings = FileMappings{mappings: make([]FileMapping, 1)}
gp.fileMappings.mappings[0] = FileMapping{source: tp.srcFileName, destination: tp.destFileName}

filename := ""
var old *os.File = nil
var temp *os.File = nil
if tp.destFileName == stdoutPlaceholder {
filename = "stdout"
old = os.Stdout // keep backup of the real stdout
temp, _ = os.Create("stdout") // create temp file
os.Stdout = temp
defer func() {
temp.Close()
os.Stdout = old // restoring the real stdout
}()
} else {
filename = tp.destFileName
}
if err := ExecuteGetFiles(nil); err != nil {
t.Error("Execute failed with: ", err)
}

raw, err := ioutil.ReadFile(tp.destFileName)
defer os.Remove(tp.destFileName)
raw, err := ioutil.ReadFile(filename)
defer os.Remove(filename)
if err != nil {
t.Error("Expected to download file: ", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/scccmd_get_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ scccmd get files [flags]
### Options

```
-f, --files FileMappings files to get in form of source:destination pairs, example '--files application.yaml:config.yaml'
-f, --files FileMappings files to get in form of source:destination pairs, you can use - as a output to stdout, example '--files application.yaml:config.yaml'
-h, --help help for files
```

Expand Down

0 comments on commit 1f6b069

Please sign in to comment.