@@ -12,10 +12,23 @@ import (
1212
1313var 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}
0 commit comments