Skip to content

Commit

Permalink
feat: add --file flag to generate file
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 23, 2020
1 parent 3aa0aee commit 0e4fb09
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ feature:
### Usage

```bash
# Automatically generate a change log from the current to the latest tag
$ changelog
# Generate 1.2.0 changelog
$ changelog v1.2.0
# Generate changelog from v1.2.0~v2.0.0
$ changelog v2.0.0~v1.2.0
# Generate changelog and write to CHANGELOG>md
$ changelog > CHANGELOG.md

$ changelog --help
changelog - a cli to generate changelog from git project

Expand All @@ -55,6 +46,7 @@ OPTIONS:
--version Print version information.
--dir Specify the directory to be generated.
The directory should contain a .git folder. defaults to $PWD.
--file Write output to file. default write to stdout.
--fmt The changelog format. Available options are "md"/"json".
Defaults to "md".
--preset Cli built-in markdown template. Available options are "default".
Expand All @@ -64,16 +56,16 @@ OPTIONS:

EXAMPLES:
# generate changelog from HEAD to <latest version>. equivalent to 'changelog HEAD~tag:0'
$ changelog
$ changelog

# generate changelog of the specified version
$ changelog v1.2.0

# generate changelog within the specified range
$ changelog v1.3.0~v1.2.0
$ changelog v1.3.0~v1.2.0

# generate changelog from HEAD to <Nth tag>
$ changelog ~tag:0
# generate changelog from HEAD to <Nth tag>
$ changelog ~tag:0

# generate changelog from <0th tag> to <2th tag>
$ changelog tag:0~tag:2
Expand All @@ -82,7 +74,7 @@ EXAMPLES:
$ changelog HEAD~v1.3.0

# generate all changelog
$ changelog HEAD~
$ changelog HEAD~

# generate changelog from two commit hashes
$ changelog 770ed02~585445d
Expand Down
33 changes: 27 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path"

parser "github.com/axetroy/changelog/1_parser"
extractor "github.com/axetroy/changelog/2_extractor"
Expand Down Expand Up @@ -44,6 +45,7 @@ OPTIONS:
--version Print version information.
--dir Specify the directory to be generated.
The directory should contain a .git folder. defaults to $PWD.
--file Write output to file. default write to stdout.
--fmt The changelog format. Available options are "md"/"json".
Defaults to "md".
--preset Cli built-in markdown template. Available options are "default".
Expand All @@ -53,16 +55,16 @@ OPTIONS:
EXAMPLES:
# generate changelog from HEAD to <latest version>. equivalent to 'changelog HEAD~tag:0'
$ changelog
$ changelog
# generate changelog of the specified version
$ changelog v1.2.0
# generate changelog within the specified range
$ changelog v1.3.0~v1.2.0
$ changelog v1.3.0~v1.2.0
# generate changelog from HEAD to <Nth tag>
$ changelog ~tag:0
# generate changelog from HEAD to <Nth tag>
$ changelog ~tag:0
# generate changelog from <0th tag> to <2th tag>
$ changelog tag:0~tag:2
Expand All @@ -71,7 +73,7 @@ EXAMPLES:
$ changelog HEAD~v1.3.0
# generate all changelog
$ changelog HEAD~
$ changelog HEAD~
# generate changelog from two commit hashes
$ changelog 770ed02~585445d
Expand All @@ -91,6 +93,7 @@ func run() error {
preset string
templateFile string
format string
outputFile string
)

cwd, err := os.Getwd()
Expand All @@ -102,6 +105,7 @@ func run() error {
flag.StringVar(&format, "fmt", "md", "The changelog format")
flag.StringVar(&preset, "preset", "default", "Cli built-in markdown template")
flag.StringVar(&templateFile, "tpl", "", "Specify the template when generating")
flag.StringVar(&outputFile, "file", "", "Specify output result to file.")
flag.BoolVar(&showHelp, "help", false, "Print help information")
flag.BoolVar(&showVersion, "version", false, "Print version information")

Expand Down Expand Up @@ -149,7 +153,24 @@ func run() error {
return errors.WithStack(err)
}

_, err = io.Copy(os.Stdout, bytes.NewBuffer(output))
var writer io.Writer

if outputFile != "" {
if !path.IsAbs(outputFile) {
outputFile = path.Join(cwd, outputFile)
}
file, err := os.Create(outputFile)

if err != nil {
return errors.WithStack(err)
}

writer = file
} else {
writer = os.Stdout
}

_, err = io.Copy(writer, bytes.NewBuffer(output))

if err != nil {
return errors.WithStack(err)
Expand Down

0 comments on commit 0e4fb09

Please sign in to comment.