Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit b4cb364

Browse files
author
Marcin Pronobis
committed
feat: custom logger
1 parent 9aa3988 commit b4cb364

File tree

8 files changed

+63
-28
lines changed

8 files changed

+63
-28
lines changed

cmd/jira-versioner/main.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/kataras/golog"
56
"github.com/psmarcin/jira-versioner/pkg/git"
67
"github.com/psmarcin/jira-versioner/pkg/jira"
78
"github.com/spf13/cobra"
8-
"log"
99
"os"
1010
"path/filepath"
1111
)
@@ -51,6 +51,7 @@ func main() {
5151
}
5252

5353
func rootFunc(c *cobra.Command, args []string) {
54+
log := golog.New()
5455
tag := c.Flag("tag").Value.String()
5556

5657
version := c.Flag("jira-version").Value.String()
@@ -63,28 +64,29 @@ func rootFunc(c *cobra.Command, args []string) {
6364
jiraProject := c.Flag("jira-project").Value.String()
6465
jiraBaseUrl := c.Flag("jira-base-url").Value.String()
6566
gitDir := c.Flag("dir").Value.String()
66-
log.Printf("[JIRA-VERSIONER] git directory: %s", gitDir)
67+
log.Infof("[JIRA-VERSIONER] git directory: %s", gitDir)
6768

68-
g := git.New(gitDir)
69+
g := git.New(gitDir, log)
6970

7071
tasks, err := g.GetTasks(tag)
7172
if err != nil {
72-
log.Panicf("[GIT] error while getting tasks since latest commit %+v", err)
73+
log.Fatalf("[GIT] error while getting tasks since latest commit %+v", err)
7374
}
7475

75-
j, err := jira.New(jiraEmail, jiraToken, jiraProject, jiraBaseUrl)
76+
77+
j, err := jira.New(jiraEmail, jiraToken, jiraProject, jiraBaseUrl, log)
7678
if err != nil {
77-
log.Panicf("[VERSION] error while connecting to jira server %+v", err)
79+
log.Fatalf("[VERSION] error while connecting to jira server %+v", err)
7880
}
7981

8082
_, err = j.CreateVersion(version)
8183
if err != nil {
82-
log.Panicf("[VERSION] error while creating version %+v", err)
84+
log.Fatalf("[VERSION] error while creating version %+v", err)
8385
}
8486

8587
j.LinkTasksToVersion(tasks)
8688

87-
log.Print("[JIRA-VERSIONER] done ✅")
89+
log.Infof("[JIRA-VERSIONER] done ✅")
8890
}
8991

9092
func Execute() {

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ require (
66
github.com/andygrunwald/go-jira v1.12.0
77
github.com/fatih/structs v1.1.0 // indirect
88
github.com/google/go-querystring v1.0.0 // indirect
9+
github.com/kataras/golog v0.0.18
910
github.com/kr/text v0.2.0 // indirect
1011
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1112
github.com/pkg/errors v0.9.1
1213
github.com/spf13/cobra v1.0.0
1314
github.com/spf13/pflag v1.0.5 // indirect
14-
github.com/stretchr/testify v1.4.0
15+
github.com/stretchr/testify v1.6.1
1516
github.com/trivago/tgo v1.0.7 // indirect
17+
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 // indirect
1618
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
17-
gopkg.in/yaml.v2 v2.3.0 // indirect
19+
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
1820
)

go.sum

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
5555
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
5656
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
5757
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
58+
github.com/kataras/golog v0.0.18 h1:Td7hcKN25yzqB/0SO5iohOsMk5Mq5V9kDtM5apaJLY0=
59+
github.com/kataras/golog v0.0.18/go.mod h1:jRYl7dFYqP8aQj9VkwdBUXYZSfUktm+YYg1arJILfyw=
60+
github.com/kataras/pio v0.0.8 h1:6pX6nHJk7DAV3x1dEimibQF2CmJLlo0jWVmM9yE9KY8=
61+
github.com/kataras/pio v0.0.8/go.mod h1:NFfMp2kVP1rmV4N6gH6qgWpuoDKlrOeYi3VrAIWCGsE=
5862
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
5963
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6064
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -107,8 +111,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
107111
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
108112
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
109113
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
110-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
111-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
114+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
115+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
112116
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
113117
github.com/trivago/tgo v1.0.1/go.mod h1:w4dpD+3tzNIIiIfkWWa85w5/B77tlvdZckQ+6PkFnhc=
114118
github.com/trivago/tgo v1.0.7 h1:uaWH/XIy9aWYWpjm2CU3RpcqZXmX2ysQ9/Go+d9gyrM=
@@ -140,7 +144,10 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
140144
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
141145
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
142146
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
147+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
143148
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
149+
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 h1:B6caxRw+hozq68X2MY7jEpZh/cr4/aHLv9xU8Kkadrw=
150+
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
144151
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
145152
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
146153
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -159,6 +166,7 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
159166
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
160167
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
161168
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
162-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
163-
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
169+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
170+
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
171+
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
164172
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pkg/cmd/git.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package cmd
22

33
import (
44
"fmt"
5-
"log"
5+
pslog "github.com/psmarcin/jira-versioner/pkg/log"
66
"strings"
77
)
88

99
// Git keeps all dependency interface
1010
type Git struct {
1111
PreviousTagGetter
1212
CommitGetter
13+
14+
log pslog.Logger
1315
}
1416

1517
// Commit stores basic data about git commit
@@ -22,18 +24,19 @@ type PreviousTagGetter func(name string, arg ...string) (string, error)
2224
type CommitGetter func(name string, arg ...string) (string, error)
2325

2426
// New creates Git with default dependencies
25-
func New() Git {
27+
func New(log pslog.Logger) Git {
2628
return Git{
2729
PreviousTagGetter: Exec,
2830
CommitGetter: Exec,
31+
log: log,
2932
}
3033
}
3134

3235
// GetCommits gets all commits between current and previous tag
3336
func (c Git) GetCommits(currentTag, previousTag, gitPath string) ([]Commit, error) {
3437
var commits []Commit
3538
r := fmt.Sprintf("%s...%s", currentTag, previousTag)
36-
log.Printf("[GIT] found tags: %s", r)
39+
c.log.Infof("[GIT] found tags: %s", r)
3740

3841
out, err := c.CommitGetter("git", "-C", gitPath, "log", "--pretty=format:\"%H;%s\"", "--no-notes", r)
3942
if err != nil {

pkg/cmd/git_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"errors"
5+
"github.com/kataras/golog"
56
"github.com/stretchr/testify/assert"
67
"reflect"
78
"testing"
@@ -102,7 +103,7 @@ func TestGitCommand_GetCommits(t *testing.T) {
102103
wantErr bool
103104
}{
104105
{
105-
name: "should retrun two commits from v1.1.0 to v1.0.0",
106+
name: "should return two commits from v1.1.0 to v1.0.0",
106107
fields: fields{
107108
PreviousTagGetter: nil,
108109
CommitGetter: func(name string, arg ...string) (string, error) {
@@ -157,9 +158,11 @@ sha2;fix: JIR-9899 commit message`, nil
157158
}
158159
for _, tt := range tests {
159160
t.Run(tt.name, func(t *testing.T) {
161+
l := golog.New()
160162
c := Git{
161163
PreviousTagGetter: tt.fields.PreviousTagGetter,
162164
CommitGetter: tt.fields.CommitGetter,
165+
log: l,
163166
}
164167
got, err := c.GetCommits(tt.args.tag, tt.args.previousTag, ".")
165168
if (err != nil) != tt.wantErr {
@@ -174,6 +177,7 @@ sha2;fix: JIR-9899 commit message`, nil
174177
}
175178

176179
func TestNew(t *testing.T) {
177-
g := New()
180+
l:= golog.New()
181+
g := New(l)
178182
assert.NotEmpty(t, g)
179183
}

pkg/git/git.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package git
22

33
import (
44
"github.com/psmarcin/jira-versioner/pkg/cmd"
5+
pslog "github.com/psmarcin/jira-versioner/pkg/log"
56
"regexp"
67
)
78

89
// Git keeps only dependencies
910
type Git struct {
1011
Path string
1112
Dependencies Getter
13+
log pslog.Logger
1214
}
1315

1416
// Getter is interface for GetTasks dependencies for easier mocking
@@ -18,11 +20,12 @@ type Getter interface {
1820
}
1921

2022
// New creates Git with default dependencies
21-
func New(path string) Git {
22-
command := cmd.New()
23+
func New(path string, log pslog.Logger) Git {
24+
command := cmd.New(log)
2325
return Git{
2426
path,
2527
command,
28+
log,
2629
}
2730
}
2831

pkg/jira/jira.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package jira
33
import (
44
"github.com/andygrunwald/go-jira"
55
"github.com/pkg/errors"
6+
pslog "github.com/psmarcin/jira-versioner/pkg/log"
67
"io/ioutil"
7-
"log"
88
"strconv"
99
"time"
1010
)
@@ -16,6 +16,7 @@ type Jira struct {
1616
Project *jira.Project
1717
ProjectID string
1818
Version *jira.Version
19+
log pslog.Logger
1920
}
2021

2122
type UpdatePayload struct {
@@ -33,8 +34,10 @@ type IdVersion struct {
3334
}
3435

3536
// New creates Jira instance with all required details like email, token, base url
36-
func New(email, token, projectId, baseUrl string) (Jira, error) {
37-
j := Jira{}
37+
func New(email, token, projectId, baseUrl string, log pslog.Logger) (Jira, error) {
38+
j := Jira{
39+
log: log,
40+
}
3841
tp := jira.BasicAuthTransport{
3942
Username: email,
4043
Password: token,
@@ -85,7 +88,7 @@ func (j *Jira) CreateVersion(name string) (*jira.Version, error) {
8588
}
8689
if isFound == true {
8790
j.Version = version
88-
log.Printf("[JIRA] version %s already exists, skip creating", j.Version.Name)
91+
j.log.Infof("[JIRA] version %s already exists, skip creating", j.Version.Name)
8992
return version, nil
9093
}
9194

@@ -111,7 +114,7 @@ func (j *Jira) CreateVersion(name string) (*jira.Version, error) {
111114

112115
j.Version = version
113116

114-
log.Printf("[JIRA] version created %s", j.Version.Name)
117+
j.log.Infof("[JIRA] version created %s", j.Version.Name)
115118

116119
return version, nil
117120
}
@@ -121,7 +124,7 @@ func (j Jira) LinkTasksToVersion(taskIds []string) {
121124
for _, taskId := range taskIds {
122125
err := j.SetIssueVersion(taskId)
123126
if err != nil {
124-
log.Printf("[JIRA] can't update task %s to fixed version %s (%s)", taskId, j.Version.Name, j.Version.ID)
127+
j.log.Warnf("[JIRA] can't update task %s to fixed version %s (%s)", taskId, j.Version.Name, j.Version.ID)
125128
}
126129
}
127130
}
@@ -152,6 +155,6 @@ func (j Jira) SetIssueVersion(taskID string) error {
152155
return errors.Wrap(err, string(body))
153156
}
154157

155-
log.Printf("[JIRA] task updated %s", taskID)
158+
j.log.Infof("[JIRA] task updated %s", taskID)
156159
return nil
157160
}

pkg/log/log.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package log
2+
3+
type Logger interface {
4+
Info(v ...interface{})
5+
Infof(message string, args ...interface{})
6+
Warn(v ...interface{})
7+
Warnf(message string, args ...interface{})
8+
Fatal(v ...interface{})
9+
Fatalf(message string, args ...interface{})
10+
}

0 commit comments

Comments
 (0)