Skip to content

Commit

Permalink
feat(cli): rename '--dir' to '--project' and '--file' to '--output' #4
Browse files Browse the repository at this point in the history
BREAKING CHANGE: new flags

```diff
- --dir=/path/to/dir
+ --project=/path/to/project
```

```diff
- --file=CHANGELOG.md
+ --output=CHANGELOG.md
```
  • Loading branch information
axetroy committed Nov 26, 2020
1 parent 0bb75dd commit 189186a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 35 deletions.
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <latest version>. equivalent to 'whatchanged HEAD~tag:0'
Expand Down Expand Up @@ -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
Expand Down
47 changes: 30 additions & 17 deletions cmd/whatchanged/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <latest version>. equivalent to 'whatchanged HEAD~tag:0'
Expand Down Expand Up @@ -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`)
Expand All @@ -84,7 +97,7 @@ func run() error {
var (
showHelp bool
showVersion bool
projectDir string
project string
preset string
templateFile string
format string
Expand All @@ -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")

Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions option/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type Options struct {
Format Format
Preset Preset
TemplateFile string // Priority is higher than preset
Silent bool
}
49 changes: 41 additions & 8 deletions whatchanged.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 189186a

Please sign in to comment.