This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
allow passing whole directory as -f argument #166
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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) | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 wayif err := ifFilesPassed(InputFiles); err != nil {
is being done in those files.We can create one
validateFiles
function incmd/util.go
which callsifFilesPassed
andGetAllYAMLFiles
since these are common in all ofcmd/{apply,create,delete,generate}.go
files, and just callvalidateFiles
in all of them.It will make this behavior more uniform instead of adding in
func Generate
used by only generate.go andfunc ExecuteKubectl
used by {apply,create,delete}.go.WDYT?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
andGetAllYAMLFiles
out of cmd, but that's for another PR. Tracking in #185