added data dir cli option

This commit is contained in:
2026-05-18 04:39:31 -05:00
parent ea69970a4b
commit 4bc4566f34
+30 -15
View File
@@ -9,6 +9,7 @@ import (
"log"
"log/slog"
"os"
"path"
_ "github.com/mattn/go-sqlite3"
@@ -27,16 +28,21 @@ Valid commands:
`
func main() {
var host = flag.String("host", "127.0.0.1:8000", "Set the web server host address.")
var debug = flag.Bool("debug", false, "Enable debug mode.")
var notapology = flag.Bool("no-tapology", false, "Disable scraping tapology")
var noscraping = flag.Bool("no-scraping", false, "Disable scraping on runserver")
host := flag.String("host", "127.0.0.1:8000", "Set the web server host address.")
debug := flag.Bool("debug", false, "Enable debug mode.")
no_tapology := flag.Bool("no-tapology", false, "Disable scraping tapology")
no_scraping := flag.Bool("no-scraping", false, "Disable scraping on runserver")
data_dir := flag.String("data-dir", "", "Directory for storing data")
cmd := flag.String("cmd", "runserver", "Command to run [runserver, scrape]")
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Print(POSITIONAL_ARGS_HELP)
os.Exit(1)
if *data_dir != "" {
err := os.MkdirAll(*data_dir, 0666)
if err != nil {
fmt.Printf("Error creating data dir: %s\n", err)
os.Exit(1)
}
}
if *debug {
@@ -45,7 +51,8 @@ func main() {
slog.SetLogLoggerLevel(slog.LevelInfo)
}
f, err := os.OpenFile("mmaschedule.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
log_path := path.Join(*data_dir, "mmaschedule.log")
f, err := os.OpenFile(log_path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println("Failed opening log file", err)
os.Exit(1)
@@ -55,28 +62,36 @@ func main() {
w := io.MultiWriter(f, os.Stdout)
log.SetOutput(w)
cmd := flag.Args()[0]
client := NewScraperClient()
db, err := InitDb("db.sqlite")
db_path := path.Join(*data_dir, "db.sqlite")
db, err := InitDb(db_path)
if err != nil {
slog.Error("Failed initializing database", "error", err)
return
}
switch cmd {
slog.Info(
"Starting app",
"command", *cmd,
"host", *host,
"data_dir", *data_dir,
"log_path", log_path,
"db_path", db_path,
)
switch *cmd {
case "runserver":
if !*noscraping {
if !*no_scraping {
slog.Debug("Starting scraping loop")
go ScrapeEventsLoop(db, client, !*notapology)
go ScrapeEventsLoop(db, client, !*no_tapology)
}
err = RunServer(*host, db)
if err != nil {
slog.Error("Error starting web server:", "error", err)
}
case "scrape":
ScrapeEvents(db, client, !*notapology)
ScrapeEvents(db, client, !*no_tapology)
default:
fmt.Print(POSITIONAL_ARGS_HELP)
os.Exit(1)