Skip to content

Commit ddb6463

Browse files
committed
refactor: remove auto ordering
Signed-off-by: Youngjin Jo <[email protected]>
1 parent 7a7d5ec commit ddb6463

File tree

1 file changed

+74
-7
lines changed

1 file changed

+74
-7
lines changed

cmd/other/setting.go

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ var envCmd = &cobra.Command{
317317
// Update only the environment field in app setting
318318
appV.Set("environment", switchEnv)
319319

320-
if err := appV.WriteConfig(); err != nil {
321-
pterm.Error.Printf("Failed to update environment in setting.yaml: %v", err)
320+
if err := WriteConfigPreservingKeyOrder(appV, appSettingPath); err != nil {
321+
pterm.Error.Printf("Failed to update environment in setting.yaml: %v\n", err)
322322
return
323323
}
324324

@@ -357,19 +357,18 @@ var envCmd = &cobra.Command{
357357
targetViper.Set("environments", envMap)
358358

359359
// Write the updated configuration back to the respective setting file
360-
if err := targetViper.WriteConfig(); err != nil {
361-
pterm.Error.Printf("Failed to update setting file '%s': %v", targetSettingPath, err)
360+
if err := WriteConfigPreservingKeyOrder(targetViper, targetSettingPath); err != nil {
361+
pterm.Error.Printf("Failed to update setting file '%s': %v\n", targetSettingPath, err)
362362
return
363363
}
364364

365365
// If the deleted environment was the current one, unset it
366366
if currentEnv == removeEnv {
367367
appV.Set("environment", "")
368-
if err := appV.WriteConfig(); err != nil {
369-
pterm.Error.Printf("Failed to update environment in setting.yaml: %v", err)
368+
if err := WriteConfigPreservingKeyOrder(appV, appSettingPath); err != nil {
369+
pterm.Error.Printf("Failed to clear current environment: %v\n", err)
370370
return
371371
}
372-
pterm.Info.WithShowLineNumber(false).Println("Cleared current environment in setting.yaml")
373372
}
374373

375374
// Display success message
@@ -1570,6 +1569,74 @@ func convertToSlice(s []interface{}) []interface{} {
15701569
return result
15711570
}
15721571

1572+
func WriteConfigPreservingKeyOrder(v *viper.Viper, path string) error {
1573+
allSettings := v.AllSettings()
1574+
1575+
rawBytes, err := yaml.Marshal(allSettings)
1576+
if err != nil {
1577+
return fmt.Errorf("failed to marshal viper data: %w", err)
1578+
}
1579+
1580+
var rootNode yaml.Node
1581+
if err := yaml.Unmarshal(rawBytes, &rootNode); err != nil {
1582+
return fmt.Errorf("failed to unmarshal into yaml.Node: %w", err)
1583+
}
1584+
1585+
reorderRootNode(&rootNode)
1586+
1587+
reorderedBytes, err := yaml.Marshal(&rootNode)
1588+
if err != nil {
1589+
return fmt.Errorf("failed to marshal reordered yaml.Node: %w", err)
1590+
}
1591+
1592+
if err := os.WriteFile(path, reorderedBytes, 0644); err != nil {
1593+
return fmt.Errorf("failed to write config file: %w", err)
1594+
}
1595+
1596+
return nil
1597+
}
1598+
1599+
func reorderRootNode(doc *yaml.Node) {
1600+
if doc.Kind != yaml.DocumentNode || len(doc.Content) == 0 {
1601+
return
1602+
}
1603+
1604+
rootMap := doc.Content[0]
1605+
if rootMap.Kind != yaml.MappingNode {
1606+
return
1607+
}
1608+
1609+
// rootMap.Content 에는 [keyNode, valNode, keyNode, valNode, ...] 순
1610+
var newContent []*yaml.Node
1611+
var aliasesKV []*yaml.Node
1612+
var environmentKV []*yaml.Node
1613+
var environmentsKV []*yaml.Node
1614+
var otherKVs []*yaml.Node
1615+
1616+
for i := 0; i < len(rootMap.Content); i += 2 {
1617+
keyNode := rootMap.Content[i]
1618+
valNode := rootMap.Content[i+1]
1619+
1620+
switch keyNode.Value {
1621+
case "aliases":
1622+
aliasesKV = append(aliasesKV, keyNode, valNode)
1623+
case "environment":
1624+
environmentKV = append(environmentKV, keyNode, valNode)
1625+
case "environments":
1626+
environmentsKV = append(environmentsKV, keyNode, valNode)
1627+
default:
1628+
otherKVs = append(otherKVs, keyNode, valNode)
1629+
}
1630+
}
1631+
1632+
newContent = append(newContent, environmentKV...)
1633+
newContent = append(newContent, environmentsKV...)
1634+
newContent = append(newContent, otherKVs...)
1635+
newContent = append(newContent, aliasesKV...)
1636+
1637+
rootMap.Content = newContent
1638+
}
1639+
15731640
func init() {
15741641
SettingCmd.AddCommand(settingInitCmd)
15751642
SettingCmd.AddCommand(settingEndpointCmd)

0 commit comments

Comments
 (0)