Skip to content

Commit 429c93b

Browse files
committed
refactor(envfile): extract yaml marshaling
- extract YAML marshaling logic into a new internal method `marshalEnvFileBytes` - introduce new public method `MarshalBody` to return formatted YAML as a string - update `WriteEnvFile` to utilize the new marshaling helper - improve code organization and reusability for YAML content generation
1 parent f43ba64 commit 429c93b

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

core/env/envfile.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type EnvFile struct {
2323
Body string `json:"-" yaml:"-"`
2424
}
2525

26-
func (ef *EnvFile) WriteEnvFile() (err error) {
26+
// marshalEnvFileBytes marshals the EnvFile into formatted YAML bytes
27+
func (ef *EnvFile) marshalEnvFileBytes() ([]byte, error) {
2728
connsMap := yaml.MapSlice{}
2829

2930
// order connections names
@@ -58,21 +59,38 @@ func (ef *EnvFile) WriteEnvFile() (err error) {
5859

5960
envBytes, err := yaml.Marshal(efMap)
6061
if err != nil {
61-
return g.Error(err, "could not marshal into YAML")
62+
return nil, g.Error(err, "could not marshal into YAML")
6263
}
6364

64-
output := []byte(ef.TopComment + string(envBytes))
65+
output := formatYAML([]byte(ef.TopComment + string(envBytes)))
66+
return output, nil
67+
}
68+
69+
func (ef *EnvFile) WriteEnvFile() (err error) {
70+
output, err := ef.marshalEnvFileBytes()
71+
if err != nil {
72+
return err
73+
}
6574

6675
// fix windows path
6776
ef.Path = strings.ReplaceAll(ef.Path, `\`, `/`)
68-
err = os.WriteFile(ef.Path, formatYAML(output), 0644)
77+
err = os.WriteFile(ef.Path, output, 0644)
6978
if err != nil {
7079
return g.Error(err, "could not write YAML file")
7180
}
7281

7382
return
7483
}
7584

85+
// MarshalBody returns the EnvFile as a formatted YAML string
86+
func (ef *EnvFile) MarshalBody() (string, error) {
87+
output, err := ef.marshalEnvFileBytes()
88+
if err != nil {
89+
return "", err
90+
}
91+
return string(output), nil
92+
}
93+
7694
func formatYAML(input []byte) []byte {
7795
newOutput := []byte{}
7896
pIndent := 0

0 commit comments

Comments
 (0)