|
| 1 | +package migrations |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | +) |
| 9 | + |
| 10 | +// cimgmtYaml wraps the contents of a .ci-mgmt.yaml file along with its source path. |
| 11 | +type cimgmtYaml struct { |
| 12 | + path string |
| 13 | + // we operate on the yaml.Node so that we can preserve comments and ordering |
| 14 | + node *yaml.Node |
| 15 | +} |
| 16 | + |
| 17 | +// newCimgmtYaml loads the YAML document from disk and prepares it for mutation. |
| 18 | +func newCimgmtYaml(path string) (*cimgmtYaml, error) { |
| 19 | + ciMgmtPath := path |
| 20 | + ciMgmtFile, err := os.ReadFile(ciMgmtPath) |
| 21 | + if err != nil { |
| 22 | + return nil, fmt.Errorf("error reading .ci-mgmt.yaml: %w", err) |
| 23 | + } |
| 24 | + |
| 25 | + var ciMgmt yaml.Node |
| 26 | + if err := yaml.Unmarshal(ciMgmtFile, &ciMgmt); err != nil { |
| 27 | + return nil, fmt.Errorf("error unmarshaling .ci-mgmt.yaml: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + return &cimgmtYaml{ |
| 31 | + node: &ciMgmt, |
| 32 | + path: path, |
| 33 | + }, nil |
| 34 | +} |
| 35 | + |
| 36 | +// writeFile persists the in-memory YAML representation back to its original path. |
| 37 | +func (c *cimgmtYaml) writeFile() error { |
| 38 | + newCiMgmt, err := yaml.Marshal(c.node) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("error marshaling .ci-mgmt.yaml: %w", err) |
| 41 | + } |
| 42 | + if err := os.WriteFile(c.path, newCiMgmt, 0644); err != nil { |
| 43 | + return fmt.Errorf("error writing .ci-mgmt.yaml: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// deleteKey deletes a top level field from the ci-mgmt.yaml file |
| 50 | +func (c *cimgmtYaml) deleteKey(key string) { |
| 51 | + node := c.node.Content[0] |
| 52 | + if node == nil || node.Kind != yaml.MappingNode { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + out := node.Content[:0] |
| 57 | + for i := 0; i < len(node.Content); i += 2 { |
| 58 | + if i+1 >= len(node.Content) { |
| 59 | + continue |
| 60 | + } |
| 61 | + k := node.Content[i] |
| 62 | + v := node.Content[i+1] |
| 63 | + if k.Value == key { |
| 64 | + continue // skip this key/value pair (delete) |
| 65 | + } |
| 66 | + out = append(out, k, v) |
| 67 | + } |
| 68 | + node.Content = out |
| 69 | +} |
| 70 | + |
| 71 | +// getFieldNode gets a top level field from the ci-mgmt.yaml file |
| 72 | +func (c *cimgmtYaml) getFieldNode(key string) *yaml.Node { |
| 73 | + node := c.node.Content[0] |
| 74 | + if node.Kind != yaml.MappingNode { |
| 75 | + return nil |
| 76 | + } |
| 77 | + for i := 0; i < len(node.Content); i += 2 { |
| 78 | + k := node.Content[i] |
| 79 | + v := node.Content[i+1] |
| 80 | + if k.Value == key { |
| 81 | + return v |
| 82 | + } |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// nodeToMap converts a yaml.Node with object data to a map[string]string |
| 88 | +func nodeToMap(m *yaml.Node) map[string]string { |
| 89 | + if m == nil { |
| 90 | + return nil |
| 91 | + } |
| 92 | + if m.Kind != yaml.MappingNode { |
| 93 | + return nil |
| 94 | + } |
| 95 | + out := make(map[string]string) |
| 96 | + for i := 0; i < len(m.Content); i += 2 { |
| 97 | + k := m.Content[i] |
| 98 | + v := m.Content[i+1] |
| 99 | + out[k.Value] = v.Value |
| 100 | + } |
| 101 | + return out |
| 102 | +} |
0 commit comments