Skip to content

Commit

Permalink
Merge pull request kedgeproject#450 from surajnarwade/stdin
Browse files Browse the repository at this point in the history
Kedge will read input from stdin
  • Loading branch information
concaf authored Jan 30, 2018
2 parents dee5894 + 36b722d commit b86dfbc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
56 changes: 35 additions & 21 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,25 @@ type inputData struct {

func getApplicationsFromFiles(files []string) ([]inputData, error) {
var appData []inputData

var data []byte
var err error
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrap(err, "file reading failed")
}
file, err := filepath.Abs(file)
if err != nil {
return nil, errors.Wrapf(err, "cannot determine the absolute file path of %q", file)

if file == "-" {
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, errors.Wrap(err, "standard input reading failed")
}

} else {
data, err = ioutil.ReadFile(file)
if err != nil {
return nil, errors.Wrap(err, "file reading failed")
}
file, err := filepath.Abs(file)
if err != nil {
return nil, errors.Wrapf(err, "cannot determine the absolute file path of %q", file)
}
}

// The regular expression takes care of when triple dashes are in the
Expand Down Expand Up @@ -82,23 +92,27 @@ func getApplicationsFromFiles(files []string) ([]inputData, error) {
func GetAllYAMLFiles(paths []string) ([]string, error) {
var files []string
for _, path := range paths {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, errors.Wrapf(err, "can't get file info about %s", path)
}
if fileInfo.IsDir() {
ymlFiles, err := filepath.Glob(filepath.Join(path, "*.yml"))
if path != "-" {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, errors.Wrapf(err, "can't list *.yml files in %s", path)
return nil, errors.Wrapf(err, "can't retrieve file information about %s", path)
}
files = append(files, ymlFiles...)
yamlFiles, err := filepath.Glob(filepath.Join(path, "*.yaml"))
if err != nil {
return nil, errors.Wrapf(err, "can't list *.yaml files in %s", path)
if fileInfo.IsDir() {
ymlFiles, err := filepath.Glob(filepath.Join(path, "*.yml"))
if err != nil {
return nil, errors.Wrapf(err, "can't list *.yml files in %s", path)
}
files = append(files, ymlFiles...)
yamlFiles, err := filepath.Glob(filepath.Join(path, "*.yaml"))
if err != nil {
return nil, errors.Wrapf(err, "can't list *.yaml files in %s", path)
}
files = append(files, yamlFiles...)
} else {
// path is regular file, do nothing and just add it to list of files
files = append(files, path)
}
files = append(files, yamlFiles...)
} else {
// path is regular file, do nothing and just add it to list of files
files = append(files, path)
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ func Test_builderror(t *testing.T) {
}
if strings.TrimSpace(string(output)) != imagename {
t.Errorf("Test Failed")

}
}

func Test_stdin(t *testing.T) {

kjson := `{"name": "httpd","containers": [{"image": "centos/httpd"}]}`
cmdStr := fmt.Sprintf("%s generate -f - <<EOF\n%s\nEOF\n", BinaryLocation, kjson)
subproc := exec.Command("/bin/sh", "-c", cmdStr)
output, err := subproc.Output()
if err != nil {
fmt.Println("Error executing command", err)
}
g, err := ioutil.ReadFile(Fixtures + "stdin/output.yml")
if !bytes.Equal(output, g) {
t.Errorf("Test Failed")
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/cmd/fixtures/stdin/output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: httpd
name: httpd
spec:
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: httpd
name: httpd
spec:
containers:
- image: centos/httpd
name: httpd
resources: {}
status: {}

0 comments on commit b86dfbc

Please sign in to comment.