From adc502b79077e8b88b105462514256a422027d95 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 30 Oct 2017 13:15:56 -0400 Subject: [PATCH] Add template filepath for release tool This allows a project to have a TEMPLATE file in the root of the repo to be used with the release tool. If they don't have this file and did not specify a custom file then it will use the compiled in template in the release tool. Signed-off-by: Michael Crosby --- cmd/containerd-release/main.go | 12 +++++++++++- cmd/containerd-release/template.go | 5 ++++- cmd/containerd-release/util.go | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cmd/containerd-release/main.go b/cmd/containerd-release/main.go index 01dc19895..c9e9c5764 100644 --- a/cmd/containerd-release/main.go +++ b/cmd/containerd-release/main.go @@ -61,6 +61,11 @@ This tool should be ran from the root of the project repository for a new releas Name: "dry,n", Usage: "run the release tooling as a dry run to print the release notes to stdout", }, + cli.StringFlag{ + Name: "template,t", + Usage: "template filepath to use in place of the default", + Value: defaultTemplateFile, + }, } app.Action = func(context *cli.Context) error { var ( @@ -100,8 +105,13 @@ This tool should be ran from the root of the project repository for a new releas r.Changes = changes r.Version = tag + tmpl, err := getTemplate(context) + if err != nil { + return err + } + if context.Bool("dry") { - t, err := template.New("release-notes").Parse(releaseNotes) + t, err := template.New("release-notes").Parse(tmpl) if err != nil { return err } diff --git a/cmd/containerd-release/template.go b/cmd/containerd-release/template.go index 9e347e763..7db710510 100644 --- a/cmd/containerd-release/template.go +++ b/cmd/containerd-release/template.go @@ -1,6 +1,8 @@ package main -const releaseNotes = `Welcome to the release of {{.ProjectName}} {{.Version}}! +const ( + defaultTemplateFile = "TEMPLATE" + releaseNotes = `Welcome to the release of {{.ProjectName}} {{.Version}}! {{if .PreRelease}} *This is a pre-release of {{.ProjectName}}* {{- end}} @@ -33,3 +35,4 @@ Previous release can be found at [{{.Previous}}](https://github.com/{{.GithubRep * {{$dep.Previous}} -> {{$dep.Commit}} **{{$dep.Name}}** {{- end}} ` +) diff --git a/cmd/containerd-release/util.go b/cmd/containerd-release/util.go index e041822bb..1e5be4b48 100644 --- a/cmd/containerd-release/util.go +++ b/cmd/containerd-release/util.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "os" "os/exec" "path/filepath" @@ -13,6 +14,7 @@ import ( "strings" "github.com/BurntSushi/toml" + "github.com/urfave/cli" ) func loadRelease(path string) (*release, error) { @@ -163,3 +165,23 @@ func getContributors(previous, commit string) ([]string, error) { sort.Strings(out) return out, nil } + +// getTemplate will use a builtin template if the template is not specified on the cli +func getTemplate(context *cli.Context) (string, error) { + path := context.GlobalString("template") + f, err := os.Open(path) + if err != nil { + // if the template file does not exist and the path is for the default template then + // return the compiled in template + if os.IsNotExist(err) && path == defaultTemplateFile { + return releaseNotes, nil + } + return "", err + } + defer f.Close() + data, err := ioutil.ReadAll(f) + if err != nil { + return "", err + } + return string(data), nil +}