1
0
mirror of https://github.com/getsops/sops.git synced 2026-02-05 12:45:21 +01:00

add formats

This commit is contained in:
Damien Nozay
2019-10-22 12:19:59 -07:00
parent 559b27c01f
commit 4376ac973c
3 changed files with 121 additions and 4 deletions

View File

@@ -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
}

View File

@@ -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"))
}

View File

@@ -24,8 +24,8 @@ func File(path, format string) (cleartext []byte, err error) {
}
// uses same logic as cli.
format := FormatForPathOrString(path, format)
return DataWithFormat(encryptedData, format)
formatFmt := FormatForPathOrString(path, format)
return DataWithFormat(encryptedData, formatFmt)
}
// DataWithFormat is a helper that takes encrypted data, and a format enum value,
@@ -71,6 +71,6 @@ func DataWithFormat(data []byte, format Format) (cleartext []byte, err error) {
// The format string can be `json`, `yaml`, `ini`, `dotenv` or `binary`.
// If the format string is empty, binary format is assumed.
func Data(data []byte, format string) (cleartext []byte, err error) {
format := FormatFromString(format)
return DataWithFormat(data, format)
formatFmt := FormatFromString(format)
return DataWithFormat(data, formatFmt)
}