Same things, more cleanup

explicit support for subcommands, separate backend library
This commit is contained in:
baz 2018-09-27 16:51:36 +02:00
parent 49255544f2
commit 0a2388168b
4 changed files with 72 additions and 20 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
vendor
panel
panelcli
go.sum

68
cmd/panelcli/cli.go Normal file
View file

@ -0,0 +1,68 @@
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)
}

View file

@ -1,8 +1,7 @@
package main
package paneldb
import (
"fmt"
"os"
"github.com/go-pg/pg"
"github.com/pkg/errors"
@ -44,19 +43,3 @@ func (db *DB) UserAdd(username, email, pwhash string) error {
}
return nil
}
func main() {
pgdb := pg.Connect(&pg.Options{
User: "panel",
Password: "panelpass",
Database: "feeds",
})
db := DB{PgDB: pgdb}
err := db.Setup()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("All done")
os.Exit(0)
}

2
go.mod
View file

@ -1,4 +1,4 @@
module panel
module git.lattuga.net/boyska/feedpanel
require (
github.com/go-pg/pg v6.15.0+incompatible