Skip to content

Commit

Permalink
[fix] setup & config
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Apr 24, 2023
1 parent 0c438d0 commit ce4aaf6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions cmd/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var packCmd = &cobra.Command{
Long: L("Package the application into a single file"),
Run: func(cmd *cobra.Command, args []string) {

config.Init()
cfg := config.Conf
output, err := filepath.Abs(filepath.Join(cfg.Root, "dist"))
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ func Execute() {

// Boot 设定配置
func Boot() {
root := config.Conf.Root

config.Init()
root := config.Conf.Root
if appPath != "" {
r, err := filepath.Abs(appPath)
if err != nil {
Expand All @@ -156,7 +157,13 @@ func Boot() {

config.Conf = config.LoadFrom(filepath.Join(root, ".env"))

if share.BUILDIN {
os.Setenv("YAO_APP_SOURCE", "::binary")
config.Conf.AppSource = "::binary"
}

if yazFile != "" {
os.Setenv("YAO_APP_SOURCE", yazFile)
config.Conf.AppSource = yazFile
}

Expand Down
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var runCmd = &cobra.Command{
}()

Boot()

cfg := config.Conf
cfg.Session.IsCLI = true
if len(args) < 1 {
Expand Down
14 changes: 7 additions & 7 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ var startCmd = &cobra.Command{
defer share.SessionStop()
defer plugin.KillAll()

// recive interrupt signal
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

Boot()

// Setup
if setup.Check() {
go setup.Start()
select {
case <-setup.Done:
setup.Stop()
Boot()
break
case <-setup.Canceled:
os.Exit(1)
break
}
}

// recive interrupt signal
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

// defer service.Stop(func() { fmt.Println(L("Service stopped")) })
Boot()

// force debug
if startDebug {
config.Development()
Expand Down
17 changes: 10 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ var LogOutput *os.File // 日志文件
// DSLExtensions the dsl file Extensions
var DSLExtensions = []string{"*.yao", "*.json", "*.jsonc"}

func init() {
// Init setting
func Init() {

filename, _ := filepath.Abs(filepath.Join(".", ".env"))
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
Conf = Load()
if Conf.Mode == "production" {
Production()
} else if Conf.Mode == "development" {
Development()
}
return
}

Expand Down Expand Up @@ -88,19 +95,14 @@ func Load() Config {
if !filepath.IsAbs(cfg.DataRoot) {
cfg.DataRoot, _ = filepath.Abs(cfg.DataRoot)
}

if _, err := os.Stat(cfg.DataRoot); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(cfg.DataRoot, os.ModePerm); err != nil {
exception.New("Can't create data root %s", 500, err.Error()).Throw()
}
}
}

return cfg
}

// Production 设定为生产环境
func Production() {
os.Setenv("YAO_MODE", "production")
Conf.Mode = "production"
log.SetLevel(log.InfoLevel)
log.SetFormatter(log.TEXT)
Expand All @@ -113,6 +115,7 @@ func Production() {

// Development 设定为开发环境
func Development() {
os.Setenv("YAO_MODE", "development")
Conf.Mode = "development"
log.SetLevel(log.TraceLevel)
log.SetFormatter(log.TEXT)
Expand Down
2 changes: 2 additions & 0 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func loadApp(root string) error {
}

application.Load(app)
config.Init() // Reset Config
data.RemoveApp()

} else if strings.HasSuffix(root, ".yaz") {
Expand All @@ -260,6 +261,7 @@ func loadApp(root string) error {
return err
}
application.Load(app)
config.Init() // Reset Config

} else {
app, err = application.OpenFromDisk(root) // Load app from Disk
Expand Down
8 changes: 2 additions & 6 deletions setup/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ func Check() bool {
root := appRoot()
envfile := filepath.Join(root, ".env")
if _, err := os.Stat(envfile); err != nil && os.IsNotExist(err) {
cfg, err := getConfig()
if err != nil || !hasInstalled(cfg) {
return true
}
return true
}

return false
Expand All @@ -37,8 +34,7 @@ func Check() bool {
func appSourceExists() bool {

appsource := appSource()

if strings.HasSuffix(appsource, ".pkg") || strings.HasPrefix(appsource, "bin:") {
if strings.HasSuffix(appsource, ".yaz") || strings.HasPrefix(appsource, "::binary") {
return true
}

Expand Down
10 changes: 6 additions & 4 deletions setup/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ func makeLog(root string) error {

func makeDirs(root string) error {

err := os.MkdirAll(filepath.Join(root, "public"), os.ModePerm)
if err != nil && !os.IsExist(err) {
return err
if !appSourceExists() {
err := os.MkdirAll(filepath.Join(root, "public"), os.ModePerm)
if err != nil && !os.IsExist(err) {
return err
}
}

err = os.MkdirAll(filepath.Join(root, "logs"), os.ModePerm)
err := os.MkdirAll(filepath.Join(root, "logs"), os.ModePerm)
if err != nil && !os.IsExist(err) {
return err
}
Expand Down

0 comments on commit ce4aaf6

Please sign in to comment.