Skip to content

Commit cf4c717

Browse files
author
Andrei
committed
Make the pathing relative
1 parent 8260ab2 commit cf4c717

File tree

6 files changed

+100
-23
lines changed

6 files changed

+100
-23
lines changed

cmd/init.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@ import (
1212

1313
var initCmd = &cobra.Command{
1414
Use: "init",
15-
Short: "Create a new GitHub repository using the GitHub CLI (`gh`) and initialize locally",
15+
Short: "Initialize Guardian in this folder (uses existing Git repo if present)",
1616
RunE: func(cmd *cobra.Command, args []string) error {
1717
rd := bufio.NewReader(os.Stdin)
1818

19+
if _, err := os.Stat(".git"); err == nil {
20+
fmt.Println("This folder already has a Git repository.")
21+
fmt.Print("Guardian will use the existing repository. Continue? [Y/n]: ")
22+
resp, _ := rd.ReadString('\n')
23+
resp = strings.ToLower(strings.TrimSpace(resp))
24+
if resp == "n" {
25+
fmt.Println("Aborting init.")
26+
return nil
27+
}
28+
fmt.Println("Guardian will use the existing Git repository for automatic backups.")
29+
return nil
30+
}
31+
1932
fmt.Print("Repository name (user/repo or repo): ")
2033
name, _ := rd.ReadString('\n')
2134
name = strings.TrimSpace(name)
@@ -36,7 +49,18 @@ var initCmd = &cobra.Command{
3649
return fmt.Errorf("failed to create repo via gh: %w", err)
3750
}
3851

39-
fmt.Println("Created GitHub repo:", name)
52+
addRemote := exec.Command("git", "remote", "add", "origin", fmt.Sprintf("[email protected]:%s.git", name))
53+
addRemote.Run()
54+
55+
status := exec.Command("git", "status", "--porcelain")
56+
out, _ := status.Output()
57+
if len(out) > 0 {
58+
exec.Command("git", "add", ".").Run()
59+
exec.Command("git", "commit", "-m", "Initial commit by Guardian").Run()
60+
exec.Command("git", "push", "-u", "origin", "main").Run()
61+
}
62+
63+
fmt.Println("Guardian setup complete. GitHub repository linked and ready for backups.")
4064
return nil
4165
},
4266
}

cmd/link.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,39 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

7-
"github.com/google/uuid"
88
"github.com/spf13/cobra"
99
"github.com/itzcodex24/git-guardian/internal/state"
1010
)
1111

