fix more issue

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2025-05-08 20:59:57 +08:00 committed by yxxhero
parent c0deedb93e
commit 20ee7330ac
3 changed files with 45 additions and 29 deletions

View File

@ -6,13 +6,14 @@ const (
// use helm status to check if a release exists before installing it
UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE"
DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID"
Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case
Environment = "HELMFILE_ENVIRONMENT"
FilePath = "HELMFILE_FILE_PATH"
TempDir = "HELMFILE_TEMPDIR"
UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED"
GoccyGoYaml = "HELMFILE_GOCCY_GOYAML"
CacheHome = "HELMFILE_CACHE_HOME"
Interactive = "HELMFILE_INTERACTIVE"
DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID"
Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case
Environment = "HELMFILE_ENVIRONMENT"
FilePath = "HELMFILE_FILE_PATH"
TempDir = "HELMFILE_TEMPDIR"
UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED"
GoccyGoYaml = "HELMFILE_GOCCY_GOYAML"
CacheHome = "HELMFILE_CACHE_HOME"
Interactive = "HELMFILE_INTERACTIVE"
EanbleGoccyGoYamlJsonStyle = "HELMFILE_ENABLE_GOCCY_GOYAML_JSON_STYLE"
)

View File

@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
goruntime "runtime"
"testing"
"github.com/stretchr/testify/require"
"github.com/helmfile/helmfile/pkg/envvar"
"github.com/helmfile/helmfile/pkg/filesystem"
"github.com/helmfile/helmfile/pkg/runtime"
)
@ -187,16 +189,24 @@ func TestToYaml_NestedMapInterfaceKey(t *testing.T) {
func TestToYaml(t *testing.T) {
tests := []struct {
name string
input any
expected string
wantErr bool
name string
input any
expected string
wantErr bool
enableJsonStyle bool
}{
{
// https://github.com/helmfile/helmfile/issues/2024
name: "test unmarshalling issue 2024",
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
expected: `thisShouldBeString: "01234567890123456789"
name: "test unmarshalling issue 2024",
enableJsonStyle: true,
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
expected: `'thisShouldBeString': '01234567890123456789'
`,
},
{
name: "test unmarshalling issue 2024 with int64",
input: map[string]any{"thisShouldBeString": 1234567890123456789},
expected: `thisShouldBeString: 1234567890123456789
`,
},
{
@ -237,6 +247,12 @@ func TestToYaml(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.enableJsonStyle {
_ = os.Setenv(envvar.EanbleGoccyGoYamlJsonStyle, "true")
}
defer func() {
_ = os.Unsetenv(envvar.EanbleGoccyGoYamlJsonStyle)
}()
actual, err := ToYaml(tt.input)
if tt.wantErr {
require.Error(t, err)

View File

@ -3,12 +3,12 @@ package yaml
import (
"bytes"
"io"
"strconv"
"strings"
"os"
"github.com/goccy/go-yaml"
v2 "gopkg.in/yaml.v2"
"github.com/helmfile/helmfile/pkg/envvar"
"github.com/helmfile/helmfile/pkg/runtime"
)
@ -68,20 +68,19 @@ func NewDecoder(data []byte, strict bool) func(any) error {
func Marshal(v any) ([]byte, error) {
if runtime.GoccyGoYaml {
var b bytes.Buffer
yamlEncoder := yaml.NewEncoder(
&b,
yamlEncoderOpts := []yaml.EncodeOption{
yaml.Indent(2),
yaml.UseSingleQuote(true),
yaml.UseLiteralStyleIfMultiline(true),
yaml.CustomMarshaler(
func(v string) ([]byte, error) {
if strings.HasPrefix(v, "0") {
// check v is 0xxxx style number.
return []byte(strconv.Quote(v)), nil
}
return []byte(v), nil
},
),
}
// enable JSON style if the envvar is set
if os.Getenv(envvar.EanbleGoccyGoYamlJsonStyle) == "true" {
yamlEncoderOpts = append(yamlEncoderOpts, yaml.JSON(), yaml.Flow(false))
}
yamlEncoder := yaml.NewEncoder(
&b,
yamlEncoderOpts...,
)
err := yamlEncoder.Encode(v)
defer func() {