esr/cmd/esr/main.go
2024-10-09 14:22:28 -04:00

72 lines
1.3 KiB
Go

package main
import (
"esr/internal/config"
lib "esr/internal/esr"
"flag"
"fmt"
"os"
)
func initCommand() {
lib.InitProject()
}
func main() {
var (
configPath string
serve bool
build bool
watch bool
run bool
)
flag.Usage = func() {
lib.ShowHelpMessage()
}
flag.StringVar(&configPath, "config", ".esr.yml", "path to the config file")
flag.BoolVar(&serve, "serve", false, "serve")
flag.BoolVar(&build, "build", false, "build")
flag.BoolVar(&watch, "watch", false, "watch")
flag.BoolVar(&run, "run", false, "run")
flag.Parse()
args := flag.Args()
if len(os.Args) > 1 && os.Args[1] == "init" {
initCommand()
return
}
if len(args) < 1 {
fmt.Println("Error: no entrypoint specified")
os.Exit(1)
}
entryPoint := args[0]
if _, err := os.Stat(entryPoint); os.IsNotExist(err) {
fmt.Printf("Error: entrypoint '%s' does not exist\n", entryPoint)
os.Exit(1)
}
cfg, err := config.ReadConfig(configPath)
if err != nil {
fmt.Println("esr :: Failed to read config:", err)
cfg = config.GetDefaultConfig()
}
esr := lib.NewEsr(cfg, entryPoint)
if build || serve || run {
lib.ExecuteTask(esr, build, serve, run, watch)
} else if watch {
fmt.Println("--watch given, but no action specified (e.g. --build, --serve, --run)")
os.Exit(1)
}
}