diff --git a/cmd/input.go b/cmd/input_dialog.go similarity index 72% rename from cmd/input.go rename to cmd/input_dialog.go index dc580c2..fae871e 100644 --- a/cmd/input.go +++ b/cmd/input_dialog.go @@ -13,9 +13,14 @@ func SelectionInput() ([]string, string) { log.Fatalf("Please provide path to models.go") } - path := os.Args[1] + modelsPath := os.Args[1] - structNames := utils.GetStructList(path) + structNames := utils.GetNotImplementedStructs(modelsPath) + + if len(structNames) == 0 { + log.Println("No unimplemented models -> nothing to do") + os.Exit(0) + } var result []string @@ -30,7 +35,7 @@ func SelectionInput() ([]string, string) { form.AddFormItem(cb) } - form.AddButton("Confirm", func() { + form.AddButton("Generate", func() { for i, cb := range checkboxes { if cb.IsChecked() { result = append(result, structNames[i]) @@ -43,5 +48,5 @@ func SelectionInput() ([]string, string) { panic(err) } - return result, path + return result, modelsPath } diff --git a/main.go b/main.go index 36470cc..5cb794a 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ func main() { log.Fatalf("Failed to open file: %s", err) } structFields := utils.GetStructFields(file, structName) + _ = file.Close() generation.Generate(structName, structFields) } } diff --git a/utils/get_not_implemented_structs.go b/utils/get_not_implemented_structs.go new file mode 100644 index 0000000..b91b91f --- /dev/null +++ b/utils/get_not_implemented_structs.go @@ -0,0 +1,11 @@ +package utils + +func GetNotImplementedStructs(modelsFilePath string) []string { + var models []string + for _, model := range GetStructsList(modelsFilePath) { + if !IsEntityImplemented(model) { + models = append(models, model) + } + } + return models +} diff --git a/utils/get_struct_fields.go b/utils/get_struct_fields.go index 2e734fb..f5979d4 100644 --- a/utils/get_struct_fields.go +++ b/utils/get_struct_fields.go @@ -10,9 +10,7 @@ import ( func GetStructFields(file *os.File, structName string) []entities.Field { bracketsCount := 1 - - structFound := false - + var structFound bool var structFields []entities.Field scanner := bufio.NewScanner(file) diff --git a/utils/get_structs_list.go b/utils/get_structs_list.go index 73b702d..8d4eb48 100644 --- a/utils/get_structs_list.go +++ b/utils/get_structs_list.go @@ -7,8 +7,8 @@ import ( "strings" ) -func GetStructList(filePath string) []string { - file, err := os.Open(filePath) +func GetStructsList(modelsFilePath string) []string { + file, err := os.Open(modelsFilePath) defer file.Close() if err != nil { log.Fatalf("Failed to open a file: %s", err) diff --git a/utils/is_implemented.go b/utils/is_implemented.go new file mode 100644 index 0000000..b54c1cc --- /dev/null +++ b/utils/is_implemented.go @@ -0,0 +1,16 @@ +package utils + +import ( + "os" + "path/filepath" + "strings" +) + +func IsEntityImplemented(entityName string) bool { + entityDirectory := filepath.Join(FindFrontendPath(), strings.ToLower(entityName)) + if _, err := os.Stat(entityDirectory); os.IsNotExist(err) { + return false + } else { + return true + } +}