From 4f6c321e1fff5be061b58f5cc8acf7d39df483e0 Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Thu, 18 May 2023 10:22:50 -0400 Subject: [PATCH] Added support for specifying a non-default changelog file Also added support for resolving files relative to `.sfreleaser` location. --- CHANGELOG.md | 6 ++++++ cmd/sfreleaser/bashism.go | 7 +++---- cmd/sfreleaser/main.go | 18 ++++++++++++++++++ cmd/sfreleaser/models.go | 24 +++++++++++++++++++----- cmd/sfreleaser/release.go | 5 ++++- cmd/sfreleaser/release_notes.go | 3 +-- 6 files changed, 51 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f201153..fadfffb 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v0.4.2 + +* Added support for resolving files relative to `.sfreleaser` location. + +* Added support for specifying a non-default changelog file. + ## v0.4.1 * Added checks that `docker` CLI exists and also that `docker info` works properly. diff --git a/cmd/sfreleaser/bashism.go b/cmd/sfreleaser/bashism.go index 44119c3..1f1d4d3 100644 --- a/cmd/sfreleaser/bashism.go +++ b/cmd/sfreleaser/bashism.go @@ -80,12 +80,11 @@ func (i *commandInfo) String() string { return strings.Join(append(append(i.env, i.command), i.args...), " ") } -func run(inputs ...string) (output string, err error) { - var info *commandInfo - output, info, err = maybeRun(inputs...) +func run(inputs ...string) (output string) { + output, info, err := maybeRun(inputs...) cli.NoError(err, "Command %q failed", info) - return output, nil + return output } // runSilent is like [run] but do not print the command output but do print diff --git a/cmd/sfreleaser/main.go b/cmd/sfreleaser/main.go index a863b0b..4410649 100644 --- a/cmd/sfreleaser/main.go +++ b/cmd/sfreleaser/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "github.com/spf13/pflag" "github.com/streamingfast/cli" @@ -83,3 +84,20 @@ func verifyCommandRunSuccesfully(command string, onErrorText string) { cli.Exit(1) } } + +func findSfreleaserDir(workingDirectory string) string { + current := workingDirectory + volumeName := filepath.VolumeName(current) + + for { + if current == volumeName { + return "" + } + + if _, err := os.Stat(filepath.Join(current, ".sfreleaser")); err == nil { + return current + } + + current = filepath.Dir(current) + } +} diff --git a/cmd/sfreleaser/models.go b/cmd/sfreleaser/models.go index b1d3610..2b45829 100644 --- a/cmd/sfreleaser/models.go +++ b/cmd/sfreleaser/models.go @@ -11,20 +11,24 @@ import ( ) type GlobalModel struct { - Project string - Binary string - Language Language - Variant Variant - Root string + Project string + Binary string + Language Language + Variant Variant + Root string + ConfigRoot string WorkingDirectory string } func (g *GlobalModel) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("project", g.Project) + encoder.AddString("binary", g.Binary) encoder.AddString("language", g.Language.String()) encoder.AddString("variant", g.Variant.String()) + encoder.AddString("config_root", g.ConfigRoot) encoder.AddString("working_directory", g.WorkingDirectory) + return nil } @@ -50,9 +54,19 @@ func mustGetGlobal(cmd *cobra.Command) *GlobalModel { global.Binary = global.Project } + global.ConfigRoot = findSfreleaserDir(global.WorkingDirectory) + return global } +func (g *GlobalModel) ResolveFile(in string) string { + if filepath.IsAbs(in) { + return in + } + + return filepath.Join(g.ConfigRoot, in) +} + func (g *GlobalModel) ensureValidForRelease() { var errors []string if g.Language == LanguageUnset { diff --git a/cmd/sfreleaser/release.go b/cmd/sfreleaser/release.go index d9f1fbd..df46314 100644 --- a/cmd/sfreleaser/release.go +++ b/cmd/sfreleaser/release.go @@ -52,6 +52,7 @@ var ReleaseCmd = Command(release, `), Flags(func(flags *pflag.FlagSet) { flags.Bool("allow-dirty", false, "Perform release step even if Git is not clean, tries to configured used tool(s) to also allow dirty Git state") + flags.String("changelog-path", "CHANGELOG.md", "Path where to find the changelog file used to extract the release notes") flags.StringArray("pre-build-hooks", nil, "Set of pre build hooks to run before run the actual building steps") flags.String("upload-substreams-spkg", "", "If provided, add this Substreams package file to the release, if manifest is a 'substreams.yaml' file, the package is first built") flags.Bool("publish-now", false, "By default, publish the release to GitHub in draft mode, if the flag is used, the release is published as latest") @@ -94,6 +95,7 @@ func release(cmd *cobra.Command, args []string) error { } allowDirty := sflags.MustGetBool(cmd, "allow-dirty") + changelogPath := global.ResolveFile(sflags.MustGetString(cmd, "changelog-path")) goreleaserDockerImage := sflags.MustGetString(cmd, "goreleaser-docker-image") publishNow := sflags.MustGetBool(cmd, "publish-now") preBuildHooks := sflags.MustGetStringArray(cmd, "pre-build-hooks") @@ -104,6 +106,7 @@ func release(cmd *cobra.Command, args []string) error { zlog.Debug("starting 'sfreleaser release'", zap.Inline(global), zap.Bool("allow_dirty", allowDirty), + zap.String("changelog_path", changelogPath), zap.String("goreleaser_docker_image", goreleaserDockerImage), zap.Bool("publish_now", publishNow), zap.Strings("pre_build_hooks", preBuildHooks), @@ -135,7 +138,7 @@ func release(cmd *cobra.Command, args []string) error { cli.NoError(os.MkdirAll("build", os.ModePerm), "Unable to create build directory") configureGitHubTokenEnvFile("build/.env.release") - cli.WriteFile("build/.release_notes.md", readReleaseNotes()) + cli.WriteFile("build/.release_notes.md", readReleaseNotes(changelogPath)) // By doing this after creating the build directory and release notes, we ensure // that those are ignored, the user will need to ignore them to process (or --allow-dirty). diff --git a/cmd/sfreleaser/release_notes.go b/cmd/sfreleaser/release_notes.go index a81b684..3ff56c2 100644 --- a/cmd/sfreleaser/release_notes.go +++ b/cmd/sfreleaser/release_notes.go @@ -12,8 +12,7 @@ import ( var headerRegex = regexp.MustCompile(`^##([^#])`) -func readReleaseNotes() string { - changelogFile := "./CHANGELOG.md" +func readReleaseNotes(changelogFile string) string { if !cli.FileExists(changelogFile) { return "" }