编辑时在 YAML 文件中保留单引号。去吧

问题描述 投票:0回答:1

我想编辑 YAML 文件中某些键的值,同时保持其余键不变。我编写了一个片段来为这些键插入一些值,但生成的新文件不维护单引号 (')。如何避免这种情况?

我的代码:

func updateVariables(nameValue, nameCluster string) error {

yamlFile, err := os.ReadFile("path")
if err != nil {
    return fmt.Errorf("Error reading YAML file: %v", err)
}

var config PipelineConfig

err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
    return fmt.Errorf("Error parsing YAML file: %v", err)
}

for i := range config.Variables {
    switch config.Variables[i].Name {
    case "app_name":
        config.Variables[i].Value = nameValue
    case "cluster_name":
        config.Variables[i].Value = nameCluster

    }
}

modifiedYAML, err := yaml.Marshal(&config,)
if err != nil {
    return fmt.Errorf("Error converting structure to YAML: %v", err)
}

err = os.WriteFile("path", modifiedYAML, 0644)
if err != nil {
    return fmt.Errorf("Error writing modified YAML file: %v", err)
}
fmt.Println("File YAML modified.")
return nil

}`

我的结构:

type PipelineConfig struct {
Trigger   string `yaml:"trigger"`
Variables []struct {
    Name  string `yaml:"name"`
    Value string `yaml:"value"`
} `yaml:"variables"`

Stages []struct {
    Template   string `yaml:"template"`
    Parameters struct {
        AppName       string `yaml:"app_name"`
        AppRepoBranch string `yaml:"app_repo_branch"`
        LocationEnv   string `yaml:"location_env"`
        ComponentName string `yaml:"component_name"`
        ClusterName   string `yaml:"cluster_name"`
    } `yaml:"parameters"`
} `yaml:"stages"`

}

编辑前归档 yaml

trigger: none

variables:
  - name: app_name
    value: '<name>'
  - name: cluster_name
    value: '<cluster>'
  - name: component_name
    value: '<component>'
  - name: location_env
    value: 'dev'


stages:
  - template: 'tem'
    parameters:
      app_name: '${{ variables.app_name }}'
      app_repo_branch: 'dev'
      location_env: '${{ variables.location_env }}'
      component_name: '${{ variables.component_name }}'
      cluster_name: '${{ variables.cluster_name }}'

编辑后归档yaml

trigger: none
variables:
    - name: app_name
      value: test
    - name: cluster_name
      value: test
    - name: component_name
      value: <component>
    - name: location_env
      value: dev

stages:
    - template: tem
      parameters:
        app_name: ${{ variables.app_name }}
        app_repo_branch: develop
        location_env: ${{ variables.location_env }}
        component_name: ${{ variables.component_name }}
        cluster_name: ${{ variables.cluster_name }}

如您所见,单引号消失了。有什么建议吗?

go kubernetes yaml cloud marshalling
1个回答
0
投票

尝试将

string
值更改为
yaml.Node
并使用它:

    Variables []struct {
        Name  yaml.Node `yaml:"name"`
        Value yaml.Node `yaml:"value"`
    } `yaml:"variables"`
    for i := range config.Variables {
        switch config.Variables[i].Name.Value {
        case "app_name":
            config.Variables[i].Value.Value = nameValue
        case "cluster_name":
            config.Variables[i].Value.Value = nameCluster

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.