From 189186a89693c724ac794c17f2c35781b2fdc017 Mon Sep 17 00:00:00 2001 From: axetroy Date: Fri, 27 Nov 2020 02:11:24 +0800 Subject: [PATCH] feat(cli): rename '--dir' to '--project' and '--file' to '--output' #4 BREAKING CHANGE: new flags ```diff - --dir=/path/to/dir + --project=/path/to/project ``` ```diff - --file=CHANGELOG.md + --output=CHANGELOG.md ``` --- README.md | 34 +++++++++++++++++++--------- cmd/whatchanged/main.go | 47 +++++++++++++++++++++++++-------------- option/options.go | 1 + whatchanged.go | 49 ++++++++++++++++++++++++++++++++++------- 4 files changed, 96 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b372a6d2..266bdfcc 100644 --- a/README.md +++ b/README.md @@ -45,15 +45,26 @@ ARGUMENTS: OPTIONS: --help Print help information. --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". - Only available when --fmt=md and --tpl is nil. Defaults to - "default". - --tpl Specify the template file for generating. Only available when --fmt=md. + --project Specify the project to be generated. It can be a relative path. + or an absolute path or even a remote Git URL. eg. + --project=/path/to/project/which/contains/.git/folder + --project=https://github.com/axetroy/whatchanged.git + Defaults to "$PWD". + + --output Write output to file. default write to stdout. + --fmt Specify the changelog format. Available options: + --fmt=md + --fmt=json + Defaults to "--fmt=md". + + --preset Cli built-in markdown template. Available options: + --preset=default + --preset=full + Only available when --fmt=md and --tpl is nil. + Defaults to "default". + + --tpl Specify the template file for generating. Only available when + --fmt=md. EXAMPLES: # generate changelog from HEAD to . equivalent to 'whatchanged HEAD~tag:0' @@ -81,7 +92,10 @@ EXAMPLES: $ whatchanged 770ed02~585445d # Generate changelog for the specified project - $ whatchanged --dir=/path/to/project v1.0.0 + $ whatchanged --project=/path/to/project v1.0.0 + + # Generate changelog for the specified project + $ whatchanged --project=https://github.com/axetroy/whatchanged.git v0.1.0 SOURCE CODE: https://github.com/axetroy/whatchanged diff --git a/cmd/whatchanged/main.go b/cmd/whatchanged/main.go index 9b8c29e5..79d9b8d4 100644 --- a/cmd/whatchanged/main.go +++ b/cmd/whatchanged/main.go @@ -37,16 +37,26 @@ ARGUMENTS: OPTIONS: --help Print help information. --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" - /"full". - Only available when --fmt=md and --tpl is nil. Defaults to - "default". - --tpl Specify the template file for generating. Only available when --fmt=md. + --project Specify the project to be generated. It can be a relative path. + or an absolute path or even a remote Git URL. eg. + --project=/path/to/project/which/contains/.git/folder + --project=https://github.com/axetroy/whatchanged.git + Defaults to "$PWD". + + --output Write output to file. default write to stdout. + --fmt Specify the changelog format. Available options: + --fmt=md + --fmt=json + Defaults to "--fmt=md". + + --preset Cli built-in markdown template. Available options: + --preset=default + --preset=full + Only available when --fmt=md and --tpl is nil. + Defaults to "default". + + --tpl Specify the template file for generating. Only available when + --fmt=md. EXAMPLES: # generate changelog from HEAD to . equivalent to 'whatchanged HEAD~tag:0' @@ -74,7 +84,10 @@ EXAMPLES: $ whatchanged 770ed02~585445d # Generate changelog for the specified project - $ whatchanged --dir=/path/to/project v1.0.0 + $ whatchanged --project=/path/to/project v1.0.0 + + # Generate changelog for the specified project + $ whatchanged --project=https://github.com/axetroy/whatchanged.git v0.1.0 SOURCE CODE: https://github.com/axetroy/whatchanged`) @@ -84,7 +97,7 @@ func run() error { var ( showHelp bool showVersion bool - projectDir string + project string preset string templateFile string format string @@ -97,11 +110,11 @@ func run() error { return errors.WithStack(err) } - flag.StringVar(&projectDir, "dir", cwd, "Project dir") - flag.StringVar(&format, "fmt", "md", "The changelog format") - flag.StringVar(&preset, "preset", "default", "Cli built-in markdown template") + flag.StringVar(&project, "project", cwd, "Project filepath or remote URL") + flag.StringVar(&outputFile, "output", "", "Specify the file to be written") + flag.StringVar(&format, "fmt", string(option.FormatMarkdown), "The changelog format") + flag.StringVar(&preset, "preset", string(option.PresetDefault), "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") @@ -133,7 +146,7 @@ func run() error { output = f } - if err := whatchanged.Generate(projectDir, output, &option.Options{ + if err := whatchanged.Generate(project, output, &option.Options{ Version: version, Preset: option.Preset(preset), Format: option.Format(format), diff --git a/option/options.go b/option/options.go index feacd62a..27d262fd 100644 --- a/option/options.go +++ b/option/options.go @@ -17,4 +17,5 @@ type Options struct { Format Format Preset Preset TemplateFile string // Priority is higher than preset + Silent bool } diff --git a/whatchanged.go b/whatchanged.go index 748fb33c..a2416513 100644 --- a/whatchanged.go +++ b/whatchanged.go @@ -1,9 +1,11 @@ package whatchanged import ( + "context" "io" "os" "path" + "regexp" parser "github.com/axetroy/whatchanged/1_parser" extractor "github.com/axetroy/whatchanged/2_extractor" @@ -13,10 +15,23 @@ import ( writer "github.com/axetroy/whatchanged/6_writer" "github.com/axetroy/whatchanged/internal/client" "github.com/axetroy/whatchanged/option" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/storage/memory" "github.com/pkg/errors" ) +var ( + gitHTTPURLReg = regexp.MustCompile(`^http?s:\/\/.+$`) + gitSSHURLReg = regexp.MustCompile(`^git@.+$`) +) + +// generate changelog +// project: it could be a file path or a git URL func Generate(project string, w io.Writer, options *option.Options) error { + var ( + err error + ) + if options == nil { options = &option.Options{ Format: option.FormatMarkdown, @@ -43,31 +58,49 @@ func Generate(project string, w io.Writer, options *option.Options) error { } } - client, err := client.NewGitClient(project) + var g *client.GitClient - if err != nil { - return errors.WithStack(err) + if gitHTTPURLReg.MatchString(project) || gitSSHURLReg.MatchString(project) { + repo, err := git.CloneContext(context.Background(), memory.NewStorage(), nil, &git.CloneOptions{ + URL: project, + Progress: os.Stderr, + SingleBranch: true, + Tags: git.AllTags, + }) + + if err != nil { + return errors.WithStack(err) + } + + g = &client.GitClient{ + Repository: repo, + } + } else { + g, err = client.NewGitClient(project) + + if err != nil { + return errors.WithStack(err) + } } - scope, err := parser.Parse(client, options.Version) + scope, err := parser.Parse(g, options.Version) if err != nil { return errors.WithStack(err) } - splices, err := extractor.Extract(client, scope) + splices, err := extractor.Extract(g, scope) if err != nil { return errors.WithStack(err) } - ctxs, err := transformer.Transform(client, splices) + ctxs, err := transformer.Transform(g, splices) if err != nil { return errors.WithStack(err) } - - output, err := generator.Generate(client, ctxs, options.Format, options.Preset, options.TemplateFile) + output, err := generator.Generate(g, ctxs, options.Format, options.Preset, options.TemplateFile) if err != nil { return errors.WithStack(err)