Refactor startup logic to improve directory checks and enhance welcome message display

This commit is contained in:
Max 2024-11-29 17:36:45 +08:00
parent 79f77618d3
commit d7693a0487
4 changed files with 67 additions and 75 deletions

View file

@ -49,13 +49,27 @@ var startCmd = &cobra.Command{
Boot() Boot()
// Setup // Setup
isNewApp := false isnew := false
if !setup.SourceExists() { if setup.IsEmptyDir(config.Conf.Root) {
// In Yao App
if setup.InYaoApp(config.Conf.Root) {
fmt.Println(color.RedString(L("Please run the command in the root directory of project")))
os.Exit(1)
}
// Install the init app
if err := install(); err != nil { if err := install(); err != nil {
fmt.Println(color.RedString(L("Install: %s"), err.Error())) fmt.Println(color.RedString(L("Install: %s"), err.Error()))
os.Exit(1) os.Exit(1)
} }
isNewApp = true isnew = true
}
// Is Yao App
if !setup.IsYaoApp(config.Conf.Root) {
fmt.Println(color.RedString(L("yao.app not found")))
os.Exit(1)
} }
// force debug // force debug
@ -105,17 +119,6 @@ var startCmd = &cobra.Command{
urls, _ = setup.URLs(config.Conf) urls, _ = setup.URLs(config.Conf)
} }
fmt.Println(color.WhiteString(L("Runtime")), color.GreenString(" %s", runtimeMode))
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
for _, url := range urls {
fmt.Println(color.CyanString("\n%s", url))
fmt.Println(color.WhiteString("--------------------------"))
fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" %s", url))
fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
}
// print the messages under the development mode // print the messages under the development mode
if mode == "development" { if mode == "development" {
@ -144,10 +147,22 @@ var startCmd = &cobra.Command{
printStores(false) printStores(false)
printStudio(false, host) printStudio(false, host)
// Print welcome message for the new application
if isNewApp {
printWelcome()
} }
fmt.Println(color.WhiteString(L("Runtime")), color.GreenString(" %s", runtimeMode))
fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot))
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))
for _, url := range urls {
fmt.Println(color.CyanString("\n%s", url))
fmt.Println(color.WhiteString("--------------------------"))
fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" %s", url))
fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
}
// Print welcome message for the new application
if isnew {
printWelcome()
} }
// Start Tasks // Start Tasks
@ -195,7 +210,7 @@ var startCmd = &cobra.Command{
switch v { switch v {
case http.READY: case http.READY:
fmt.Println(color.GreenString(L("✨Server is up and running..."))) fmt.Println(color.GreenString(L("✨Server is up and running...")))
fmt.Println(color.BlackString("✨Ctrl+C to stop")) fmt.Println(color.GreenString("✨Ctrl+C to stop"))
break break
case http.CLOSED: case http.CLOSED:
@ -256,11 +271,11 @@ func adminRoot() (string, int) {
} }
func printWelcome() { func printWelcome() {
fmt.Println(color.BlueString("\n---------------------------------")) fmt.Println(color.CyanString("\n---------------------------------"))
fmt.Println(color.BlueString(L("🎉 Welcome to Yao 🎉 "))) fmt.Println(color.CyanString(L("🎉 Welcome to Yao 🎉 ")))
fmt.Println(color.BlueString("---------------------------------")) fmt.Println(color.CyanString("---------------------------------"))
fmt.Println(color.WhiteString("📚 Documentation:"), color.BlueString("https://yaoapps.com/docs")) fmt.Println(color.WhiteString("📚 Documentation:"), color.CyanString("https://yaoapps.com/docs"))
fmt.Println(color.WhiteString("💬 Build App via Chat:"), color.BlueString("https://moapi.ai")) fmt.Println(color.WhiteString("💬 Build App via Chat:"), color.CyanString("https://moapi.ai"))
fmt.Println("") fmt.Println("")
} }

View file

@ -150,17 +150,19 @@ func OpenLog() {
logfile, err := filepath.Abs(Conf.Log) logfile, err := filepath.Abs(Conf.Log)
if err != nil { if err != nil {
log.With(log.F{"file": logfile}).Error(err.Error())
return return
} }
logpath := filepath.Dir(logfile) logpath := filepath.Dir(logfile)
if _, err := os.Stat(logpath); os.IsNotExist(err) {
if err := os.MkdirAll(logpath, os.ModePerm); err != nil { // Check if the log path exists
log.With(log.F{"file": logfile}).Error(err.Error()) if _, err := os.Stat(logpath); errors.Is(err, os.ErrNotExist) {
LogOutput, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
log.SetOutput(LogOutput)
gin.DefaultWriter = io.MultiWriter(LogOutput)
return return
} }
}
LogOutput = &lumberjack.Logger{ LogOutput = &lumberjack.Logger{
Filename: logfile, Filename: logfile,
MaxSize: Conf.LogMaxSize, // megabytes MaxSize: Conf.LogMaxSize, // megabytes

View file

@ -1,41 +1,44 @@
package setup package setup
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
) )
// SourceExists check if the app source exists // InYaoApp Check if the current directory is a yao app
func SourceExists() bool { func InYaoApp(root string) bool {
return appSourceExists() // Check current directory and parent directories
for root != "/" {
if IsYaoApp(root) {
return true
}
root = filepath.Dir(root)
} }
func appSourceExists() bool {
root := appRoot()
if isEmptyDir(root) {
return false return false
} }
// check app.yao/app.json/app.jsonc // IsYaoApp Check if the directory is a yao app
func IsYaoApp(root string) bool {
appfiles := []string{"app.yao", "app.json", "app.jsonc"} appfiles := []string{"app.yao", "app.json", "app.jsonc"}
exist := false yaoapp := false
for _, appfile := range appfiles { for _, appfile := range appfiles {
appfile = filepath.Join(root, appfile) appfile = filepath.Join(root, appfile)
if _, err := os.Stat(appfile); err == nil { if _, err := os.Stat(appfile); err == nil {
exist = true yaoapp = true
break break
} }
} }
return yaoapp
return exist
} }
func isEmptyDir(dir string) bool { // IsEmptyDir Check if the directory is empty
func IsEmptyDir(dir string) bool {
f, err := os.Open(dir) f, err := os.Open(dir)
if err != nil { if err != nil {
fmt.Println("Can't open the directory: ", err)
return true return true
} }
defer f.Close() defer f.Close()

View file

@ -1,15 +1,12 @@
package setup package setup
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/google/uuid"
"github.com/yaoapp/gou/model" "github.com/yaoapp/gou/model"
"github.com/yaoapp/gou/process" "github.com/yaoapp/gou/process"
v8 "github.com/yaoapp/gou/runtime/v8"
"github.com/yaoapp/kun/log" "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/data" "github.com/yaoapp/yao/data"
@ -48,7 +45,7 @@ func Initialize(root string, cfg config.Config) error {
func makeInit(root string) error { func makeInit(root string) error {
if appSourceExists() { if !IsEmptyDir(root) {
return nil return nil
} }
@ -106,31 +103,6 @@ func makeSetup(cfg config.Config) error {
if app.Setting != nil && app.Setting.Setup != "" { if app.Setting != nil && app.Setting.Setup != "" {
if strings.HasPrefix(app.Setting.Setup, "studio.") {
names := strings.Split(app.Setting.Setup, ".")
if len(names) < 3 {
return fmt.Errorf("setup studio script %s error", app.Setting.Setup)
}
service := strings.Join(names[1:len(names)-1], ".")
method := names[len(names)-1]
script, err := v8.SelectRoot(service)
if err != nil {
return err
}
sid := uuid.NewString()
ctx, err := script.NewContext(fmt.Sprintf("%v", sid), nil)
if err != nil {
return err
}
defer ctx.Close()
_, err = ctx.Call(method)
return err
}
p, err := process.Of(app.Setting.Setup, cfg) p, err := process.Of(app.Setting.Setup, cfg)
if err != nil { if err != nil {
return err return err