mirror of
https://github.com/opbnq-q/nto-cli.git
synced 2025-12-06 16:20:34 +07:00
fix: paths, feat: many models, selection
This commit is contained in:
44
cmd/input.go
44
cmd/input.go
@@ -1,6 +1,12 @@
|
||||
package input
|
||||
package cmd
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"nto_cli/utils"
|
||||
"os"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func Input() (string, string) {
|
||||
fmt.Print("struct name, path to file (including struct): ")
|
||||
@@ -8,3 +14,37 @@ func Input() (string, string) {
|
||||
fmt.Scan(&structName, &path)
|
||||
return structName, path
|
||||
}
|
||||
|
||||
func SelectionInput() ([]string, string) {
|
||||
path := os.Args[1]
|
||||
|
||||
structNames := utils.GetStructList(path)
|
||||
|
||||
result := []string{}
|
||||
|
||||
app := tview.NewApplication()
|
||||
|
||||
form := tview.NewForm()
|
||||
checkboxes := []*tview.Checkbox{}
|
||||
|
||||
for _, name := range structNames {
|
||||
cb := tview.NewCheckbox().SetLabel(name)
|
||||
checkboxes = append(checkboxes, cb)
|
||||
form.AddFormItem(cb)
|
||||
}
|
||||
|
||||
form.AddButton("Confirm", func() {
|
||||
for i, cb := range checkboxes {
|
||||
if cb.IsChecked() {
|
||||
result = append(result, structNames[i])
|
||||
}
|
||||
}
|
||||
app.Stop()
|
||||
})
|
||||
|
||||
if err := app.SetRoot(form, true).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return result, path
|
||||
}
|
||||
|
||||
@@ -12,13 +12,39 @@ type Field struct {
|
||||
Medatada []Medatada
|
||||
}
|
||||
|
||||
var PRIMITIVE_TYPES = []string{"date", "number", "string", "multiple", "boolean"}
|
||||
var PRIMITIVE_TYPES = map[string]string{
|
||||
"date": "date",
|
||||
"number": "number",
|
||||
"string": "string",
|
||||
"multiple": "multiple",
|
||||
"boolean": "boolean",
|
||||
"bool": "boolean",
|
||||
"int": "number",
|
||||
"uint": "number",
|
||||
"float32": "number",
|
||||
"float64": "number",
|
||||
"int32": "number",
|
||||
"int64": "number",
|
||||
"uint32": "number",
|
||||
"uint64": "number",
|
||||
"int8": "number",
|
||||
"int16": "number",
|
||||
"uint8": "number",
|
||||
"uint16": "number",
|
||||
"byte": "number",
|
||||
"rune": "number",
|
||||
}
|
||||
|
||||
func (f *Field) GenerateType() string {
|
||||
result := " type: {\n"
|
||||
|
||||
if slices.Contains(PRIMITIVE_TYPES, strings.ToLower(f.Type)) {
|
||||
result += fmt.Sprintf(` primitive: "%s",`, strings.ToLower(f.Type))
|
||||
keys := make([]string, 0, len(PRIMITIVE_TYPES))
|
||||
for k := range PRIMITIVE_TYPES {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
if slices.Contains(keys, strings.ToLower(f.Type)) {
|
||||
result += fmt.Sprintf(` primitive: "%s",`, PRIMITIVE_TYPES[strings.ToLower(f.Type)])
|
||||
} else {
|
||||
var field string = "[]"
|
||||
for _, meta := range f.Medatada {
|
||||
|
||||
@@ -68,7 +68,7 @@ func LoadDependencies(fields []entities.Field) string {
|
||||
fieldName: field.Name,
|
||||
dependencyName: dependency,
|
||||
})
|
||||
result += fmt.Sprintf("import %sService from '../%s/%s.service.ts'\n", dependency, strings.ToUpper(dependency[:1]) + strings.ToLower(dependency[1:]), strings.ToLower(dependency))
|
||||
result += fmt.Sprintf("import %sService from '../%s/%s.service.ts'\n", dependency, strings.ToLower(dependency), strings.ToLower(dependency))
|
||||
result += fmt.Sprintf("const %sService = new %sService\n", strings.ToLower(dependency), strings.ToUpper(dependency[:1]) + strings.ToLower(dependency[1:]))
|
||||
}
|
||||
}
|
||||
|
||||
13
go.mod
13
go.mod
@@ -2,4 +2,15 @@ module nto_cli
|
||||
|
||||
go 1.23.6
|
||||
|
||||
require github.com/fatih/structtag v1.2.0 // indirect
|
||||
require (
|
||||
github.com/fatih/structtag v1.2.0 // indirect
|
||||
github.com/gdamore/encoding v1.0.0 // indirect
|
||||
github.com/gdamore/tcell/v2 v2.7.1 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/term v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
)
|
||||
|
||||
50
go.sum
50
go.sum
@@ -1,2 +1,52 @@
|
||||
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
||||
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
||||
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
|
||||
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
|
||||
github.com/gdamore/tcell/v2 v2.7.1 h1:TiCcmpWHiAU7F0rA2I3S2Y4mmLmO9KHxJ7E1QhYzQbc=
|
||||
github.com/gdamore/tcell/v2 v2.7.1/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57 h1:LmsF7Fk5jyEDhJk0fYIqdWNuTxSyid2W42A0L2YWjGE=
|
||||
github.com/rivo/tview v0.0.0-20241227133733-17b7edb88c57/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
10
main.go
10
main.go
@@ -1,20 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
input "nto_cli/cmd"
|
||||
"nto_cli/cmd"
|
||||
"nto_cli/generation"
|
||||
"nto_cli/utils"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
structName, path := input.Input()
|
||||
structNames, path := cmd.SelectionInput()
|
||||
|
||||
for _, structName := range structNames {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
structFields := utils.GetStructFields(file, structName)
|
||||
file.Close()
|
||||
generation.Generate(structName, structFields)
|
||||
}
|
||||
}
|
||||
|
||||
32
utils/get_structs_list.go
Normal file
32
utils/get_structs_list.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"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
|
||||
}
|
||||
@@ -19,18 +19,17 @@ func SplitStructField(field string) (*entities.Field, error) {
|
||||
endBacktip := -1
|
||||
var metadata []entities.Medatada
|
||||
if startBacktip > -1 {
|
||||
endBacktip = strings.Index(field[startBacktip + 1:], "`")
|
||||
endBacktip = strings.Index(field[startBacktip+1:], "`")
|
||||
if endBacktip > -1 {
|
||||
endBacktip += startBacktip + 1
|
||||
meta := field[startBacktip + 1 : endBacktip]
|
||||
meta := field[startBacktip+1 : endBacktip]
|
||||
tags, err := structtag.Parse(meta)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
uiTag, err := tags.Get("ui")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
uiTags := append([]string{uiTag.Name}, uiTag.Options...)
|
||||
for _, t := range uiTags {
|
||||
analyzed := entities.NewMetadata(t)
|
||||
@@ -39,6 +38,7 @@ func SplitStructField(field string) (*entities.Field, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
startBacktip = len(field)
|
||||
}
|
||||
@@ -46,7 +46,6 @@ func SplitStructField(field string) (*entities.Field, error) {
|
||||
|
||||
data := SplitBySingleSpace(field)
|
||||
|
||||
|
||||
name := data[0]
|
||||
dataType := data[1]
|
||||
return &entities.Field{
|
||||
|
||||
Reference in New Issue
Block a user