Skip to content

Commit b44ec93

Browse files
committed
Improve public interface
1 parent cb65f32 commit b44ec93

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

common/service.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package common
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67
"os/user"
@@ -69,18 +70,43 @@ func NewService() (Service, error) {
6970
}
7071

7172
func (srv Service) Enable() error {
72-
// TODO: Uninstall the old one, in case it was running
73-
// Also stop the old service?
73+
s := srv.Service
7474

75-
err := srv.Service.Install()
75+
status, err := s.Status()
76+
if err != nil {
77+
if !strings.Contains(err.Error(), "the service is not installed") {
78+
return tracerr.Wrap(err)
79+
}
80+
}
81+
82+
stopped := false
83+
if status == service.StatusRunning {
84+
err := s.Stop()
85+
if err != nil {
86+
return tracerr.Wrap(err)
87+
}
88+
stopped = true
89+
}
90+
91+
err = s.Install()
7692
if err != nil {
7793
if strings.Contains(err.Error(), "Init already exists") {
78-
return nil
94+
s.Uninstall()
95+
s.Install()
96+
} else {
97+
return tracerr.Wrap(err)
7998
}
80-
return tracerr.Wrap(err)
99+
} else {
100+
fmt.Println("Installing git-auto-sync as a daemon")
101+
}
102+
103+
if stopped {
104+
fmt.Println("Restarting git-auto-sync-daemon")
105+
} else {
106+
fmt.Println("Starting git-auto-sync-daemon")
81107
}
82108

83-
err = srv.Service.Restart()
109+
err = s.Start()
84110
if err != nil {
85111
return tracerr.Wrap(err)
86112
}
@@ -89,11 +115,13 @@ func (srv Service) Enable() error {
89115
}
90116

91117
func (srv Service) Disable() error {
118+
fmt.Println("Stopping git-auto-sync-daemon")
92119
err := srv.Service.Stop()
93120
if err != nil {
94121
return tracerr.Wrap(err)
95122
}
96123

124+
fmt.Println("Uninstalling git-auto-sync as a daemon")
97125
err = srv.Service.Uninstall()
98126
if err != nil {
99127
return tracerr.Wrap(err)

daemon.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/GitJournal/git-auto-sync/common"
1111
cli "github.com/urfave/cli/v2"
1212
"github.com/ztrue/tracerr"
13+
"golang.org/x/exp/slices"
1314
git "gopkg.in/src-d/go-git.v4"
1415
)
1516

@@ -46,7 +47,6 @@ func daemonAdd(ctx *cli.Context) error {
4647
}
4748

4849
repoPath = filepath.Join(cwd, repoPath)
49-
// TODO: Check if the parent/ancestor is the repoPath!
5050
}
5151

5252
repoPath, err := isValidGitRepo(repoPath)
@@ -59,15 +59,9 @@ func daemonAdd(ctx *cli.Context) error {
5959
return tracerr.Wrap(err)
6060
}
6161

62-
contains := false
63-
for _, rp := range config.Repos {
64-
if rp == repoPath {
65-
contains = true
66-
break
67-
}
68-
}
69-
70-
if !contains {
62+
if slices.Contains(config.Repos, repoPath) {
63+
fmt.Println("The Daemon is already monitoring " + repoPath)
64+
} else {
7165
config.Repos = append(config.Repos, repoPath)
7266
}
7367

@@ -105,16 +99,20 @@ func isValidGitRepo(repoPath string) (string, error) {
10599
}
106100

107101
for true {
108-
_, err := os.Stat(repoPath)
102+
info, err := os.Stat(filepath.Join(repoPath, ".git"))
109103
if err != nil {
110104
return "", tracerr.Errorf("%w - %s", errRepoPathInvalid, repoPath)
111105
}
112106

113-
if repoPath == "." {
114-
return "", tracerr.Errorf("%w - %s", errRepoPathInvalid, repoPath)
107+
if os.IsNotExist(err) {
108+
repoPath = filepath.Dir(repoPath)
109+
continue
115110
}
116111

117-
repoPath = filepath.Dir(repoPath)
112+
if !info.IsDir() {
113+
return "", tracerr.Errorf("%w - %s", errRepoPathInvalid, repoPath)
114+
}
115+
break
118116
}
119117

120118
return repoPath, nil

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
3232
github.com/xanzy/ssh-agent v0.2.1 // indirect
3333
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
34+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
3435
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
3536
golang.org/x/sys v0.0.0-20220429233432-b5fbb4746d32 // indirect
3637
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8U
8585
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
8686
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
8787
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
88+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4=
89+
golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
8890
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
8991
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
9092
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func main() {
5555
Aliases: []string{"d"},
5656
Usage: "Interact with the background daemon",
5757
Subcommands: []*cli.Command{
58+
// FIXME: Add a command to show the daemon's logs
5859
{
5960
Name: "status",
6061
Usage: "Show the Daemon's status",

0 commit comments

Comments
 (0)