|
| 1 | +package assets |
| 2 | + |
| 3 | +import ( |
| 4 | + "embed" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + "github.com/rocket-pool/smartnode/v2/rocketpool-cli/utils/terminal" |
| 10 | +) |
| 11 | + |
| 12 | +var patchnotesTemplateFields = struct { |
| 13 | + ColorReset string |
| 14 | + ColorBold string |
| 15 | + ColorRed string |
| 16 | + ColorGreen string |
| 17 | + ColorYellow string |
| 18 | + ColorBlue string |
| 19 | + RocketPoolVersion string |
| 20 | +}{ |
| 21 | + ColorReset: terminal.ColorReset, |
| 22 | + ColorBold: terminal.ColorBold, |
| 23 | + ColorRed: terminal.ColorRed, |
| 24 | + ColorGreen: terminal.ColorGreen, |
| 25 | + ColorYellow: terminal.ColorYellow, |
| 26 | + ColorBlue: terminal.ColorBlue, |
| 27 | + RocketPoolVersion: "", |
| 28 | +} |
| 29 | + |
| 30 | +//go:embed patchnotes/*.tmpl |
| 31 | +var patchnotesFS embed.FS |
| 32 | + |
| 33 | +func loadTemplate(version string) (*template.Template, error) { |
| 34 | + return template.ParseFS(patchnotesFS, fmt.Sprintf("patchnotes/%s.tmpl", version)) |
| 35 | +} |
| 36 | + |
| 37 | +// PrintPatchNotes looks for a template in ./patchnotes/ with a name matching the current |
| 38 | +// version of smartnode and a file extension of .tmpl |
| 39 | +// |
| 40 | +// If it finds one, it populates it using the PatchNotes struct defined above, and prints it after printing the logo. |
| 41 | +// It returns an error when no template exists, or the template could not be populated. |
| 42 | +func GetPatchNotes() (string, error) { |
| 43 | + version := RocketPoolVersion() |
| 44 | + tmpl, err := loadTemplate(version) |
| 45 | + if err != nil { |
| 46 | + return "", fmt.Errorf("unable to read patch notes: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + // Set RocketPoolVersion before executing |
| 50 | + patchnotesTemplateFields.RocketPoolVersion = version |
| 51 | + |
| 52 | + notes := new(strings.Builder) |
| 53 | + notes.WriteString("\n") |
| 54 | + notes.WriteString(Logo()) |
| 55 | + err = tmpl.Execute(notes, patchnotesTemplateFields) |
| 56 | + if err != nil { |
| 57 | + return "", fmt.Errorf("unable to populate patch notes template: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + return notes.String(), nil |
| 61 | +} |
0 commit comments