Files
gator/main.go
T

59 lines
1.3 KiB
Go
Raw Normal View History

2026-07-31 08:06:29 -05:00
package main
import (
2026-08-01 14:26:19 -05:00
"database/sql"
2026-07-31 08:06:29 -05:00
"fmt"
"os"
"github.com/StevanFreeborn/gator/internal/command"
"github.com/StevanFreeborn/gator/internal/config"
2026-08-01 14:26:19 -05:00
"github.com/StevanFreeborn/gator/internal/database"
2026-07-31 08:06:29 -05:00
"github.com/StevanFreeborn/gator/internal/state"
2026-08-01 14:26:19 -05:00
_ "github.com/lib/pq"
2026-07-31 08:06:29 -05:00
)
2026-08-03 07:16:59 -05:00
// TODO: Reconcile identifiers for commands. Should prefer referring to things by Name
// not URL
// TODO: Address what happens when user provides URL that resolves to same set of posts
// but URL slight difference than existing one. i.e. https://test.com/rss vs. https://test.com/rss/
// TODO: Extract exit handling in agg command to separate function
2026-07-31 08:06:29 -05:00
func main() {
if len(os.Args) < 2 {
fmt.Println("Not enough arguments provided. Usage: <command> <args>")
os.Exit(1)
}
c, err := config.Read()
if err != nil {
fmt.Printf("Error reading config file: %s", err)
2026-08-01 14:26:19 -05:00
os.Exit(1)
}
db, err := sql.Open("postgres", c.DbUrl)
if err != nil {
fmt.Printf("Error connecting to database: %s", err)
os.Exit(1)
2026-07-31 08:06:29 -05:00
}
cmd := os.Args[1]
args := os.Args[2:]
2026-08-01 14:26:19 -05:00
dbQueries := database.New(db)
s := state.NewState(dbQueries, c, args)
2026-07-31 08:06:29 -05:00
registry := command.NewRegistry()
err = registry.RunCommand(cmd, s)
if err != nil {
fmt.Printf("Error running '%s' command: %s\n", cmd, err)
2026-08-01 14:26:19 -05:00
os.Exit(1)
2026-07-31 08:06:29 -05:00
}
os.Exit(0)
}