-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
181 lines (162 loc) · 4.8 KB
/
Copy pathmain.go
File metadata and controls
181 lines (162 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/LyleMi/AgentMeter/internal/app"
"github.com/LyleMi/AgentMeter/internal/cli"
"github.com/LyleMi/AgentMeter/internal/startup"
"github.com/LyleMi/AgentMeter/internal/tui"
)
type runtimeConfig struct {
uiMode string
httpAddr string
staticDir string
start bool
skipBrowser bool
forceBuild bool
}
func main() {
if exitCode, ok := runCLICommand(os.Args[1:]); ok {
os.Exit(exitCode)
}
config := parseRuntimeConfig(os.Args[1:])
service := newStartedApp()
runConfiguredUI(config, service)
}
func runCLICommand(args []string) (int, bool) {
if len(args) == 0 || !cli.IsCommand(args[0]) {
return 0, false
}
return cli.Run(args, os.Stdout, os.Stderr), true
}
func parseRuntimeConfig(args []string) runtimeConfig {
config := defaultRuntimeConfig()
flag.StringVar(&config.uiMode, "ui", config.uiMode, "UI mode: web or tui")
flag.StringVar(&config.httpAddr, "http", config.httpAddr, "HTTP listen address, for example 127.0.0.1:34115")
flag.StringVar(&config.staticDir, "static", config.staticDir, "directory containing the built frontend assets")
flag.BoolVar(&config.start, "start", false, "install/build frontend assets before starting web mode and open the browser")
flag.BoolVar(&config.skipBrowser, "skip-browser", false, "with -start, do not open the browser")
flag.BoolVar(&config.forceBuild, "force-build", false, "with -start, rebuild the frontend even when built assets look current")
configureRuntimeFlagUsage()
flag.CommandLine.Parse(normalizeCommandArgs(args))
requireNoRuntimeArgs()
config = normalizeRuntimeConfig(config)
if err := validateRuntimeConfig(config); err != nil {
log.Fatal(err)
}
config, err := prepareRuntimeConfig(config)
if err != nil {
log.Fatal(err)
}
return config
}
func defaultRuntimeConfig() runtimeConfig {
return runtimeConfig{
uiMode: "web",
httpAddr: "127.0.0.1:34115",
staticDir: "frontend/dist",
}
}
func configureRuntimeFlagUsage() {
flag.Usage = func() {
cli.PrintUsage(os.Stderr)
fmt.Fprintln(os.Stderr, "\nFlags:")
flag.PrintDefaults()
}
}
func requireNoRuntimeArgs() {
if flag.NArg() > 0 {
fmt.Fprintf(os.Stderr, "unknown command or argument %q\n\n", flag.Arg(0))
cli.PrintUsage(os.Stderr)
os.Exit(cli.ExitUsage)
}
}
func normalizeRuntimeConfig(config runtimeConfig) runtimeConfig {
config.uiMode = strings.ToLower(strings.TrimSpace(config.uiMode))
if config.uiMode == "" {
config.uiMode = "web"
}
if strings.HasPrefix(config.httpAddr, ":") {
config.httpAddr = "127.0.0.1" + config.httpAddr
}
return config
}
func validateRuntimeConfig(config runtimeConfig) error {
if (config.skipBrowser || config.forceBuild) && !config.start {
return errors.New("-skip-browser and -force-build require -start")
}
if config.start && config.uiMode != "web" {
return errors.New("-start can only be used with -ui web")
}
return nil
}
func prepareRuntimeConfig(config runtimeConfig) (runtimeConfig, error) {
if !config.start {
return config, nil
}
preparedStaticDir, err := startup.PrepareWebAssets(config.staticDir, config.forceBuild)
if err != nil {
return runtimeConfig{}, fmt.Errorf("prepare frontend: %w", err)
}
config.staticDir = preparedStaticDir
return config, nil
}
func newStartedApp() *app.App {
service, err := app.New()
if err != nil {
log.Fatalf("create app: %v", err)
}
if err := service.Startup(context.Background()); err != nil {
log.Fatalf("startup: %v", err)
}
return service
}
func runConfiguredUI(config runtimeConfig, service *app.App) {
switch config.uiMode {
case "web":
if config.start && !config.skipBrowser {
startup.OpenBrowserAfterDelay("http://"+config.httpAddr, 2*time.Second)
}
startWeb(service, config.httpAddr, config.staticDir)
case "tui":
if err := tui.Run(context.Background(), service); err != nil {
log.Fatalf("tui: %v", err)
}
default:
log.Fatalf("unknown -ui mode %q; expected web or tui", config.uiMode)
}
}
func normalizeCommandArgs(args []string) []string {
if len(args) == 0 {
return args
}
switch strings.ToLower(strings.TrimSpace(args[0])) {
case "tui", "cli":
return prependArgs([]string{"-ui", "tui"}, args[1:])
case "web":
return prependArgs([]string{"-ui", "web"}, args[1:])
case "start":
return prependArgs([]string{"-start"}, args[1:])
default:
return args
}
}
func prependArgs(prefix, rest []string) []string {
normalized := make([]string, 0, len(prefix)+len(rest))
normalized = append(normalized, prefix...)
normalized = append(normalized, rest...)
return normalized
}
func startWeb(service *app.App, httpAddr, staticDir string) {
mux := http.NewServeMux()
app.RegisterHTTPHandlers(mux, service, os.DirFS(staticDir))
fmt.Fprintf(os.Stdout, "AgentMeter listening on http://%s\n", httpAddr)
log.Fatal(http.ListenAndServe(httpAddr, mux))
}