This repository has been archived on 2024-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
feedpanel/cmd/panelcli/cli.go

69 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"io"
"os"
"git.lattuga.net/boyska/feedpanel/db"
"github.com/go-pg/pg"
)
func getDB() paneldb.DB {
pgdb := pg.Connect(&pg.Options{
User: "panel",
Password: "panelpass",
Database: "feeds",
})
db := paneldb.DB{PgDB: pgdb}
return db
}
type commandFunc func() error
func cmdSetup() error {
db := getDB()
err := db.Setup()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("All done")
os.Exit(0)
return nil
}
var cmdMap map[string]commandFunc = map[string]commandFunc{
"setup": cmdSetup,
}
func usage(w io.Writer) {
fmt.Fprintf(w, "Usage: %s SUBCOMMAND [command args...]\n", os.Args[0])
fmt.Fprintln(w, "\nSubcommands:")
for cmdName, _ := range cmdMap {
fmt.Fprintln(w, " -", cmdName)
}
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Not enough arguments!")
usage(os.Stderr)
os.Exit(2)
}
cmdName := os.Args[1]
cmdFunc, ok := cmdMap[cmdName]
if !ok {
fmt.Fprintln(os.Stderr, "Invalid command")
usage(os.Stderr)
os.Exit(2)
}
err := cmdFunc()
if err != nil {
fmt.Fprintln(os.Stderr, "Error")
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}