Debian Codegraph @

core/change.go

raw
1 package core
2
3 import (
4 "fmt"
5 "io"
6 "os"
7 "strings"
8 "time"
9
10 "gopkg.in/yaml.v3"
11 )
12
13 // Less will compare two Change values with the config settings.
14 // * Components, if enabled and componentFormat is configured, are sorted by index in config
15 // * Kind, if enabled and kindFormat is configured, are sorted by index in config
16 // * Time sorted newest first
17 func ChangeLess(cfg *Config, changes []Change) func(i, j int) bool {
18 return func(i, j int) bool {
19 a := changes[i]
20 b := changes[j]
21
22 // Start by sorting by component index
23 if cfg.ComponentFormat != "" && len(cfg.Components) > 0 && a.Component != b.Component {
24 for _, c := range cfg.Components {
25 if a.Component == c {
26 return true
27 } else if b.Component == c {
28 return false
29 }
30 }
31 }
32
33 // Then sort by kind index
34 if cfg.KindFormat != "" && len(cfg.Kinds) > 0 && a.Kind != b.Kind {
35 for _, k := range cfg.Kinds {
36 if a.Kind == k.Key || a.Kind == k.Label {
37 return true
38 } else if b.Kind == k.Key || b.Kind == k.Label {
39 return false
40 }
41 }
42 }
43
44 // Finish sort by oldest first
45 return b.Time.After(a.Time)
46 }
47 }
48
49 // Change represents an atomic change to a project.
50 type Change struct {
51 // Project of our change, if one was provided.
52 Project string `yaml:",omitempty" default:""`
53 // Component of our change, if one was provided.
54 Component string `yaml:",omitempty" default:""`
55 // Kind key of our change, if one was provided.
56 // Backwards compatible alias for KindKey.
57 Kind string `yaml:",omitempty" default:""`
58 // Body message of our change, if one was provided.
59 Body string `yaml:",omitempty" default:""`
60 // When our change was made.
61 Time time.Time `yaml:"" required:"true"`
62 // Custom values corresponding to our options where each key-value pair is the key of the custom option
63 // and value the one provided in the change.
64 // example: yaml
65 // custom:
66 // - key: Issue
67 // type: int
68 // changeFormat: "{{.Body}} from #{{.Custom.Issue}}"
69 Custom map[string]string `yaml:",omitempty" default:"nil"`
70 // Env vars configured by the system.
71 // This is not written in change fragments but instead loaded by the system and accessible for templates.
72 // For example if you want to use an env var in [change format](#config-changeformat) you can,
73 // but env vars configured when executing `changie new` will not be saved.
74 // See [envPrefix](#config-envprefix) for configuration.
75 Env map[string]string `yaml:"-" default:"nil"`
76 // Filename the change was saved to.
77 Filename string `yaml:"-"`
78 // Kind key of our change, if one was provided.
79 KindKey string `yaml:"kindKey,omitempty" default:""`
80 // Kind label of our change, if one was provided.
81 KindLabel string `yaml:"kindLabel,omitempty" default:""`
82 }
83
84 // WriteTo will write a change to the writer as YAML
85 func (change Change) WriteTo(writer io.Writer) (int64, error) {
86 bs, _ := yaml.Marshal(&change)
87 n, err := writer.Write(bs)
88
89 return int64(n), err
90 }
91
92 func (change *Change) PostProcess(cfg *Config, kind *KindConfig) error {
93 postConfigs := make([]PostProcessConfig, 0)
94
95 if kind == nil || !kind.SkipGlobalPost {
96 postConfigs = append(postConfigs, cfg.Post...)
97 }
98
99 if kind != nil {
100 postConfigs = append(postConfigs, kind.Post...)
101 }
102
103 if len(postConfigs) == 0 {
104 return nil
105 }
106
107 templateCache := NewTemplateCache()
108
109 for _, post := range postConfigs {
110 writer := strings.Builder{}
111
112 err := templateCache.Execute(post.Value, &writer, change)
113 if err != nil {
114 return err
115 }
116
117 change.Custom[post.Key] = writer.String()
118 }
119
120 return nil
121 }
122
123 // LoadChange will load a change from file path
124 func LoadChange(path string) (Change, error) {
125 var c Change
126
127 bs, err := os.ReadFile(path)
128 if err != nil {
129 return c, fmt.Errorf("reading change file '%s': %w", path, err)
130 }
131
132 err = yaml.Unmarshal(bs, &c)
133 if err != nil {
134 return c, fmt.Errorf("unmarshaling change file '%s': %w", path, err)
135 }
136
137 c.Filename = path
138 c.KindKey = c.Kind
139
140 return c, nil
141 }