mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 18:27:36 +00:00
Merge pull request #2194 from GLEF1X/bugfix/yaml-exporting
fix(cli): make yaml exporting reliable and standardized
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/Infisical/infisical-merge/packages/util"
|
"github.com/Infisical/infisical-merge/packages/util"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -188,7 +189,7 @@ func formatEnvs(envs []models.SingleEnvironmentVariable, format string) (string,
|
|||||||
case FormatCSV:
|
case FormatCSV:
|
||||||
return formatAsCSV(envs), nil
|
return formatAsCSV(envs), nil
|
||||||
case FormatYaml:
|
case FormatYaml:
|
||||||
return formatAsYaml(envs), nil
|
return formatAsYaml(envs)
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("invalid format type: %s. Available format types are [%s]", format, []string{FormatDotenv, FormatJson, FormatCSV, FormatYaml, FormatDotEnvExport})
|
return "", fmt.Errorf("invalid format type: %s. Available format types are [%s]", format, []string{FormatDotenv, FormatJson, FormatCSV, FormatYaml, FormatDotEnvExport})
|
||||||
}
|
}
|
||||||
@@ -224,12 +225,18 @@ func formatAsDotEnvExport(envs []models.SingleEnvironmentVariable) string {
|
|||||||
return dotenv
|
return dotenv
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatAsYaml(envs []models.SingleEnvironmentVariable) string {
|
func formatAsYaml(envs []models.SingleEnvironmentVariable) (string, error) {
|
||||||
var dotenv string
|
m := make(map[string]string)
|
||||||
for _, env := range envs {
|
for _, env := range envs {
|
||||||
dotenv += fmt.Sprintf("%s: %s\n", env.Key, env.Value)
|
m[env.Key] = env.Value
|
||||||
}
|
}
|
||||||
return dotenv
|
|
||||||
|
yamlBytes, err := yaml.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to format environment variables as YAML: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(yamlBytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format environment variables as a JSON file
|
// Format environment variables as a JSON file
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Infisical/infisical-merge/packages/models"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormatAsYaml(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input []models.SingleEnvironmentVariable
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Empty input",
|
||||||
|
input: []models.SingleEnvironmentVariable{},
|
||||||
|
expected: "{}\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Single environment variable",
|
||||||
|
input: []models.SingleEnvironmentVariable{
|
||||||
|
{Key: "KEY1", Value: "VALUE1"},
|
||||||
|
},
|
||||||
|
expected: "KEY1: VALUE1\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Multiple environment variables",
|
||||||
|
input: []models.SingleEnvironmentVariable{
|
||||||
|
{Key: "KEY1", Value: "VALUE1"},
|
||||||
|
{Key: "KEY2", Value: "VALUE2"},
|
||||||
|
{Key: "KEY3", Value: "VALUE3"},
|
||||||
|
},
|
||||||
|
expected: "KEY1: VALUE1\nKEY2: VALUE2\nKEY3: VALUE3\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Overwriting duplicate keys",
|
||||||
|
input: []models.SingleEnvironmentVariable{
|
||||||
|
{Key: "KEY1", Value: "VALUE1"},
|
||||||
|
{Key: "KEY1", Value: "VALUE2"},
|
||||||
|
},
|
||||||
|
expected: "KEY1: VALUE2\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Special characters in values",
|
||||||
|
input: []models.SingleEnvironmentVariable{
|
||||||
|
{Key: "KEY1", Value: "Value with spaces"},
|
||||||
|
{Key: "KEY2", Value: "Value:with:colons"},
|
||||||
|
{Key: "KEY3", Value: "Value\nwith\nnewlines"},
|
||||||
|
},
|
||||||
|
expected: "KEY1: Value with spaces\nKEY2: Value:with:colons\nKEY3: |-\n Value\n with\n newlines\n",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := formatAsYaml(tt.input)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Compare the result with the expected output
|
||||||
|
assert.Equal(t, tt.expected, result)
|
||||||
|
|
||||||
|
// Additionally, parse the result back into a map to ensure it's valid YAML
|
||||||
|
var resultMap map[string]string
|
||||||
|
err = yaml.Unmarshal([]byte(result), &resultMap)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Create an expected map from the input
|
||||||
|
expectedMap := make(map[string]string)
|
||||||
|
for _, env := range tt.input {
|
||||||
|
expectedMap[env.Key] = env.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, expectedMap, resultMap)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user