Skip to content

Commit 5cc1860

Browse files
author
Andrei
committed
Tidy up
1 parent 2c171bf commit 5cc1860

File tree

7 files changed

+7
-30
lines changed

7 files changed

+7
-30
lines changed

cmd/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var rootCmd = &cobra.Command{
1515
}
1616

1717
func Execute() {
18-
// Load and start any configured watchers (fast startup).
1918
supervisor.StartAll()
2019

2120
if err := rootCmd.Execute(); err != nil {

go.mod

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

1313
require (
14-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
15-
github.com/spf13/pflag v1.0.10 // indirect
16-
golang.org/x/sys v0.38.0 // indirect
14+
github.com/inconshreveable/mousetrap v1.1.0
15+
github.com/spf13/pflag v1.0.10
16+
golang.org/x/sys v0.38.0
1717
)

guardian

-4.29 MB
Binary file not shown.

internal/git/git.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99
)
1010

11-
// hasChanges returns true if there are staged or unstaged changes.
1211
func hasChanges(folder string) (bool, error) {
1312
cmd := exec.Command("git", "-C", folder, "status", "--porcelain")
1413
out, err := cmd.Output()
@@ -18,31 +17,24 @@ func hasChanges(folder string) (bool, error) {
1817
return len(bytes.TrimSpace(out)) > 0, nil
1918
}
2019

21-
// CommitAndPush adds, commits (if changes), and pushes.
22-
// It will skip commits if there are no changes.
2320
func CommitAndPush(folder string) error {
2421
changed, err := hasChanges(folder)
2522
if err != nil {
2623
return fmt.Errorf("git status failed: %w", err)
2724
}
2825
if !changed {
29-
// nothing to commit
3026
return nil
3127
}
3228

33-
// git add .
3429
if out, err := exec.Command("git", "-C", folder, "add", ".").CombinedOutput(); err != nil {
3530
return fmt.Errorf("git add failed: %v: %s", err, strings.TrimSpace(string(out)))
3631
}
3732

38-
// commit
3933
msg := "Auto backup " + time.Now().Format(time.RFC3339)
4034
if out, err := exec.Command("git", "-C", folder, "commit", "-m", msg).CombinedOutput(); err != nil {
41-
// ignore "nothing to commit" or other benign messages
4235
return fmt.Errorf("git commit failed: %v: %s", err, strings.TrimSpace(string(out)))
4336
}
4437

45-
// push
4638
if out, err := exec.Command("git", "-C", folder, "push").CombinedOutput(); err != nil {
4739
return fmt.Errorf("git push failed: %v: %s", err, strings.TrimSpace(string(out)))
4840
}

internal/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type WatcherState struct {
1111
ID string `json:"id"`
1212
Folder string `json:"folder"`
13-
Mode string `json:"mode"` // "watch" | "interval" | "none"
13+
Mode string `json:"mode"`
1414
Interval string `json:"interval,omitempty"`
1515
Debounce string `json:"debounce,omitempty"`
1616
Paused bool `json:"paused"`

internal/supervisor/supervisor.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func StartAll() {
2727
}
2828
}
2929

30-
// StartAllBlocking is used by daemon (blocks forever).
30+
3131
func StartAllBlocking() {
3232
StartAll()
3333
select {}
@@ -37,7 +37,7 @@ func startWatcher(st state.WatcherState) {
3737
mu.Lock()
3838
if _, ok := running[st.ID]; ok {
3939
mu.Unlock()
40-
return // already running
40+
return
4141
}
4242
stop := make(chan struct{})
4343
running[st.ID] = stop
@@ -51,7 +51,6 @@ func startWatcher(st state.WatcherState) {
5151
}()
5252

5353
for {
54-
// auto-remove if folder disappears
5554
if _, err := os.Stat(s.Folder); os.IsNotExist(err) {
5655
fmt.Println("[supervisor] folder removed, cancelling:", s.Folder)
5756
Remove(s.ID)
@@ -71,13 +70,12 @@ func startWatcher(st state.WatcherState) {
7170
}
7271
select {
7372
case <-time.After(d):
74-
// continue
73+
7574
case <-stopCh:
7675
return
7776
}
7877

7978
case "watch":
80-
// Run watcher in goroutine and make it cancelable via stopCh by exiting goroutine on stop.
8179
done := make(chan struct{})
8280
go func() {
8381
_ = watcher.WatchAndDebounce(s.Folder, s.Debounce, func() {
@@ -94,10 +92,8 @@ func startWatcher(st state.WatcherState) {
9492
case <-stopCh:
9593
return
9694
case <-done:
97-
// watcher finished (unexpectedly) — restart loop which will try again.
9895
}
9996
default:
100-
// nothing to do
10197
return
10298
}
10399
}
@@ -172,11 +168,9 @@ func updateLastRun(id string) {
172168
state.Update(list)
173169
}
174170

175-
// GenerateLaunchdPlist produces the plist contents to start `guardian daemon`.
176171
func GenerateLaunchdPlist() string {
177172
bin, _ := exec.LookPath("guardian")
178173
if bin == "" {
179-
// common Homebrew bin
180174
bin = "/usr/local/bin/guardian"
181175
}
182176
home, _ := os.UserHomeDir()

internal/watcher/watcher.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"github.com/fsnotify/fsnotify"
99
)
1010

11-
// WatchAndDebounce watches a folder (non-recursive) and debounces events.
12-
// If you need recursive watching, consider walking the tree and adding subdirs.
1311
func WatchAndDebounce(folder string, debounceStr string, fn func()) error {
1412
debounce, err := time.ParseDuration(debounceStr)
1513
if err != nil || debounce <= 0 {
@@ -22,18 +20,12 @@ func WatchAndDebounce(folder string, debounceStr string, fn func()) error {
2220
}
2321
defer w.Close()
2422

25-
// add the folder itself
2623
if err := w.Add(folder); err != nil {
2724
return err
2825
}
2926

30-
// optional: also watch immediate subdirectories (common case)
31-
// NOTE: large projects with many subdirs may need a recursive strategy.
3227
entries, _ := filepath.Glob(filepath.Join(folder, "*"))
3328
for _, e := range entries {
34-
// watch only directories
35-
// ignore errors — best-effort
36-
// if fi, err := os.Stat(e); err == nil && fi.IsDir() { w.Add(e) }
3729
_ = w.Add(e)
3830
}
3931

0 commit comments

Comments
 (0)