4
4
"bytes"
5
5
"context"
6
6
"encoding/json"
7
+ "errors"
8
+ "flag"
7
9
"fmt"
8
10
"io/ioutil"
9
11
"os"
@@ -29,8 +31,17 @@ func main() {
29
31
30
32
config := NewConfig ()
31
33
32
- crawlRepoBehaviorsAndSaveToJSON (config )
34
+ var crawl string
35
+ flag .StringVar (& crawl , "crawl" , "behavior" , "what should this crawler do? -crawl=src (source files), -crawl=behavior (test files)" )
36
+ flag .Parse ()
33
37
38
+ if crawl == "behavior" {
39
+ crawlRepoBehaviorsAndSaveToJSON (config )
40
+ } else if crawl == "src" {
41
+ if err := crawlRepoSourceCodeAndSaveToYaml (config ); err != nil {
42
+ fmt .Println (err .Error ())
43
+ }
44
+ }
34
45
}
35
46
36
47
func crawlRepoBehaviorsAndSaveToJSON (config Config ) {
@@ -81,6 +92,22 @@ func crawlRepoBehaviorsAndSaveToJSON(config Config) {
81
92
Save (result , config .TestCrawlerOutputMode , config .TestCrawlerOutputDir , config .TestCrawlerIndentJSON )
82
93
}
83
94
95
+ func crawlRepoSourceCodeAndSaveToYaml (config Config ) error {
96
+ ctx := context .Background ()
97
+ for _ , path := range config .BehaviorGenPaths {
98
+ systemMethods , err := crawlFolderForSystemMethods (path )
99
+ if err != nil {
100
+ return err
101
+ }
102
+
103
+ if err := makeYAMLFromSystemMethods (ctx , config , * systemMethods ); err != nil {
104
+ return fmt .Errorf ("error %w saving to yaml for system %s" , err , systemMethods .System )
105
+ }
106
+ }
107
+
108
+ return nil
109
+ }
110
+
84
111
// crawlSingleFileForMethods accepts path of single go file,
85
112
// and prints extracted methods out of it.
86
113
func crawlSingleFileForFunctions (ctx context.Context , path string ) ([]c.FunctionAnnotation , error ) {
@@ -118,29 +145,34 @@ func crawlFolderForSystemMethods(system string) (*c.SystemMethods, error) {
118
145
return & systemFunctions , nil
119
146
}
120
147
121
- // makeYAML will make yaml file from public methods.
122
- func makeYAML (ctx context.Context , filePath string ) error {
148
+ // makeYAMLFromSystemMethods will make yaml file from public methods.
149
+ func makeYAMLFromSystemMethods (ctx context.Context , config Config , systemMethods c. SystemMethods ) error {
123
150
124
- publicMethods , err := crawlSingleFileForFunctions (ctx , filePath )
125
- if err != nil {
126
- fmt .Print (err )
127
- os .Exit (1 )
151
+ if _ , err := os .Stat (config .BehaviorGenOutputDir ); errors .Is (err , os .ErrNotExist ) {
152
+ err2 := os .Mkdir (config .BehaviorGenOutputDir , os .ModePerm )
153
+ if err2 != nil {
154
+ return fmt .Errorf ("error create dir: %w" , err )
155
+ }
128
156
}
129
157
130
- for i := 0 ; i < len (publicMethods ); i ++ {
131
- publicMethods [i ].ID = i
158
+ yamlData , err := y .Marshal (& systemMethods )
159
+ if err != nil {
160
+ return fmt .Errorf ("error marhaling: %w" , err )
132
161
}
133
162
134
- yamlData , err := y .Marshal (& publicMethods )
163
+ filename := fmt .Sprintf ("%s/%s.yaml" , config .BehaviorGenOutputDir , systemMethods .System )
164
+ file , err := os .Create (filename )
135
165
if err != nil {
136
- fmt .Printf ( "Error while Marshaling. %v " , err )
166
+ return fmt .Errorf ( "error create file: %w " , err )
137
167
}
168
+ defer file .Close ()
169
+ fmt .Println ("File generated: " , filename )
138
170
139
- fileName := "test.yaml"
140
- err = ioutil .WriteFile (fileName , yamlData , 0644 )
171
+ err = ioutil .WriteFile (filename , yamlData , 0644 )
141
172
if err != nil {
142
- panic ( "Unable to write data into the file" )
173
+ return fmt . Errorf ( "error write to file: %w" , err )
143
174
}
175
+
144
176
return nil
145
177
}
146
178
0 commit comments