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 // use helm status to check if a release exists before installing it
UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE" UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE"
DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID" DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID"
Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case
Environment = "HELMFILE_ENVIRONMENT" Environment = "HELMFILE_ENVIRONMENT"
FilePath = "HELMFILE_FILE_PATH" FilePath = "HELMFILE_FILE_PATH"
TempDir = "HELMFILE_TEMPDIR" TempDir = "HELMFILE_TEMPDIR"
UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED" UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED"
GoccyGoYaml = "HELMFILE_GOCCY_GOYAML" GoccyGoYaml = "HELMFILE_GOCCY_GOYAML"
CacheHome = "HELMFILE_CACHE_HOME" CacheHome = "HELMFILE_CACHE_HOME"
Interactive = "HELMFILE_INTERACTIVE" Interactive = "HELMFILE_INTERACTIVE"
EanbleGoccyGoYamlJsonStyle = "HELMFILE_ENABLE_GOCCY_GOYAML_JSON_STYLE"
) )

View File

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

View File

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