-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve internal release command (#2315)
- Loading branch information
Showing
15 changed files
with
311 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"fmt" | ||
"go/format" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
) | ||
|
||
const ( | ||
dnsTemplate = "templates/dns.go.tmpl" | ||
dnsTargetFile = "./providers/dns/internal/useragent/useragent.go" | ||
) | ||
|
||
const ( | ||
senderTemplate = "templates/sender.go.tmpl" | ||
senderTargetFile = "./acme/api/internal/sender/useragent.go" | ||
) | ||
|
||
const ( | ||
versionTemplate = "templates/version.go.tmpl" | ||
versionTargetFile = "./cmd/lego/zz_gen_version.go" | ||
) | ||
|
||
//go:embed templates | ||
var templateFS embed.FS | ||
|
||
type Generator struct { | ||
templatePath string | ||
targetFile string | ||
} | ||
|
||
func NewGenerator(templatePath string, targetFile string) *Generator { | ||
return &Generator{templatePath: templatePath, targetFile: targetFile} | ||
} | ||
|
||
func (g *Generator) Generate(version, comment string) error { | ||
tmpl, err := template.New(filepath.Base(g.templatePath)).ParseFS(templateFS, g.templatePath) | ||
if err != nil { | ||
return fmt.Errorf("parsing template (%s): %w", g.templatePath, err) | ||
} | ||
|
||
b := &bytes.Buffer{} | ||
|
||
err = tmpl.Execute(b, map[string]string{ | ||
"version": version, | ||
"comment": comment, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("execute template (%s): %w", g.templatePath, err) | ||
} | ||
|
||
source, err := format.Source(b.Bytes()) | ||
if err != nil { | ||
return fmt.Errorf("format generated content (%s): %w", g.targetFile, err) | ||
} | ||
|
||
err = os.WriteFile(g.targetFile, source, 0o644) | ||
if err != nil { | ||
return fmt.Errorf("write file (%s): %w", g.targetFile, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func generate(targetVersion, comment string) error { | ||
generators := []*Generator{ | ||
NewGenerator(dnsTemplate, dnsTargetFile), | ||
NewGenerator(senderTemplate, senderTargetFile), | ||
NewGenerator(versionTemplate, versionTargetFile), | ||
} | ||
|
||
for _, generator := range generators { | ||
err := generator.Generate(targetVersion, comment) | ||
if err != nil { | ||
return fmt.Errorf("generate file(s): %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"log" | ||
"os" | ||
"strconv" | ||
|
||
hcversion "github.com/hashicorp/go-version" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
const flgMode = "mode" | ||
|
||
const ( | ||
modePatch = "patch" | ||
modeMinor = "minor" | ||
modeMajor = "major" | ||
) | ||
|
||
const versionSourceFile = "./cmd/lego/zz_gen_version.go" | ||
|
||
const ( | ||
commentRelease = "release" | ||
commentDetach = "detach" | ||
) | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Name = "lego-releaser" | ||
app.Usage = "Lego releaser" | ||
app.HelpName = "releaser" | ||
app.Commands = []*cli.Command{ | ||
{ | ||
Name: "release", | ||
Usage: "Update file for a release", | ||
Action: release, | ||
Before: func(ctx *cli.Context) error { | ||
mode := ctx.String("mode") | ||
switch mode { | ||
case modePatch, modeMinor, modeMajor: | ||
return nil | ||
default: | ||
return fmt.Errorf("invalid mode: %s", mode) | ||
} | ||
}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: flgMode, | ||
Aliases: []string{"m"}, | ||
Value: modePatch, | ||
Usage: fmt.Sprintf("The release mode: %s|%s|%s", modePatch, modeMinor, modeMajor), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "detach", | ||
Usage: "Update file post release", | ||
Action: detach, | ||
}, | ||
} | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func release(ctx *cli.Context) error { | ||
mode := ctx.String(flgMode) | ||
|
||
currentVersion, err := readCurrentVersion(versionSourceFile) | ||
if err != nil { | ||
return fmt.Errorf("read current version: %w", err) | ||
} | ||
|
||
nextVersion, err := bumpVersion(mode, currentVersion) | ||
if err != nil { | ||
return fmt.Errorf("bump version: %w", err) | ||
} | ||
|
||
err = generate(nextVersion, commentRelease) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func detach(_ *cli.Context) error { | ||
currentVersion, err := readCurrentVersion(versionSourceFile) | ||
if err != nil { | ||
return fmt.Errorf("read current version: %w", err) | ||
} | ||
|
||
v := currentVersion.Core().String() | ||
|
||
err = generate(v, commentDetach) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readCurrentVersion(filename string) (*hcversion.Version, error) { | ||
fset := token.NewFileSet() | ||
file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
v := visitor{data: make(map[string]string)} | ||
ast.Walk(v, file) | ||
|
||
current, err := hcversion.NewSemver(v.data["defaultVersion"]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return current, nil | ||
} | ||
|
||
type visitor struct { | ||
data map[string]string | ||
} | ||
|
||
func (v visitor) Visit(n ast.Node) ast.Visitor { | ||
if n == nil { | ||
return nil | ||
} | ||
|
||
switch d := n.(type) { | ||
case *ast.GenDecl: | ||
if d.Tok == token.CONST { | ||
for _, spec := range d.Specs { | ||
valueSpec, ok := spec.(*ast.ValueSpec) | ||
if !ok { | ||
continue | ||
} | ||
if len(valueSpec.Names) != 1 || len(valueSpec.Values) != 1 { | ||
continue | ||
} | ||
|
||
va, ok := valueSpec.Values[0].(*ast.BasicLit) | ||
if !ok { | ||
continue | ||
} | ||
if va.Kind != token.STRING { | ||
continue | ||
} | ||
|
||
s, err := strconv.Unquote(va.Value) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
v.data[valueSpec.Names[0].String()] = s | ||
} | ||
} | ||
default: | ||
// noop | ||
} | ||
return v | ||
} | ||
|
||
func bumpVersion(mode string, v *hcversion.Version) (string, error) { | ||
segments := v.Segments() | ||
|
||
switch mode { | ||
case modePatch: | ||
return fmt.Sprintf("%d.%d.%d", segments[0], segments[1], segments[2]+1), nil | ||
case modeMinor: | ||
return fmt.Sprintf("%d.%d.0", segments[0], segments[1]+1), nil | ||
case modeMajor: | ||
return fmt.Sprintf("%d.0.0", segments[0]+1), nil | ||
default: | ||
return "", fmt.Errorf("invalid mode: %s", mode) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/useragent/templates/dns.go.tmpl → internal/releaser/templates/dns.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
internal/useragent/templates/sender.go.tmpl → internal/releaser/templates/sender.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Code generated by 'internal/releaser'; DO NOT EDIT. | ||
|
||
package main | ||
|
||
const defaultVersion = "v{{ .version }}+dev{{ if .comment }}-{{ .comment }}{{end}}" | ||
|
||
var version = "" | ||
|
||
func getVersion() string { | ||
if version == "" { | ||
return defaultVersion | ||
} | ||
|
||
return version | ||
} |
Oops, something went wrong.