From 8f3171633c984a8ef9c714ea8a33438865aa2d8e Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Fri, 16 Feb 2018 15:01:56 +0900 Subject: [PATCH] refactor: Refactor the main logic --- chglog.go | 39 ++++++++++++++++++++++++--------------- chglog_test.go | 6 +++--- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/chglog.go b/chglog.go index d698d3e..0a52df4 100644 --- a/chglog.go +++ b/chglog.go @@ -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 ... @@ -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 { @@ -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 } diff --git a/chglog_test.go b/chglog_test.go index 89801b6..4d81bc3 100644 --- a/chglog_test.go +++ b/chglog_test.go @@ -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",