Skip to content

Commit 6673fa4

Browse files
committed
Format files and add go modules support
1 parent 8627e66 commit 6673fa4

13 files changed

Lines changed: 95 additions & 68 deletions

config_reader.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ type ConfigReader interface {
1313
Read(path string) (*Config, error)
1414
}
1515

16-
type JsonConfigReader struct {}
16+
type JsonConfigReader struct{}
1717

1818
func (c *JsonConfigReader) Read(path string) (*Config, error) {
1919
file, err := os.Open(path)
20-
if err != nil { return nil, err }
20+
if err != nil {
21+
return nil, err
22+
}
2123

2224
decoder := json.NewDecoder(file)
2325

2426
var config Config
2527
err = decoder.Decode(&config)
26-
if err != nil { return nil, err }
28+
if err != nil {
29+
return nil, err
30+
}
2731

2832
return &config, nil
2933
}

config_reader_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package main
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestJsonConfigReader_Read(t *testing.T) {

git.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
const (
14-
Error State = "error"
15-
Dirty State = "dirty"
14+
Error State = "error"
15+
Dirty State = "dirty"
1616
Ahead State = "ahead"
1717
OutOfSync State = "out-of-sync"
1818
Sync State = "sync"
@@ -27,8 +27,7 @@ type Git interface {
2727
Update(path string) error
2828
}
2929

30-
type GitCmd struct {
31-
}
30+
type GitCmd struct{}
3231

3332
func (g *GitCmd) Sync(path string) error {
3433
state, err := g.GetState(path)
@@ -60,7 +59,7 @@ func (g *GitCmd) Sync(path string) error {
6059
}
6160
}
6261

63-
func runCmd(path string, command string, args... string) (string, error) {
62+
func runCmd(path string, command string, args ...string) (string, error) {
6463
cmd := exec.Command(command, args...)
6564
cmd.Dir = path
6665

@@ -159,9 +158,8 @@ func GetStateAgainstRemote(path string) (State, error) {
159158

160159
func (g *GitCmd) Update(path string) error {
161160
state, err := g.GetState(path)
162-
163161
if err != nil {
164-
return err
162+
return err
165163
}
166164

167165
switch state {

git_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/stretchr/testify/assert"
6-
"github.com/tanin47/git-notes/internal/test_helpers"
75
"log"
86
"os"
97
"testing"
10-
)
118

9+
"github.com/stretchr/testify/assert"
10+
"github.com/tanin47/git-notes/internal/test_helpers"
11+
)
1212

1313
func assertState(t *testing.T, path string, expectedState State) {
1414
gogit := GitCmd{}
@@ -293,4 +293,3 @@ func TestGoGit_SyncFixConflict(t *testing.T) {
293293
performSync(t, repos.Local)
294294
assertState(t, repos.Local, Sync)
295295
}
296-

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/tanin47/git-notes
2+
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.8.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
12+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
15+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/test_helpers/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package test_helpers
22

33
import (
44
"fmt"
5-
"github.com/stretchr/testify/assert"
65
"io/ioutil"
76
"log"
87
"os"
98
"os/exec"
109
"strings"
1110
"testing"
11+
12+
"github.com/stretchr/testify/assert"
1213
)
1314

1415
type Repos struct {
@@ -76,11 +77,10 @@ func SetupRemote(local string, remote string) {
7677
func WriteFile(t *testing.T, repoPath string, filePath string, content string) {
7778
fullPath := fmt.Sprintf("%s/%s", repoPath, filePath)
7879
log.Printf("Write file: %v, content: %v", fullPath, content)
79-
assert.NoError(t, ioutil.WriteFile(fullPath, []byte(content), 0644))
80+
assert.NoError(t, ioutil.WriteFile(fullPath, []byte(content), 0o644))
8081
}
8182

82-
83-
func PerformCmd(t *testing.T, path string, cmd string, args... string) {
83+
func PerformCmd(t *testing.T, path string, cmd string, args ...string) {
8484
log.Printf("Run cmd: %v", strings.Join(append([]string{cmd}, args...), " "))
8585
c := exec.Command(cmd, args...)
8686
c.Dir = path

main.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ func main() {
1919

2020
log.Println("Git Notes is starting...")
2121

22-
var git = NewGoGit()
23-
var watcher = GitWatcher{
24-
git: &git,
25-
running: false,
26-
checkInterval: 10 * time.Second,
22+
git := NewGoGit()
23+
watcher := GitWatcher{
24+
git: &git,
25+
running: false,
26+
checkInterval: 10 * time.Second,
2727
delayBeforeFiringEvent: 2 * time.Second,
28-
delayAfterFiringEvent: 5 * time.Second,
28+
delayAfterFiringEvent: 5 * time.Second,
2929
}
30-
var configReader = JsonConfigReader{}
31-
var gitRepoMonitor = GitRepoMonitor{
30+
configReader := JsonConfigReader{}
31+
gitRepoMonitor := GitRepoMonitor{
3232
scheduledUpdateInterval: 5 * time.Minute,
3333
}
3434

@@ -45,7 +45,6 @@ func Run(git Git, watcher Watcher, configReader ConfigReader, monitor PathMonito
4545
}
4646
configPath := os.Args[1]
4747
config, err := configReader.Read(configPath)
48-
4948
if err != nil {
5049
log.Fatalf("Unable to read the config file. Err: %v", err)
5150
}
@@ -55,4 +54,3 @@ func Run(git Git, watcher Watcher, configReader ConfigReader, monitor PathMonito
5554
monitor.StartMonitoring(repoPath, watcher, git)
5655
}
5756
}
58-

main_test.go

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

33
import (
44
"fmt"
5-
"github.com/stretchr/testify/assert"
6-
"github.com/tanin47/git-notes/internal/test_helpers"
75
"io/ioutil"
86
"os"
97
"testing"
108
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/tanin47/git-notes/internal/test_helpers"
1112
)
1213

1314
func TestMainFunc(t *testing.T) {
1415
Running = true
1516

16-
var git = NewGoGit()
17+
git := NewGoGit()
1718

1819
repos := test_helpers.SetupRepos()
1920
defer test_helpers.CleanupRepos(repos)
@@ -41,7 +42,7 @@ func TestMainFunc(t *testing.T) {
4142
state, err := git.GetState(repos.Local)
4243
assert.NoError(t, err)
4344
return state == Sync
44-
}, 15 * time.Second, 1 * time.Second)
45+
}, 15*time.Second, 1*time.Second)
4546

4647
test_helpers.WriteFile(t, repos.Local, "test.md", "TestContent2")
4748

@@ -53,16 +54,16 @@ func TestMainFunc(t *testing.T) {
5354
state, err := git.GetState(repos.Local)
5455
assert.NoError(t, err)
5556
return state == Sync
56-
}, 15 * time.Second, 1 * time.Second)
57+
}, 15*time.Second, 1*time.Second)
5758

5859
Running = false
5960
}
6061

6162
func TestRun(t *testing.T) {
62-
var git = MockGit{}
63-
var watcher = MockWatcher{}
64-
var configReader = MockConfigReader{}
65-
var monitor = MockMonitor{}
63+
git := MockGit{}
64+
watcher := MockWatcher{}
65+
configReader := MockConfigReader{}
66+
monitor := MockMonitor{}
6667

6768
oldArgs := os.Args
6869
os.Args = []string{"app", "some-git-notes.json"}
@@ -80,7 +81,7 @@ type MockConfigReader struct {
8081

8182
func (m *MockConfigReader) Read(path string) (*Config, error) {
8283
m.readPath = path
83-
var config = &Config{
84+
config := &Config{
8485
Repos: []string{"some-path", "some-path-2"},
8586
}
8687
return config, nil

path_monitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (g *GitRepoMonitor) scheduleUpdate(repoPath string, channel chan string) {
2222
}
2323

2424
func (g *GitRepoMonitor) StartMonitoring(repoPath string, watcher Watcher, git Git) {
25-
var channel = make(chan string)
25+
channel := make(chan string)
2626
err := git.Sync(repoPath)
2727
if err != nil {
2828
log.Printf("Syncing failed. Err: %v", err)

0 commit comments

Comments
 (0)