Files
go-admin/cmd/app/server.go
T
zhangwenjian 6d06241e0d fix🐛: report failures from app and migrate -g file generation
Both commands discarded genFile's error, and genFile discarded the
errors from rendering its templates and from pkg.FileCreate. A file
that could not be written ended the process from inside FileCreate;
with core v2.11.0 it would have been reported as success instead.

genFile now returns every error, and both commands print it and exit
non-zero. In `app`, a missing router template no longer panics on a
nil template, and neither file is written unless both render.
2026-09-27 14:01:48 +08:00

103 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app
import (
"bytes"
"errors"
"fmt"
"github.com/go-admin-team/go-admin-core/v2/sdk/pkg"
"github.com/go-admin-team/go-admin-core/v2/sdk/pkg/utils"
"github.com/spf13/cobra"
"os"
"text/template"
)
var (
appName string
StartCmd = &cobra.Command{
Use: "app",
Short: "Create a new app",
Long: "Use when you need to create a new app",
Example: "go-admin app -n admin",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
)
func init() {
StartCmd.PersistentFlags().StringVarP(&appName, "name", "n", "", "Start server with provided configuration file")
}
func run() {
fmt.Println(`start init`)
//1. 读取配置
fmt.Println(`generate migration file`)
if err := genFile(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func genFile() error {
if appName == "" {
return errors.New("arg `name` invalid :name is empty")
}
path := "app/"
appPath := path + appName
err := utils.IsNotExistMkDir(appPath)
if err != nil {
return err
}
apiPath := appPath + "/apis/"
err = utils.IsNotExistMkDir(apiPath)
if err != nil {
return err
}
modelsPath := appPath + "/models/"
err = utils.IsNotExistMkDir(modelsPath)
if err != nil {
return err
}
routerPath := appPath + "/router/"
err = utils.IsNotExistMkDir(routerPath)
if err != nil {
return err
}
servicePath := appPath + "/service/"
err = utils.IsNotExistMkDir(servicePath)
if err != nil {
return err
}
dtoPath := appPath + "/service/dto/"
err = utils.IsNotExistMkDir(dtoPath)
if err != nil {
return err
}
t1, err := template.ParseFiles("template/cmd_api.template")
if err != nil {
return err
}
m := map[string]string{}
m["appName"] = appName
var b1 bytes.Buffer
if err = t1.Execute(&b1, m); err != nil {
return err
}
t2, err := template.ParseFiles("template/router.template")
if err != nil {
return err
}
var b2 bytes.Buffer
if err = t2.Execute(&b2, nil); err != nil {
return err
}
if err = pkg.FileCreate(b1, "./cmd/api/"+appName+".go"); err != nil {
return err
}
return pkg.FileCreate(b2, appPath+"/router/router.go")
}