1212
var linkCmd = &cobra.Command{
13-
Use: "link <folder>",
13+
Use: "link [folder]",
14+
Short: "Link a folder to Guardian for automatic backups",
1415
Args: cobra.ExactArgs(1),
15-
Short: "Link an existing folder to guardian (adds it to persistent state, initially paused)",
1616
RunE: func(cmd *cobra.Command, args []string) error {
1717
folder := args[0]
18-
if _, err := os.Stat(folder); os.IsNotExist(err) {
19-
return fmt.Errorf("folder does not exist: %s", folder)
18+
absPath, err := filepath.Abs(folder)
19+
if err != nil {
20+
return fmt.Errorf("failed to resolve folder path: %w", err)
2021
}
22+
23+
// Clean the path to remove any redundant separators
24+
absPath = filepath.Clean(absPath)
2125

22-
w := state.WatcherState{
23-
ID: uuid.NewString()[:8],
24-
Folder: folder,
25-
Mode: "none",
26-
Paused: true,
26+
info, err := os.Stat(absPath)
27+
if err != nil {
28+
return fmt.Errorf("folder does not exist: %w", err)
2729
}
28-
state.Append(w)
29-
fmt.Println("Linked folder (paused):", folder, "id:", w.ID)
30-
fmt.Println("Run `guardian start <folder> --watch` or `--interval <duration>` to enable.")
30+
if !info.IsDir() {
31+
return fmt.Errorf("not a directory")
32+
}
33+
34+
if err := state.AddFolder(absPath); err != nil {
35+
return fmt.Errorf("failed to link folder: %w", err)
36+
}
37+
38+
fmt.Println("Linked folder:", absPath)
3139
return nil
3240
},
3341
}

cmd/listeners.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ var listenersCmd = &cobra.Command{
1313
Short: "List all configured watchers",
1414
RunE: func(cmd *cobra.Command, args []string) error {
1515
list := supervisor.List()
16-
fmt.Printf("\n%-8s %-10s %-45s %-10s %-25s\n", "ID", "Mode", "Folder", "Status", "LastRun")
16+
fmt.Printf("\n%-8s %-15s %-38s %-10s %-25s\n", "ID", "Mode", "Folder", "Status", "LastRun")
1717
fmt.Println(strings.Repeat("-", 110))
1818
for _, w := range list {
1919
status := "paused"
2020
if !w.Paused {
2121
status = "running"
2222
}
23-
fmt.Printf("%-8s %-10s %-45s %-10s %-25s\n", w.ID, w.Mode, w.Folder, status, w.LastRun)
23+
mode := w.Mode
24+
if w.Mode == "interval" && w.Interval != "" {
25+
mode = fmt.Sprintf("interval (%s)", w.Interval)
26+
} else if w.Mode == "watch" && w.Debounce != "" {
27+
mode = fmt.Sprintf("watch (%s)", w.Debounce)
28+
}
29+
fmt.Printf("%-8s %-15s %-38s %-10s %-25s\n", w.ID, mode, w.Folder, status, w.LastRun)
2430
}
2531
return nil
2632
},

cmd/start.go

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

33
import (
44
"fmt"
5+
"path/filepath"
56

67
"github.com/spf13/cobra"
78
"github.com/itzcodex24/git-guardian/internal/state"
@@ -19,9 +20,15 @@ var startCmd = &cobra.Command{
1920
Short: "Start a watcher for a linked folder (watch mode or interval mode)",
2021
RunE: func(cmd *cobra.Command, args []string) error {
2122
folder := args[0]
23+
absPath, err := filepath.Abs(folder)
24+
if err != nil {
25+
return fmt.Errorf("failed to resolve folder path: %w", err)
26+
}
27+
absPath = filepath.Clean(absPath)
28+
2229
list := state.Get()
2330
for i := range list {
24-
if list[i].Folder == folder {
31+
if list[i].Folder == absPath {
2532
if startWatch {
2633
list[i].Mode = "watch"
2734
list[i].Debounce = startDebounce
@@ -33,11 +40,11 @@ var startCmd = &cobra.Command{
3340
}
3441
list[i].Paused = false
3542
state.Update(list)
36-
fmt.Println("Watcher activated for:", folder)
43+
fmt.Println("Watcher activated for:", absPath)
3744
return nil
3845
}
3946
}
40-
return fmt.Errorf("folder not linked: %s. Use `guardian link` first", folder)
47+
return fmt.Errorf("folder not linked: %s. Use `guardian link` first", absPath)
4148
},
4249
}
4350

internal/git/git.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
func hasChanges(folder string) (bool, error) {
1212
cmd := exec.Command("git", "-C", folder, "status", "--porcelain")
13-
out, err := cmd.Output()
13+
out, err := cmd.CombinedOutput()
1414
if err != nil {
15-
return false, err
15+
return false, fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
1616
}
1717
return len(bytes.TrimSpace(out)) > 0, nil
1818
}
@@ -39,6 +39,5 @@ func CommitAndPush(folder string) error {
3939
return fmt.Errorf("git push failed: %v: %s", err, strings.TrimSpace(string(out)))
4040
}
4141

42-
fmt.Println("[guardian] backup pushed at", time.Now().Format(time.RFC3339))
4342
return nil
4443
}

internal/state/state.go

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

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"sync"
@@ -69,3 +70,35 @@ func Append(w WatcherState) {
6970
mu.Unlock()
7071
saveToDisk()
7172
}
73+
74+
func AddFolder(folder string) error {
75+
loadFromDisk()
76+
77+
// Check if folder is already linked
78+
for _, w := range watchers {
79+
if w.Folder == folder {
80+
return fmt.Errorf("folder already linked: %s", folder)
81+
}
82+
}
83+
84+
// Find the next available incremental ID
85+
maxID := 0
86+
for _, w := range watchers {
87+
var numID int
88+
if _, err := fmt.Sscanf(w.ID, "%d", &numID); err == nil {
89+
if numID > maxID {
90+
maxID = numID
91+
}
92+
}
93+
}
94+
95+
w := WatcherState{
96+
ID: fmt.Sprintf("%d", maxID+1),
97+
Folder: folder,
98+
Mode: "",
99+
Paused: true,
100+
}
101+
102+
Append(w)
103+
return nil
104+
}

0 commit comments

Comments
 (0)