@@ -2,6 +2,7 @@ package commands
22
33import (
44 "fmt"
5+ "strings"
56
67 "gnit/config"
78 "gnit/filesystem"
@@ -29,14 +30,105 @@ func (p *Pull) Execute(filename string) error {
2930 return fmt .Errorf ("failed to query file: %w" , err )
3031 }
3132
32- if len (content ) == 0 {
33- return fmt .Errorf ("file '%s' not found or empty" , filename )
34- }
35-
3633 if err := filesystem .WriteFile (filename , content ); err != nil {
3734 return err
3835 }
3936
4037 fmt .Printf ("File '%s' fetched successfully (%d bytes)\n " , filename , len (content ))
4138 return nil
4239}
40+
41+ func (p * Pull ) ExecuteAll () error {
42+ fmt .Println ("Fetching list of files..." )
43+
44+ query := fmt .Sprintf ("%s.Repo.ListFiles()" , p .config .RealmPath )
45+ result , err := p .client .QueryRaw (query )
46+ if err != nil {
47+ return fmt .Errorf ("failed to list files: %w" , err )
48+ }
49+
50+ if len (result ) == 0 {
51+ fmt .Println ("No files found in repository" )
52+ return nil
53+ }
54+
55+ files , err := parseFileList (result )
56+ if err != nil {
57+ return fmt .Errorf ("failed to parse file list: %w" , err )
58+ }
59+
60+ if len (files ) == 0 {
61+ fmt .Println ("No files found in repository" )
62+ return nil
63+ }
64+
65+ fmt .Printf ("Found %d file(s), pulling all...\n " , len (files ))
66+
67+ for _ , filename := range files {
68+ if err := p .Execute (filename ); err != nil {
69+ return fmt .Errorf ("failed to pull '%s': %w" , filename , err )
70+ }
71+ }
72+
73+ fmt .Printf ("\n Successfully pulled %d file(s)\n " , len (files ))
74+ return nil
75+ }
76+
77+ func parseFileList (data string ) ([]string , error ) {
78+ str := strings .TrimSpace (data )
79+
80+ str = strings .TrimPrefix (str , "data: " )
81+
82+ sliceStart := strings .Index (str , "slice[" )
83+ if sliceStart == - 1 {
84+ return []string {}, fmt .Errorf ("invalid format: missing 'slice['" )
85+ }
86+
87+ sliceStart += len ("slice[" )
88+ sliceEnd := strings .LastIndex (str , "]" )
89+ if sliceEnd == - 1 || sliceEnd <= sliceStart {
90+ return []string {}, fmt .Errorf ("invalid format: missing closing ']'" )
91+ }
92+
93+ content := str [sliceStart :sliceEnd ]
94+ if strings .TrimSpace (content ) == "" {
95+ return []string {}, nil
96+ }
97+
98+ var files []string
99+ var currentFile strings.Builder
100+ inQuotes := false
101+ escaped := false
102+
103+ for i := 0 ; i < len (content ); i ++ {
104+ ch := content [i ]
105+
106+ if escaped {
107+ currentFile .WriteByte (ch )
108+ escaped = false
109+ continue
110+ }
111+
112+ if ch == '\\' {
113+ escaped = true
114+ continue
115+ }
116+
117+ if ch == '"' {
118+ if inQuotes {
119+ files = append (files , currentFile .String ())
120+ currentFile .Reset ()
121+ inQuotes = false
122+ } else {
123+ inQuotes = true
124+ }
125+ continue
126+ }
127+
128+ if inQuotes {
129+ currentFile .WriteByte (ch )
130+ }
131+ }
132+
133+ return files , nil
134+ }
0 commit comments