forked from getsops/sops
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request getsops#549 from dnozay/pr-548
fix for getsops#548 - handle .ini files in `decrypt.Data`, add other helper
- Loading branch information
Showing
5 changed files
with
188 additions
and
80 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package formats | ||
|
||
import "strings" | ||
|
||
// Format is an enum type | ||
type Format int | ||
|
||
const ( | ||
Binary Format = iota | ||
Dotenv | ||
Ini | ||
Json | ||
Yaml | ||
) | ||
|
||
var stringToFormat = map[string]Format{ | ||
"binary": Binary, | ||
"dotenv": Dotenv, | ||
"ini": Ini, | ||
"json": Json, | ||
"yaml": Yaml, | ||
} | ||
|
||
// FormatFromString returns a Format from a string. | ||
// This is used for converting string cli options. | ||
func FormatFromString(formatString string) Format { | ||
format, found := stringToFormat[formatString] | ||
if !found { | ||
return Binary | ||
} | ||
return format | ||
} | ||
|
||
// IsYAMLFile returns true if a given file path corresponds to a YAML file | ||
func IsYAMLFile(path string) bool { | ||
return strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") | ||
} | ||
|
||
// IsJSONFile returns true if a given file path corresponds to a JSON file | ||
func IsJSONFile(path string) bool { | ||
return strings.HasSuffix(path, ".json") | ||
} | ||
|
||
// IsEnvFile returns true if a given file path corresponds to a .env file | ||
func IsEnvFile(path string) bool { | ||
return strings.HasSuffix(path, ".env") | ||
} | ||
|
||
// IsIniFile returns true if a given file path corresponds to a INI file | ||
func IsIniFile(path string) bool { | ||
return strings.HasSuffix(path, ".ini") | ||
} | ||
|
||
// FormatForPath returns the correct format given the path to a file | ||
func FormatForPath(path string) Format { | ||
format := Binary // default | ||
if IsYAMLFile(path) { | ||
format = Yaml | ||
} else if IsJSONFile(path) { | ||
format = Json | ||
} else if IsEnvFile(path) { | ||
format = Dotenv | ||
} else if IsIniFile(path) { | ||
format = Ini | ||
} | ||
return format | ||
} | ||
|
||
// FormatForPathOrString returns the correct format-specific implementation | ||
// of the Store interface given the formatString if specified, or the path to a file. | ||
// This is to support the cli, where both are provided. | ||
func FormatForPathOrString(path, format string) Format { | ||
formatFmt, found := stringToFormat[format] | ||
if !found { | ||
formatFmt = FormatForPath(path) | ||
} | ||
return formatFmt | ||
} |
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,39 @@ | ||
package formats | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFormatFromString(t *testing.T) { | ||
assert.Equal(t, Binary, FormatFromString("foobar")) | ||
assert.Equal(t, Dotenv, FormatFromString("dotenv")) | ||
assert.Equal(t, Ini, FormatFromString("ini")) | ||
assert.Equal(t, Yaml, FormatFromString("yaml")) | ||
assert.Equal(t, Json, FormatFromString("json")) | ||
} | ||
|
||
func TestFormatForPath(t *testing.T) { | ||
assert.Equal(t, Binary, FormatForPath("/path/to/foobar")) | ||
assert.Equal(t, Dotenv, FormatForPath("/path/to/foobar.env")) | ||
assert.Equal(t, Ini, FormatForPath("/path/to/foobar.ini")) | ||
assert.Equal(t, Json, FormatForPath("/path/to/foobar.json")) | ||
assert.Equal(t, Yaml, FormatForPath("/path/to/foobar.yml")) | ||
assert.Equal(t, Yaml, FormatForPath("/path/to/foobar.yaml")) | ||
} | ||
|
||
func TestFormatForPathOrString(t *testing.T) { | ||
assert.Equal(t, Binary, FormatForPathOrString("/path/to/foobar", "")) | ||
assert.Equal(t, Dotenv, FormatForPathOrString("/path/to/foobar", "dotenv")) | ||
assert.Equal(t, Dotenv, FormatForPathOrString("/path/to/foobar.env", "")) | ||
assert.Equal(t, Ini, FormatForPathOrString("/path/to/foobar", "ini")) | ||
assert.Equal(t, Ini, FormatForPathOrString("/path/to/foobar.ini", "")) | ||
assert.Equal(t, Json, FormatForPathOrString("/path/to/foobar", "json")) | ||
assert.Equal(t, Json, FormatForPathOrString("/path/to/foobar.json", "")) | ||
assert.Equal(t, Yaml, FormatForPathOrString("/path/to/foobar", "yaml")) | ||
assert.Equal(t, Yaml, FormatForPathOrString("/path/to/foobar.yml", "")) | ||
|
||
assert.Equal(t, Ini, FormatForPathOrString("/path/to/foobar.yml", "ini")) | ||
assert.Equal(t, Binary, FormatForPathOrString("/path/to/foobar.yml", "binary")) | ||
} |
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