feat(changelog): prepend a changelog to a file as default, use --overwrite to overwrite the whole file

BREAKING CHANGE: changelog command will now prepend changelogs to the changelog-file
This commit is contained in:
fwiedmann
2021-03-11 22:12:44 +01:00
committed by Felix Wiedmann
parent 155a16ddd5
commit a054cf9a60
2 changed files with 25 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ import (
func init() { func init() {
changelogCmd.Flags().Bool("checks", false, "Check for missing values and envs") changelogCmd.Flags().Bool("checks", false, "Check for missing values and envs")
changelogCmd.Flags().Bool("overwrite", false, "Overwrite the content of the changelog. Default is to prepend the new changelog to the existing file.")
changelogCmd.Flags().StringP("out", "o", "CHANGELOG.md", "Name of the file") changelogCmd.Flags().StringP("out", "o", "CHANGELOG.md", "Name of the file")
rootCmd.AddCommand(changelogCmd) rootCmd.AddCommand(changelogCmd)
} }
@@ -31,6 +32,11 @@ var changelogCmd = &cobra.Command{
return err return err
} }
overwrite, err := cmd.Flags().GetBool("overwrite")
if err != nil {
return err
}
file, err := cmd.Flags().GetString("out") file, err := cmd.Flags().GetString("out")
if err != nil { if err != nil {
return err return err
@@ -61,7 +67,6 @@ var changelogCmd = &cobra.Command{
if err != nil { if err != nil {
return err return err
} }
return s.WriteChangeLog(generatedChangelog.Content, file, overwrite)
return s.WriteChangeLog(generatedChangelog.Content, file)
}, },
} }

View File

@@ -2,10 +2,11 @@ package semanticrelease
import ( import (
"fmt" "fmt"
"github.com/Nightapes/go-semantic-release/internal/integrations" "os"
"io/ioutil"
"time" "time"
"github.com/Nightapes/go-semantic-release/internal/integrations"
"github.com/Masterminds/semver" "github.com/Masterminds/semver"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -194,8 +195,21 @@ func (s *SemanticRelease) GetChangelog(releaseVersion *shared.ReleaseVersion) (*
} }
// WriteChangeLog writes changelog content to the given file // WriteChangeLog writes changelog content to the given file
func (s *SemanticRelease) WriteChangeLog(changelogContent, file string) error { func (s *SemanticRelease) WriteChangeLog(changelogContent, file string, overwrite bool) error {
return ioutil.WriteFile(file, []byte(changelogContent), 0644) newContent := make([]byte, 0)
newContent = append(newContent, []byte(changelogContent)...)
// only prepend the new changelog content if the the given file already exists
_, err := os.Stat(file)
if !overwrite && err == nil {
content, err := os.ReadFile(file)
if err != nil {
return err
}
newContent = append(newContent, []byte("\n---\n\n")...)
newContent = append(newContent, content...)
}
return os.WriteFile(file, newContent, 0644)
} }
// Release publish release to provider // Release publish release to provider