main.go (2989B)
1 package main 2 3 import ( 4 "embed" 5 "errors" 6 "flag" 7 "fmt" 8 "os" 9 "runtime" 10 11 "github.com/fatih/color" 12 13 "fes/modules/config" 14 "fes/modules/doc" 15 "fes/modules/new" 16 "fes/modules/server" 17 "fes/modules/ui" 18 "fes/modules/version" 19 ) 20 21 //go:embed lib/* 22 var lib embed.FS 23 24 //go:embed index.html 25 var documentation string 26 27 func init() { 28 config.Port = flag.Int("p", 3000, "Set the server port") 29 config.Color = flag.Bool("no-color", false, "Disable color output") 30 config.Static = flag.Bool("static", false, "Render and save all pages") 31 config.Docker = flag.Bool("docker", false, "Create a docker project") 32 config.Lib = lib 33 config.Doc = documentation 34 config.Verbose = flag.Bool("verbose", false, "Enable verbose logging") 35 } 36 37 func main() { 38 var m runtime.MemStats 39 40 flag.Usage = func() { 41 fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <command> <project_dir>\n", os.Args[0]) 42 fmt.Fprintln(flag.CommandLine.Output(), "Commands:") 43 fmt.Fprintln(flag.CommandLine.Output(), " new <project_dir> Create a new project") 44 fmt.Fprintln(flag.CommandLine.Output(), " doc Open documentation") 45 fmt.Fprintln(flag.CommandLine.Output(), " run <project_dir> Start the server") 46 fmt.Fprintln(flag.CommandLine.Output(), "Options:") 47 flag.PrintDefaults() 48 fmt.Fprintln(flag.CommandLine.Output(), "For bug reports, contact a developer and describe the issue. Provide the output of the `-V1` flag.") 49 } 50 51 showVersion := flag.Bool("version", false, "Show version and exit") 52 showFullVersion := flag.Bool("V1", false, "Show extended version information and exit") 53 54 flag.Parse() 55 56 if *showVersion { 57 version.Version() 58 } 59 60 if *showFullVersion { 61 version.FullVersion() 62 } 63 64 if *config.Color { 65 color.NoColor = true 66 } 67 68 args := flag.Args() 69 if len(args) < 1 { 70 flag.Usage() 71 os.Exit(1) 72 } 73 74 cmd := args[0] 75 var dir string 76 if cmd == "new" || cmd == "run" { 77 if len(args) < 2 { 78 fmt.Fprintf(os.Stderr, "Error: %s requires <project_dir>\n", cmd) 79 flag.Usage() 80 os.Exit(1) 81 } 82 dir = args[1] 83 } 84 85 switch cmd { 86 case "new": 87 if err := new.Project(dir); err != nil { 88 fmt.Fprintln(os.Stderr, "Error:", err) 89 os.Exit(1) 90 } 91 case "doc": 92 if err := doc.Open(); err != nil { 93 fmt.Fprintln(os.Stderr, "Error:", err) 94 os.Exit(1) 95 } 96 case "run": 97 if *config.Port == 3000 { 98 ui.WARNING("Using default port, this may lead to conflicts with other services") 99 } 100 ui.Log("Fes is starting") 101 ui.Log("Fes version=%s, commit=%s, just started", version.VERSION, version.GetCommit()) 102 103 runtime.ReadMemStats(&m) 104 ui.Log("FRE memory usage when created %v Mb", m.TotalAlloc/1024/1024) 105 106 if err := server.Start(dir); err != nil { 107 if errors.Is(err, os.ErrNotExist) { 108 fmt.Fprintf(os.Stderr, "%s does not exist\n", dir) 109 fmt.Fprintf(os.Stderr, "Try: fes new %s\n", dir) 110 os.Exit(1) 111 } else { 112 fmt.Fprintln(os.Stderr, "Error:", err) 113 os.Exit(1) 114 } 115 } 116 default: 117 fmt.Fprintf(os.Stderr, "Unknown command: %s\n", cmd) 118 flag.Usage() 119 os.Exit(1) 120 } 121 }