Skip to content

Commit

Permalink
add generate command
Browse files Browse the repository at this point in the history
Add generate command, as a way to generate from template files without
bumping the version.

This renames previous bump command from:

- `go run build/bump.go $TAG`

to:

- `go run build/build.go bump $TAG`
  • Loading branch information
celiala authored and rail committed Mar 29, 2023
1 parent 0698ccd commit b5124bd
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions build/bump.go → build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const (
readmeFileTemplate = "build/templates/README.md"
)

const usage = `Usage:
- go run build/build.go bump <crdbversion>
- go run build/build.go generate
`

type parsedVersion struct {
*semver.Version
}
Expand All @@ -61,18 +66,49 @@ func (v *parsedVersion) UnmarshalYAML(value *yaml.Node) error {
}

func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run build/bump.go crdbversion")
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(1)
}
if err := run(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "cannot run: %s", err)
os.Exit(1)

switch os.Args[1] {
case "bump":
if len(os.Args) < 3 {
fmt.Print(usage)
os.Exit(1)
}
if err := bump(os.Args[2]); err != nil {
fmt.Fprintf(os.Stderr, "cannot run: %s", err)
os.Exit(1)
}
return
case "generate":
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(1)
}
if err := generate(); err != nil {
fmt.Fprintf(os.Stderr, "cannot run: %s", err)
os.Exit(1)
}
return
}

fmt.Print(usage)
os.Exit(1)
}

func run(version string) error {
// regenerate destination files based on templates, which should
// result in a zero diff, if template is up-to-date with destination files.
func generate() error {
chart, err := getVersions(chartsFile)
if err != nil {
return fmt.Errorf("cannot get chart versions: %w", err)
}
return processTemplates(chart.Version.String(), chart.AppVersion.String())
}

func bump(version string) error {
// Trim the "v" prefix if exists. It will be added explicitly in the templates when needed.
crdbVersion, err := semver.NewVersion(strings.TrimPrefix(version, "v"))
if err != nil {
Expand All @@ -87,9 +123,13 @@ func run(version string) error {
if err != nil {
return fmt.Errorf("cannot bump chart version: %w", err)
}
return processTemplates(newChartVersion, crdbVersion.Original())
}

func processTemplates(version string, appVersion string) error {
args := templateArgs{
Version: newChartVersion,
AppVersion: crdbVersion.Original(),
Version: version,
AppVersion: appVersion,
}
if err := processTemplate(
chartsFileTemplate,
Expand Down

0 comments on commit b5124bd

Please sign in to comment.