Files
gator/main.go
T

51 lines
945 B
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
)
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)
}