From 0e4fb09789732fec5b09b247e208d61794c3da0d Mon Sep 17 00:00:00 2001 From: axetroy Date: Mon, 23 Nov 2020 10:48:16 +0800 Subject: [PATCH] feat: add --file flag to generate file --- README.md | 20 ++++++-------------- main.go | 33 +++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 992124e3..b61be1a6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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". @@ -64,16 +56,16 @@ OPTIONS: EXAMPLES: # generate changelog from HEAD to . 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 - $ changelog ~tag:0 + # generate changelog from HEAD to + $ changelog ~tag:0 # generate changelog from <0th tag> to <2th tag> $ changelog tag:0~tag:2 @@ -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 diff --git a/main.go b/main.go index ccc65b75..de1d86e9 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "path" parser "github.com/axetroy/changelog/1_parser" extractor "github.com/axetroy/changelog/2_extractor" @@ -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". @@ -53,16 +55,16 @@ OPTIONS: EXAMPLES: # generate changelog from HEAD to . 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 - $ changelog ~tag:0 + # generate changelog from HEAD to + $ changelog ~tag:0 # generate changelog from <0th tag> to <2th tag> $ changelog tag:0~tag:2 @@ -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 @@ -91,6 +93,7 @@ func run() error { preset string templateFile string format string + outputFile string ) cwd, err := os.Getwd() @@ -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") @@ -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)