gracefull shutdown for unresolvable errors

This commit is contained in:
2025-03-07 21:40:53 +07:00
parent 2fd8b20b77
commit e6f21d1bb2
19 changed files with 181 additions and 133 deletions

View File

@@ -2,14 +2,14 @@ package utils
import "strings"
func ContainsMany(str string, substrs... string) bool {
count := 0
for _, substr := range substrs {
func ContainsMany(str string, substrings ...string) bool {
var matches int
for _, substr := range substrings {
if strings.Contains(str, substr) {
count++;
matches++
} else {
return false;
return false
}
}
return count == len(substrs)
}
return matches == len(substrings)
}

View File

@@ -1,45 +1,45 @@
package utils
import (
"errors"
"os"
"path/filepath"
"log"
"os"
"path/filepath"
)
func FindFrontendPath() string {
currentPath, err := os.Getwd()
if err != nil {
panic(err)
}
currentPath, err := os.Getwd()
if err != nil {
panic(err)
}
var dirs []string
for currentPath != filepath.VolumeName(currentPath)+string(os.PathSeparator) {
dir, file := filepath.Split(currentPath)
if file != "" {
dirs = append([]string{file}, dirs...)
}
currentPath = filepath.Clean(dir)
}
var dirs []string
for currentPath != filepath.VolumeName(currentPath)+string(os.PathSeparator) {
dir, file := filepath.Split(currentPath)
if file != "" {
dirs = append([]string{file}, dirs...)
}
currentPath = filepath.Clean(dir)
}
if len(dirs) < 2 || filepath.Join(dirs[len(dirs)-2], dirs[len(dirs)-1]) != filepath.Join("frontend", "src") {
panic(errors.New("You're not in frontend/src"))
}
if len(dirs) < 2 || filepath.Join(dirs[len(dirs)-2], dirs[len(dirs)-1]) != filepath.Join("frontend", "src") {
log.Fatalf("Run this program in frontend/ directory")
}
var path string
for i, dir := range dirs {
if dir == "frontend" {
break
}
if i > 0 {
path = filepath.Join(path, dir)
} else {
path = dir
}
}
var path string
for i, dir := range dirs {
if dir == "frontend" {
break
}
if i > 0 {
path = filepath.Join(path, dir)
} else {
path = dir
}
}
if filepath.VolumeName(path) == "" {
path = filepath.Join(string(os.PathSeparator), path)
}
if filepath.VolumeName(path) == "" {
path = filepath.Join(string(os.PathSeparator), path)
}
return path
}
return path
}

View File

@@ -2,6 +2,7 @@ package utils
import (
"bufio"
"log"
"nto_cli/entities"
"os"
"strings"
@@ -12,7 +13,7 @@ func GetStructFields(file *os.File, structName string) []entities.Field {
structFound := false
structFields := []entities.Field{}
var structFields []entities.Field
scanner := bufio.NewScanner(file)
for i := 1; scanner.Scan() && bracketsCount > 0; i++ {
@@ -35,7 +36,7 @@ func GetStructFields(file *os.File, structName string) []entities.Field {
}
if err := scanner.Err(); err != nil {
panic(err)
log.Fatalf("Failed to read file with scanner: %s", err)
}
return structFields
}

View File

@@ -1,32 +1,36 @@
package utils
import (
"bufio"
"os"
"strings"
"bufio"
"log"
"os"
"strings"
)
func GetStructList(filePath string) ([]string) {
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()
var structNames []string
s := bufio.NewScanner(file)
for s.Scan() {
line := s.Text()
if strings.Contains(line, "type ") && strings.Contains(line, " struct") {
start := strings.Index(line, "type ") + 5
end := strings.Index(line, " struct")
name := strings.TrimSpace(line[start:end])
if name != "" {
structNames = append(structNames, name)
}
}
}
if err := s.Err(); err != nil {
panic(err)
}
return structNames
}
func GetStructList(filePath string) []string {
file, err := os.Open(filePath)
defer file.Close()
if err != nil {
log.Fatalf("Failed to open a file: %s", err)
}
var structNames []string
s := bufio.NewScanner(file)
for s.Scan() {
line := s.Text()
if strings.Contains(line, "type ") && strings.Contains(line, " struct") {
start := strings.Index(line, "type ") + 5
end := strings.Index(line, " struct")
name := strings.TrimSpace(line[start:end])
if name != "" {
structNames = append(structNames, name)
}
}
}
if s.Err() != nil {
log.Fatalf("")
}
return structNames
}

View File

@@ -2,6 +2,7 @@ package utils
import (
"errors"
"log"
"nto_cli/entities"
"strings"
@@ -13,19 +14,21 @@ func SplitStructField(field string) (*entities.Field, error) {
return nil, nil
}
if len(strings.TrimSpace(field)) < 2 {
return nil, errors.New("End")
return nil, errors.New("unexpected end of struct field")
}
startBacktip := strings.Index(field, "`")
endBacktip := -1
var metadata []entities.Medatada
if startBacktip > -1 {
endBacktip = strings.Index(field[startBacktip+1:], "`")
if endBacktip > -1 {
endBacktip += startBacktip + 1
meta := field[startBacktip+1 : endBacktip]
startBacktick := strings.Index(field, "`")
endBacktick := -1
var metadata []entities.Metadata
if startBacktick > -1 {
endBacktick = strings.Index(field[startBacktick+1:], "`")
if endBacktick > -1 {
endBacktick += startBacktick + 1
meta := field[startBacktick+1 : endBacktick]
tags, err := structtag.Parse(meta)
if err != nil {
panic(err)
log.Fatalf("failed to parse struct tag: %s", err)
}
uiTag, err := tags.Get("ui")
@@ -40,9 +43,10 @@ func SplitStructField(field string) (*entities.Field, error) {
}
}
} else {
startBacktip = len(field)
startBacktick = len(field)
}
field = strings.TrimSpace(field[:startBacktip])
field = strings.TrimSpace(field[:startBacktick])
data := SplitBySingleSpace(field)