refactor(cli): rename output path to real output

main
hau 2021-01-05 15:55:08 +07:00
parent 7dd3918be2
commit d06a27d88d
1 changed files with 11 additions and 11 deletions

22
main.go
View File

@ -188,7 +188,7 @@ func (a *action) getConventionalCommits(commits []git.Commit) []convention.Commi
} }
func (a *action) generateChangelog(commits []convention.Commit) error { func (a *action) generateChangelog(commits []convention.Commit) error {
outputPath, _, filetype := a.getOutputPath() realOutput, _, filetype := a.getRealOutput()
version, err := a.getVersion() version, err := a.getVersion()
if err != nil { if err != nil {
@ -197,22 +197,22 @@ func (a *action) generateChangelog(commits []convention.Commit) error {
switch filetype { switch filetype {
case markdownFiletype: case markdownFiletype:
return a.generateMarkdownChangelog(outputPath, version, commits) return a.generateMarkdownChangelog(realOutput, version, commits)
default: default:
return fmt.Errorf("unknown filetype %s", filetype) return fmt.Errorf("unknown filetype %s", filetype)
} }
} }
func (a *action) getOutputPath() (string, string, string) { func (a *action) getRealOutput() (string, string, string) {
outputDir := a.flags[outputFlag] output := a.flags[outputFlag]
filename := a.flags[filenameFlag] filename := a.flags[filenameFlag]
filetype := a.flags[filetypeFlag] filetype := a.flags[filetypeFlag]
nameWithExt := filename + "." + filetype nameWithExt := filename + "." + filetype
path := filepath.Join(outputDir, nameWithExt) realOutput := filepath.Join(output, nameWithExt)
a.logDebug("output path %s", path) a.logDebug("output path %s", realOutput)
return path, filename, filetype return realOutput, filename, filetype
} }
func (a *action) getVersion() (string, error) { func (a *action) getVersion() (string, error) {
@ -230,10 +230,10 @@ func (a *action) getVersion() (string, error) {
return version, nil return version, nil
} }
func (a *action) generateMarkdownChangelog(outputPath, version string, commits []convention.Commit) error { func (a *action) generateMarkdownChangelog(output, version string, commits []convention.Commit) error {
// If CHANGELOG file already exist // If CHANGELOG file already exist
var oldData string var oldData string
bytes, err := ioutil.ReadFile(outputPath) bytes, err := ioutil.ReadFile(output)
if err == nil { if err == nil {
oldData = string(bytes) oldData = string(bytes)
} }
@ -241,8 +241,8 @@ func (a *action) generateMarkdownChangelog(outputPath, version string, commits [
markdownGenerator := changelog.NewMarkdownGenerator(oldData, version, time.Now()) markdownGenerator := changelog.NewMarkdownGenerator(oldData, version, time.Now())
newData := markdownGenerator.Generate(commits) newData := markdownGenerator.Generate(commits)
if err := ioutil.WriteFile(outputPath, []byte(newData), 0o644); err != nil { if err := ioutil.WriteFile(output, []byte(newData), 0o644); err != nil {
return fmt.Errorf("failed to write file %s: %w", outputPath, err) return fmt.Errorf("failed to write file %s: %w", output, err)
} }
return nil return nil