Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

allow passing whole directory as -f argument #166

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import (
"k8s.io/client-go/pkg/runtime"
)

func Generate(files []string) error {
func Generate(paths []string) error {

files, err := GetAllYAMLFiles(paths)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel instead of changing functions here, and getting all YAML files here, I think it makes more sense to do this in cmd/{apply,create,delete,generate}.go, similar to the way if err := ifFilesPassed(InputFiles); err != nil { is being done in those files.
We can create one validateFiles function in cmd/util.go which calls ifFilesPassed and GetAllYAMLFiles since these are common in all of cmd/{apply,create,delete,generate}.go files, and just call validateFiles in all of them.

It will make this behavior more uniform instead of adding in func Generate used by only generate.go and func ExecuteKubectl used by {apply,create,delete}.go.

WDYT?

Copy link
Member Author

@kadel kadel Jul 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was done like this in the first place, but then I changed that, to this.
It's closer to application logic and I think that in cmd/* should be mostly the code that handles argument parsing and validation.
It's also better to have it inside ExecuteKubectl and in Generate as it can't happen that someone will forget to call when using Generate/ExecuteKubectl

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kadel hmm, makes sense to move both ifFilesPassed and GetAllYAMLFiles out of cmd, but that's for another PR. Tracking in #185

if err != nil {
return errors.Wrap(err, "unable to get YAML files")
}

inputs, err := getApplicationsFromFiles(files)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import (
"github.com/pkg/errors"
)

func ExecuteKubectl(files []string, command string) error {
func ExecuteKubectl(paths []string, command string) error {

files, err := GetAllYAMLFiles(paths)
if err != nil {
return errors.Wrap(err, "unable to get YAML files")
}

inputs, err := getApplicationsFromFiles(files)
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -56,3 +58,34 @@ func getApplicationsFromFiles(files []string) ([]inputData, error) {
}
return appData, nil
}

// GetAllYAMLFiles if path in argument is directory get all *.yml and *.yaml files
// in that directory. If path is file just add it to output list as it is.
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 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)
}
}
if len(files) == 0 {
return nil, fmt.Errorf("no *.yml or *.yaml files were found")
}
return files, nil
}
80 changes: 80 additions & 0 deletions pkg/cmd/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)

var matchFiles = []string{
"test.yml",
"foo/bar.yml",
"foo/bar.yaml",
}

// createMatchFiles crates empty files in tmpDir as defined in matchFiles
func createMatchFiles(tmpDir string) error {
for _, file := range matchFiles {
fileName := filepath.Join(tmpDir, file)
// check if parrent directory exists
if _, err := os.Stat(filepath.Dir(fileName)); os.IsNotExist(err) {
// create parrent directory
err = os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
if err != nil {
return err
}
}
// create empty files
_, err := os.OpenFile(fileName, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
}
return nil
}

var matchTests = []struct {
paths []string
result []string
}{
{[]string{"test.yml"}, []string{"test.yml"}},
{[]string{"foo"}, []string{"foo/bar.yml", "foo/bar.yaml"}},
{[]string{"test.yml", "foo"}, []string{"test.yml", "foo/bar.yml", "foo/bar.yaml"}},
}

func TestGetAllYMLFiles(t *testing.T) {

// create temporary dir where all test files will be created
tmpDir, err := ioutil.TempDir("", "matchTest")
if err != nil {
t.Fatal("creating temp dir:", err)
}
defer os.RemoveAll(tmpDir)

createMatchFiles(tmpDir)

for _, test := range matchTests {
var paths []string
var result []string
// prefix all test path with tmpDir
for _, p := range test.paths {
paths = append(paths, filepath.Join(tmpDir, p))
}
// prefix all expected results with tmpDir
for _, p := range test.result {
result = append(result, filepath.Join(tmpDir, p))
}

out, err := GetAllYAMLFiles(paths)
if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(out, result) {
t.Errorf("output doesn't match expected output\n output : %#v \n expected: %#v \n", out, result)
}
}

}