2018-09-27 16:51:36 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-27 23:11:07 +02:00
|
|
|
"flag"
|
2018-09-27 16:51:36 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2018-09-27 23:11:07 +02:00
|
|
|
paneldb "git.lattuga.net/boyska/feedpanel/db"
|
2018-09-27 16:51:36 +02:00
|
|
|
"github.com/go-pg/pg"
|
2018-09-27 23:11:07 +02:00
|
|
|
"github.com/howeyc/gopass"
|
2018-09-27 16:51:36 +02:00
|
|
|
)
|
|
|
|
|
2018-09-28 00:55:12 +02:00
|
|
|
type commandFunc func([]string) error
|
|
|
|
|
|
|
|
var globalOptions struct {
|
|
|
|
verbose bool
|
|
|
|
pgOptions pg.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdMap map[string]commandFunc
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdMap = map[string]commandFunc{
|
2018-09-29 12:18:29 +02:00
|
|
|
"setup": cmdSetup,
|
|
|
|
"adduser": cmdAddUser,
|
|
|
|
"listusers": cmdListUsers,
|
2018-09-28 00:55:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:51:36 +02:00
|
|
|
func getDB() paneldb.DB {
|
2018-09-28 00:55:12 +02:00
|
|
|
pgdb := pg.Connect(&globalOptions.pgOptions)
|
2018-09-27 16:51:36 +02:00
|
|
|
db := paneldb.DB{PgDB: pgdb}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:18:29 +02:00
|
|
|
func cmdSetup(args []string) error {
|
2018-09-27 16:51:36 +02:00
|
|
|
db := getDB()
|
|
|
|
err := db.Setup()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-09-28 00:55:12 +02:00
|
|
|
if globalOptions.verbose {
|
|
|
|
fmt.Println("Tables created")
|
|
|
|
}
|
2018-09-27 16:51:36 +02:00
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-27 23:11:07 +02:00
|
|
|
func askPassword() (string, error) {
|
|
|
|
fmt.Print("New password: ")
|
|
|
|
pass, err := gopass.GetPasswd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fmt.Print("Retype new password: ")
|
|
|
|
pass2, err := gopass.GetPasswd()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if string(pass) != string(pass2) {
|
|
|
|
return "", fmt.Errorf("Passwords differ")
|
|
|
|
}
|
|
|
|
return string(pass), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdAddUser(args []string) error {
|
|
|
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
|
|
|
password := flagset.String("password", "", "set password from command line; INSECURE!")
|
|
|
|
flagset.Parse(args[1:])
|
|
|
|
if len(flagset.Args()) < 2 {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] USER EMAIL\n", args[0])
|
|
|
|
flagset.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
user := flagset.Args()[0]
|
|
|
|
email := flagset.Args()[1]
|
|
|
|
if *password == "" {
|
|
|
|
pass, err := askPassword()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
*password = string(pass)
|
|
|
|
}
|
|
|
|
db := getDB()
|
|
|
|
err := db.UserAdd(user, email, *password)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-09-28 00:55:12 +02:00
|
|
|
if globalOptions.verbose {
|
|
|
|
fmt.Printf("Success: user %s added\n", user)
|
|
|
|
}
|
2018-09-27 23:11:07 +02:00
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:18:29 +02:00
|
|
|
func composeListUserOutput(user paneldb.User, usernameFlag, emailFlag, dateCreatedFlag bool) {
|
|
|
|
var row string
|
|
|
|
if usernameFlag {
|
|
|
|
row += user.UserName
|
|
|
|
if emailFlag || !dateCreatedFlag {
|
|
|
|
row += "\t"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if emailFlag {
|
|
|
|
row += user.Email
|
|
|
|
if !dateCreatedFlag {
|
|
|
|
row += "\t"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !dateCreatedFlag {
|
|
|
|
row += user.DateCreated
|
|
|
|
}
|
|
|
|
fmt.Fprintln(os.Stdout, row)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdListUsers(args []string) error {
|
|
|
|
flagset := flag.NewFlagSet(args[0], flag.ExitOnError)
|
|
|
|
usernameFlag := flagset.Bool("no-nick", true, "skip printing usernames")
|
|
|
|
emailFlag := flagset.Bool("no-email", true, "skip printing emails")
|
|
|
|
dateCreatedFlag := flagset.Bool("created", false, "print date of user creation")
|
|
|
|
headerFlag := flagset.Bool("no-header", false, "skip printing the header")
|
|
|
|
flagset.Parse(args[1:])
|
|
|
|
db := getDB()
|
|
|
|
users, err := db.UserList()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
// Printing the header
|
|
|
|
if *headerFlag {
|
|
|
|
header := paneldb.User{
|
|
|
|
UserName: "username",
|
|
|
|
Email: "email",
|
|
|
|
DateCreated: "date_created",
|
|
|
|
}
|
|
|
|
separator := paneldb.User{
|
|
|
|
UserName: "--------",
|
|
|
|
Email: "-----",
|
|
|
|
DateCreated: "------------",
|
|
|
|
}
|
|
|
|
composeListUserOutput(header, *usernameFlag, *emailFlag, *dateCreatedFlag)
|
|
|
|
composeListUserOutput(separator, *usernameFlag, *emailFlag, *dateCreatedFlag)
|
|
|
|
}
|
|
|
|
// Printing the user list
|
|
|
|
for _, user := range users {
|
|
|
|
composeListUserOutput(user, *usernameFlag, *emailFlag, *dateCreatedFlag)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:51:36 +02:00
|
|
|
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() {
|
2018-09-28 00:55:12 +02:00
|
|
|
flag.StringVar(&globalOptions.pgOptions.User, "db-user", "panel", "The user to connect to database to")
|
|
|
|
flag.StringVar(&globalOptions.pgOptions.Password, "db-pass", "", "database password")
|
|
|
|
flag.StringVar(&globalOptions.pgOptions.Database, "db-name", "feeds", "database name")
|
|
|
|
flag.StringVar(&globalOptions.pgOptions.Addr, "db-addr", "127.0.0.1:5432", "database network address")
|
|
|
|
flag.BoolVar(&globalOptions.verbose, "verbose", false, "verbose output")
|
2018-09-27 23:11:07 +02:00
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) < 1 {
|
2018-09-27 16:51:36 +02:00
|
|
|
fmt.Fprintln(os.Stderr, "Not enough arguments!")
|
|
|
|
usage(os.Stderr)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2018-09-27 23:11:07 +02:00
|
|
|
cmdName := flag.Args()[0]
|
2018-09-27 16:51:36 +02:00
|
|
|
cmdFunc, ok := cmdMap[cmdName]
|
|
|
|
if !ok {
|
2018-09-27 23:11:07 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "Invalid command `%s`\n", cmdName)
|
2018-09-27 16:51:36 +02:00
|
|
|
usage(os.Stderr)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2018-09-27 23:11:07 +02:00
|
|
|
err := cmdFunc(flag.Args())
|
2018-09-27 16:51:36 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error")
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|