Skip to content

Commit da822db

Browse files
committed
cleanup and add a basic test case
1 parent 53dde71 commit da822db

File tree

7 files changed

+61
-262
lines changed

7 files changed

+61
-262
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ ignored_branches:
7474
- requires-io-master
7575
- some-other-branch-to-ignore
7676
```
77+
78+
## Development Testing
79+
80+
```sh
81+
go test ./...
82+
```

cmd/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func printCheckResults(resultList [][]string) {
128128
if len(resultList) == 0 {
129129
fmt.Println("Already up-to-date.")
130130
} else {
131-
table := tablewriter.NewWriter(os.Stdout)
131+
table := tablewriter.NewWriter(outWriter)
132132
table.SetHeader([]string{"Repository", "Changed Branch", "Remote URL"})
133133
table.SetBorder(false)
134134
table.AppendBulk(resultList)

cmd/list.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"strings"
@@ -56,7 +55,7 @@ func parseMonitoredBranches(repo string) []string {
5655
var branchList []string
5756
branchFile := filepath.Join(repoDir, repo, ".git-monitor-branches")
5857
if _, err := os.Stat(branchFile); err == nil {
59-
byts, err := ioutil.ReadFile(branchFile)
58+
byts, err := os.ReadFile(branchFile)
6059
if err == nil {
6160
content := strings.Replace(string(byts), "\r\n", "\n", -1)
6261
for _, branch := range strings.Split(content, "\n") {
@@ -73,7 +72,7 @@ func parseMonitoredBranches(repo string) []string {
7372
}
7473

7574
func printListResults(repoList [][]string) {
76-
table := tablewriter.NewWriter(os.Stdout)
75+
table := tablewriter.NewWriter(outWriter)
7776
table.SetHeader([]string{"Repository", "Remote URL", "Monitored Branches"})
7877
table.SetBorder(false)
7978
table.AppendBulk(repoList)

cmd/list_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestListCmd(t *testing.T) {
11+
buf := new(bytes.Buffer)
12+
13+
root := newRootCmd(buf)
14+
root.SetOut(buf)
15+
root.SetErr(buf)
16+
root.SetArgs([]string{"list", "-v", "--repo_dir", "/tmp"})
17+
18+
c, err := root.ExecuteC()
19+
assert.NotNil(t, c)
20+
assert.Nil(t, err)
21+
22+
result := buf.String()
23+
assert.Contains(t, result, "REPOSITORY")
24+
assert.Contains(t, result, "REMOTE URL")
25+
assert.Contains(t, result, "MONITORED BRANCHES")
26+
}

cmd/root.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"path/filepath"
78
"strings"
@@ -14,10 +15,12 @@ import (
1415

1516
var cfgFile string
1617

18+
var outWriter io.Writer
19+
1720
// rootCmd represents the base command when called without any subcommands
18-
var rootCmd *cobra.Command = newRootCmd()
21+
var rootCmd *cobra.Command = newRootCmd(os.Stdout)
1922

20-
func newRootCmd() *cobra.Command {
23+
func newRootCmd(w io.Writer) *cobra.Command {
2124
r := &cobra.Command{
2225
Use: "git-monitor",
2326
Short: "git-monitor keeps track of changes in monitored git repositories",
@@ -31,11 +34,25 @@ you that repository X on branch Y has new commits.`,
3134
Run: runRoot,
3235
}
3336

37+
r.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.git-monitor.yaml)")
38+
39+
r.PersistentFlags().BoolP("verbose", "v", false, "enable verbose output")
40+
viper.BindPFlag("verbose", r.PersistentFlags().Lookup("verbose"))
41+
42+
r.PersistentFlags().BoolP("open_in_browser", "b", false, "open the URL of each repository change in your browser")
43+
viper.BindPFlag("open_in_browser", r.PersistentFlags().Lookup("open_in_browser"))
44+
45+
r.PersistentFlags().String("repo_dir", "", "directory where to store local repositories")
46+
viper.BindPFlag("repo_dir", r.PersistentFlags().Lookup("repo_dir"))
47+
viper.SetDefault("repo_dir", "~/.git-monitor")
48+
3449
r.AddCommand(addCmd)
3550
r.AddCommand(checkCmd)
3651
r.AddCommand(listCmd)
3752
r.AddCommand(removeCmd)
3853

54+
outWriter = w
55+
3956
return r
4057
}
4158

@@ -50,18 +67,6 @@ func Execute(version string) {
5067

5168
func init() {
5269
cobra.OnInitialize(initConfig)
53-
54-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.git-monitor.yaml)")
55-
56-
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "enable verbose output")
57-
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
58-
59-
rootCmd.PersistentFlags().BoolP("open_in_browser", "b", false, "open the URL of each repository change in your browser")
60-
viper.BindPFlag("open_in_browser", rootCmd.PersistentFlags().Lookup("open_in_browser"))
61-
62-
rootCmd.PersistentFlags().String("repo_dir", "", "directory where to store local repositories")
63-
viper.BindPFlag("repo_dir", rootCmd.PersistentFlags().Lookup("repo_dir"))
64-
viper.SetDefault("repo_dir", "~/.git-monitor")
6570
}
6671

6772
// initConfig reads in config file and ENV variables if set.

go.mod

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/kriechi/git-monitor
22

3-
go 1.17
3+
go 1.23.0
4+
5+
toolchain go1.24.1
46

57
require (
68
github.com/go-git/go-git/v5 v5.13.0
@@ -9,6 +11,7 @@ require (
911
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
1012
github.com/spf13/cobra v1.2.1
1113
github.com/spf13/viper v1.9.0
14+
github.com/stretchr/testify v1.10.0
1215
)
1316

1417
require (
@@ -17,6 +20,7 @@ require (
1720
github.com/ProtonMail/go-crypto v1.1.3 // indirect
1821
github.com/cloudflare/circl v1.3.7 // indirect
1922
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
23+
github.com/davecgh/go-spew v1.1.1 // indirect
2024
github.com/emirpasic/gods v1.18.1 // indirect
2125
github.com/fsnotify/fsnotify v1.5.1 // indirect
2226
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
@@ -31,6 +35,7 @@ require (
3135
github.com/mitchellh/mapstructure v1.4.2 // indirect
3236
github.com/pelletier/go-toml v1.9.4 // indirect
3337
github.com/pjbgf/sha1cd v0.3.0 // indirect
38+
github.com/pmezard/go-difflib v1.0.0 // indirect
3439
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
3540
github.com/skeema/knownhosts v1.3.0 // indirect
3641
github.com/spf13/afero v1.6.0 // indirect
@@ -49,4 +54,5 @@ require (
4954
gopkg.in/ini.v1 v1.63.2 // indirect
5055
gopkg.in/warnings.v0 v0.1.2 // indirect
5156
gopkg.in/yaml.v2 v2.4.0 // indirect
57+
gopkg.in/yaml.v3 v3.0.1 // indirect
5258
)

0 commit comments

Comments
 (0)