44 "bytes"
55 "context"
66 "encoding/json"
7+ "errors"
8+ "flag"
79 "fmt"
810 "io/ioutil"
911 "os"
@@ -29,8 +31,17 @@ func main() {
2931
3032 config := NewConfig ()
3133
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 ()
3337
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+ }
3445}
3546
3647func crawlRepoBehaviorsAndSaveToJSON (config Config ) {
@@ -81,6 +92,22 @@ func crawlRepoBehaviorsAndSaveToJSON(config Config) {
8192 Save (result , config .TestCrawlerOutputMode , config .TestCrawlerOutputDir , config .TestCrawlerIndentJSON )
8293}
8394
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+
84111// crawlSingleFileForMethods accepts path of single go file,
85112// and prints extracted methods out of it.
86113func crawlSingleFileForFunctions (ctx context.Context , path string ) ([]c.FunctionAnnotation , error ) {
@@ -118,29 +145,34 @@ func crawlFolderForSystemMethods(system string) (*c.SystemMethods, error) {
118145 return & systemFunctions , nil
119146}
120147
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 {
123150
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+ }
128156 }
129157
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 )
132161 }
133162
134- yamlData , err := y .Marshal (& publicMethods )
163+ filename := fmt .Sprintf ("%s/%s.yaml" , config .BehaviorGenOutputDir , systemMethods .System )
164+ file , err := os .Create (filename )
135165 if err != nil {
136- fmt .Printf ( "Error while Marshaling. %v " , err )
166+ return fmt .Errorf ( "error create file: %w " , err )
137167 }
168+ defer file .Close ()
169+ fmt .Println ("File generated: " , filename )
138170
139- fileName := "test.yaml"
140- err = ioutil .WriteFile (fileName , yamlData , 0644 )
171+ err = ioutil .WriteFile (filename , yamlData , 0644 )
141172 if err != nil {
142- panic ( "Unable to write data into the file" )
173+ return fmt . Errorf ( "error write to file: %w" , err )
143174 }
175+
144176 return nil
145177}
146178
0 commit comments