Skip to content

Commit

Permalink
rename var
Browse files Browse the repository at this point in the history
  • Loading branch information
mesuutt committed Jun 25, 2023
1 parent f220872 commit e875862
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion internal/command/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func AddCmdAction(ctx *cli.Context) error {
// TODO: ignore already added commit
// if user run add multiple times without new commit, it should add only one commit to repo
// maybe we can add commit hast to commit message, and check it at next commit
diff, err := commitGen.GenDiff(stats, commit.CommitInfo{Time: time.Now()})
diff, err := commitGen.GenDiff(stats, commit.Meta{Time: time.Now()})
if err != nil {
return err
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/commit/commit.go

This file was deleted.

22 changes: 11 additions & 11 deletions pkg/commit/diffgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewDiffGenerator(conf *config.Config) diffGen {
return diffGen{conf: conf}
}

func (f diffGen) GenDiff(stats []FileStat, commitInfo CommitInfo) (*Diff, error) {
func (f diffGen) GenDiff(stats []FileStat, commitMeta Meta) (*Diff, error) {
mergedFileStats := make(map[string]FileStat)
for _, stat := range stats {
fileType := f.decideNewExtension(f.clearDot(stat.Ext))
Expand Down Expand Up @@ -48,12 +48,12 @@ func (f diffGen) GenDiff(stats []FileStat, commitInfo CommitInfo) (*Diff, error)
filename = fmt.Sprintf("log.%s", fileType)
}

contentText, err := f.generateContentText(&stat, fileType, &commitInfo)
contentText, err := f.generateContentText(&stat, fileType, &commitMeta)
if err != nil {
return nil, err
}

text, err := f.generateCode(fileType, contentText, commitInfo)
text, err := f.generateCode(fileType, contentText, commitMeta)
if err != nil {
return nil, fmt.Errorf("log text generation failed, err: %w", err)
}
Expand All @@ -70,7 +70,7 @@ func (f diffGen) GenDiff(stats []FileStat, commitInfo CommitInfo) (*Diff, error)
return &diff, nil
}

func (f diffGen) generateCode(fileType string, contentText string, commitInfo CommitInfo) (string, error) {
func (f diffGen) generateCode(fileType string, contentText string, commitMeta Meta) (string, error) {
if tmpl, ok := f.conf.Templates[fileType]; ok {
codeTemplate, err := template.New("code").Parse(tmpl)
if err != nil {
Expand All @@ -80,9 +80,9 @@ func (f diffGen) generateCode(fileType string, contentText string, commitInfo Co
var buf bytes.Buffer
varMap := map[string]interface{}{
"Message": contentText,
"HM": commitInfo.Time.Format("15:04"),
"Hour": commitInfo.Time.Format("15"),
"Minute": commitInfo.Time.Format("04"),
"HM": commitMeta.Time.Format("15:04"),
"Hour": commitMeta.Time.Format("15"),
"Minute": commitMeta.Time.Format("04"),
}

if err := codeTemplate.Execute(&buf, varMap); err != nil {
Expand All @@ -95,7 +95,7 @@ func (f diffGen) generateCode(fileType string, contentText string, commitInfo Co
return contentText, nil
}

func (f diffGen) generateContentText(stat *FileStat, fileType string, commitInfo *CommitInfo) (string, error) {
func (f diffGen) generateContentText(stat *FileStat, fileType string, commitMeta *Meta) (string, error) {
// TODO: we can move template.New to constructor, init or once.Do
commitTemplate, err := template.New("log").Parse(f.conf.Commit.Template)
if err != nil {
Expand All @@ -107,9 +107,9 @@ func (f diffGen) generateContentText(stat *FileStat, fileType string, commitInfo
Hour string
Minute string
}{
HM: commitInfo.Time.Format("15:04"),
Hour: commitInfo.Time.Format("15"),
Minute: commitInfo.Time.Format("04"),
HM: commitMeta.Time.Format("15:04"),
Hour: commitMeta.Time.Format("15"),
Minute: commitMeta.Time.Format("04"),
}

commitTemplateVarMap := map[string]interface{}{
Expand Down
10 changes: 5 additions & 5 deletions pkg/commit/diffgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestGenerateDiffWithDefaultsWhenConfigNotGiven(t *testing.T) {

gen := NewDiffGenerator(config.Default())
// when
diff, err := gen.GenDiff([]FileStat{stat}, CommitInfo{Time: time.Now()})
diff, err := gen.GenDiff([]FileStat{stat}, Meta{Time: time.Now()})

// then
assert.Nil(t, err)
Expand All @@ -34,7 +34,7 @@ func TestGenerateDiffFilenameShouldNotChangedWhenNotHasDot(t *testing.T) {

gen := NewDiffGenerator(config.Default())
// when
diff, err := gen.GenDiff([]FileStat{stat}, CommitInfo{Time: time.Now()})
diff, err := gen.GenDiff([]FileStat{stat}, Meta{Time: time.Now()})

// then
assert.Nil(t, err)
Expand All @@ -53,7 +53,7 @@ func TestGenerateDiffWithCustomLogTemplate(t *testing.T) {

// when
commitTime := time.Now()
diff, err := gen.GenDiff([]FileStat{stat}, CommitInfo{Time: commitTime})
diff, err := gen.GenDiff([]FileStat{stat}, Meta{Time: commitTime})

// then
assert.Nil(t, err)
Expand All @@ -77,7 +77,7 @@ func TestGenerateDiffWithFileTypeOverwrites(t *testing.T) {
})

// when
diff, err := gen.GenDiff([]FileStat{stat, stat2, stat3}, CommitInfo{Time: time.Now()})
diff, err := gen.GenDiff([]FileStat{stat, stat2, stat3}, Meta{Time: time.Now()})

// then
assert.Nil(t, err)
Expand All @@ -103,7 +103,7 @@ func TestGenerateDiffWithCodeTemplates(t *testing.T) {

// when
commitTime := time.Now()
diff, err := gen.GenDiff([]FileStat{stat}, CommitInfo{Time: commitTime})
diff, err := gen.GenDiff([]FileStat{stat}, Meta{Time: commitTime})

// then
assert.Nil(t, err)
Expand Down
7 changes: 7 additions & 0 deletions pkg/commit/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package commit

import "time"

type FileStat struct {
Insert int
Delete int
Expand All @@ -17,3 +19,8 @@ type Change struct {
Insertion int
Deletion int
}

type Meta struct {
Time time.Time
Hash string
}

0 comments on commit e875862

Please sign in to comment.