2024-10-12 21:45:00 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-10-12 22:15:44 +03:00
|
|
|
"log/slog"
|
2024-10-12 21:45:00 +03:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.a71.su/Andrew71/pye/cmd/serve"
|
|
|
|
"git.a71.su/Andrew71/pye/cmd/verify"
|
|
|
|
"git.a71.su/Andrew71/pye/config"
|
2024-10-13 11:25:00 +03:00
|
|
|
"git.a71.su/Andrew71/pye/logging"
|
2024-10-12 21:45:00 +03:00
|
|
|
)
|
|
|
|
|
2024-10-13 11:25:00 +03:00
|
|
|
func Run() {
|
2024-10-12 21:45:00 +03:00
|
|
|
|
|
|
|
serveCmd := flag.NewFlagSet("serve", flag.ExitOnError)
|
2024-10-12 22:15:44 +03:00
|
|
|
serveConfig := serveCmd.String("config", "", "override config file")
|
2024-10-12 21:45:00 +03:00
|
|
|
servePort := serveCmd.Int("port", 0, "override port")
|
|
|
|
serveDb := serveCmd.String("db", "", "override sqlite database")
|
2024-10-13 11:25:00 +03:00
|
|
|
serveDebug := serveCmd.Bool("debug", false, "debug logging")
|
2024-10-12 21:45:00 +03:00
|
|
|
|
|
|
|
verifyCmd := flag.NewFlagSet("verify", flag.ExitOnError)
|
2024-10-13 11:25:00 +03:00
|
|
|
verifyDebug := verifyCmd.Bool("debug", false, "debug logging")
|
2024-10-12 21:45:00 +03:00
|
|
|
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
fmt.Println("expected 'serve' or 'verify' subcommands")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch os.Args[1] {
|
|
|
|
case "serve":
|
|
|
|
serveCmd.Parse(os.Args[2:])
|
2024-10-13 11:25:00 +03:00
|
|
|
logging.LogInit(*serveDebug)
|
2024-10-12 22:15:44 +03:00
|
|
|
if *serveConfig != "" {
|
|
|
|
err := config.LoadConfig(*serveConfig)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("error loading custom config", "error", err)
|
|
|
|
}
|
|
|
|
}
|
2024-10-12 21:45:00 +03:00
|
|
|
if *servePort != 0 {
|
|
|
|
config.Cfg.Port = *servePort
|
|
|
|
}
|
|
|
|
if *serveDb != "" {
|
|
|
|
config.Cfg.SQLiteFile = *serveDb
|
|
|
|
}
|
|
|
|
serve.Serve()
|
|
|
|
case "verify":
|
|
|
|
verifyCmd.Parse(os.Args[2:])
|
2024-10-13 11:25:00 +03:00
|
|
|
logging.LogInit(*verifyDebug)
|
2024-10-12 21:45:00 +03:00
|
|
|
if len(os.Args) != 4 {
|
2024-10-13 11:25:00 +03:00
|
|
|
fmt.Println("Usage: <jwt> <pem file> [--debug]")
|
2024-10-12 21:45:00 +03:00
|
|
|
}
|
|
|
|
verify.Verify(os.Args[2], os.Args[3])
|
|
|
|
default:
|
|
|
|
fmt.Println("expected 'serve' or 'verify' subcommands")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|