Skip to content

Commit

Permalink
refactor: Refactor the main logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed Feb 16, 2018
1 parent 2d5764a commit 8f31716
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
39 changes: 24 additions & 15 deletions chglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ type RenderData struct {

// Config ...
type Config struct {
Bin string
Path string
Template string
Info *Info
Options *Options
Bin string
WorkingDir string
Template string
Info *Info
Options *Options
}

// Generator ...
Expand Down Expand Up @@ -98,19 +98,11 @@ func (gen *Generator) Generate(w io.Writer, query string) error {
}

func (gen *Generator) readVersions(query string) ([]*Version, error) {
tags, err := gen.tagReader.ReadAll()
tags, first, err := gen.getTags(query)
if err != nil {
return nil, err
}

first := ""
if query != "" {
tags, first, err = gen.tagSelector.Select(tags, query)
if err != nil {
return nil, err
}
}

versions := []*Version{}

for i, tag := range tags {
Expand Down Expand Up @@ -145,13 +137,30 @@ func (gen *Generator) readVersions(query string) ([]*Version, error) {
return versions, nil
}

func (gen *Generator) getTags(query string) ([]*Tag, string, error) {
tags, err := gen.tagReader.ReadAll()
if err != nil {
return nil, "", err
}

first := ""
if query != "" {
tags, first, err = gen.tagSelector.Select(tags, query)
if err != nil {
return nil, "", err
}
}

return tags, first, nil
}

func (gen *Generator) workdir() (func() error, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}

err = os.Chdir(gen.config.Path)
err = os.Chdir(gen.config.WorkingDir)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions chglog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func TestGeneratorWithTypeScopeSubject(t *testing.T) {
})

gen := NewGenerator(&Config{
Bin: "git",
Path: filepath.Join(testRepoRoot, testName),
Template: filepath.Join("fixtures", testName+".md"),
Bin: "git",
WorkingDir: filepath.Join(testRepoRoot, testName),
Template: filepath.Join("fixtures", testName+".md"),
Info: &Info{
Title: "CHANGELOG Example",
RepositoryURL: "/~https://github.com/git-chglog/git-chglog",
Expand Down

0 comments on commit 8f31716

Please sign in to comment.