Skip to content

Commit

Permalink
Merge pull request #145 from Praqma/fix/141
Browse files Browse the repository at this point in the history
allowing yaml and yml files. fixes #141
  • Loading branch information
Sami Alajrami authored Dec 17, 2018
2 parents 4855cef + feff785 commit 97d8cc7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions release.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ func validateRelease(appLabel string, r *release, names map[string]map[string]bo
}

_, err := os.Stat(r.ValuesFile)
if r.ValuesFile != "" && (!isOfType(r.ValuesFile, ".yaml") || err != nil) {
if r.ValuesFile != "" && (!isOfType(r.ValuesFile, []string{".yaml", ".yml"}) || err != nil) {
return false, fmt.Sprintf("valuesFile must be a valid relative (from dsf file) file path for a yaml file, or can be left empty (provided path resolved to %q).", r.ValuesFile)
} else if r.ValuesFile != "" && len(r.ValuesFiles) > 0 {
return false, "valuesFile and valuesFiles should not be used together."
} else if len(r.ValuesFiles) > 0 {
for i, filePath := range r.ValuesFiles {
if _, pathErr := os.Stat(filePath); !isOfType(filePath, ".yaml") || pathErr != nil {
if _, pathErr := os.Stat(filePath); !isOfType(filePath, []string{".yaml", ".yml"}) || pathErr != nil {
return false, fmt.Sprintf("valuesFiles must be valid relative (from dsf file) file paths for a yaml file; path at index %d provided path resolved to %q.", i, filePath)
}
}
}

_, err = os.Stat(r.SecretsFile)
if r.SecretsFile != "" && (!isOfType(r.SecretsFile, ".yaml") || err != nil) {
if r.SecretsFile != "" && (!isOfType(r.SecretsFile, []string{".yaml", ".yml"}) || err != nil) {
return false, fmt.Sprintf("secretsFile must be a valid relative (from dsf file) file path for a yaml file, or can be left empty (provided path resolved to %q).", r.SecretsFile)
} else if r.SecretsFile != "" && len(r.SecretsFiles) > 0 {
return false, "secretsFile and secretsFiles should not be used together."
} else if len(r.SecretsFiles) > 0 {
for _, filePath := range r.SecretsFiles {
if i, pathErr := os.Stat(filePath); !isOfType(filePath, ".yaml") || pathErr != nil {
if i, pathErr := os.Stat(filePath); !isOfType(filePath, []string{".yaml", ".yml"}) || pathErr != nil {
return false, fmt.Sprintf("secretsFiles must be valid relative (from dsf file) file paths for a yaml file; path at index %d provided path resolved to %q.", i, filePath)
}
}
Expand Down
17 changes: 11 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ func toYAML(file string, s *state) {

// invokes either yaml or toml parser considering file extension
func fromFile(file string, s *state) (bool, string) {
if isOfType(file, ".toml") {
if isOfType(file, []string{".toml"}) {
return fromTOML(file, s)
} else if isOfType(file, ".yaml") {
} else if isOfType(file, []string{".yaml", ".yml"}) {
return fromYAML(file, s)
} else {
return false, "State file does not have toml/yaml extension."
}
}

func toFile(file string, s *state) {
if isOfType(file, ".toml") {
if isOfType(file, []string{".toml"}) {
toTOML(file, s)
} else if isOfType(file, ".yaml") {
} else if isOfType(file, []string{".yaml", ".yml"}) {
toYAML(file, s)
} else {
logError("ERROR: State file does not have toml/yaml extension.")
Expand Down Expand Up @@ -208,8 +208,13 @@ func resolvePaths(relativeToFile string, s *state) {

// isOfType checks if the file extension of a filename/path is the same as "filetype".
// isisOfType is case insensitive. filetype should contain the "." e.g. ".yaml"
func isOfType(filename string, filetype string) bool {
return filepath.Ext(strings.ToLower(filename)) == strings.ToLower(filetype)
func isOfType(filename string, filetypes []string) bool {
lowerMap := make(map[string]struct{})
for _, v := range filetypes {
lowerMap[strings.ToLower(v)] = struct{}{}
}
_, result := lowerMap[filepath.Ext(strings.ToLower(filename))]
return result
}

// readFile returns the content of a file as a string.
Expand Down
27 changes: 17 additions & 10 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ func Test_fromYAML_Expand(t *testing.T) {

func Test_isOfType(t *testing.T) {
type args struct {
filename string
filetype string
filename string
filetypes []string
}
tests := []struct {
name string
Expand All @@ -307,29 +307,36 @@ func Test_isOfType(t *testing.T) {
{
name: "test case 1 -- valid xml check",
args: args{
filename: "name.xml",
filetype: ".xml",
filename: "name.xml",
filetypes: []string{".xml"},
},
want: true,
}, {
name: "test case 2 -- valid yaml check",
args: args{
filename: "another_name.yaml",
filetype: ".yaml",
filename: "another_name.yaml",
filetypes: []string{".yaml", ".yml"},
},
want: true,
}, {
name: "test case 3 -- invalid yaml check",
name: "test case 3 -- valid (short) yaml check",
args: args{
filename: "name.xml",
filetype: ".yaml",
filename: "another_name.yml",
filetypes: []string{".yaml", ".yml"},
},
want: true,
}, {
name: "test case 4 -- invalid yaml check",
args: args{
filename: "name.xml",
filetypes: []string{".yaml", ".yml"},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isOfType(tt.args.filename, tt.args.filetype); got != tt.want {
if got := isOfType(tt.args.filename, tt.args.filetypes); got != tt.want {
t.Errorf("isOfType() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 97d8cc7

Please sign in to comment.