command line errors to stderr and exit(1), like zev

This commit is contained in:
Ted Unangst 2023-08-28 01:21:44 -04:00
parent d69156f96e
commit cc3976daa6
1 changed files with 26 additions and 36 deletions

62
main.go
View File

@ -64,6 +64,11 @@ func reexecArgs(cmd string) []string {
var elog, ilog, dlog *golog.Logger var elog, ilog, dlog *golog.Logger
func errx(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
func main() { func main() {
flag.StringVar(&dataDir, "datadir", dataDir, "data directory") flag.StringVar(&dataDir, "datadir", dataDir, "data directory")
flag.StringVar(&viewDir, "viewdir", viewDir, "view directory") flag.StringVar(&viewDir, "viewdir", viewDir, "view directory")
@ -121,17 +126,17 @@ func main() {
adminscreen() adminscreen()
case "import": case "import":
if len(args) != 4 { if len(args) != 4 {
elog.Fatal("import username honk|mastodon|twitter srcdir") errx("import username honk|mastodon|twitter srcdir")
} }
importMain(args[1], args[2], args[3]) importMain(args[1], args[2], args[3])
case "export": case "export":
if len(args) != 3 { if len(args) != 3 {
elog.Fatal("export username destdir") errx("export username destdir")
} }
export(args[1], args[2]) export(args[1], args[2])
case "devel": case "devel":
if len(args) != 2 { if len(args) != 2 {
elog.Fatal("need an argument: devel (on|off)") errx("need an argument: devel (on|off)")
} }
switch args[1] { switch args[1] {
case "on": case "on":
@ -139,11 +144,11 @@ func main() {
case "off": case "off":
setconfig("devel", 0) setconfig("devel", 0)
default: default:
elog.Fatal("argument must be on or off") errx("argument must be on or off")
} }
case "setconfig": case "setconfig":
if len(args) != 3 { if len(args) != 3 {
elog.Fatal("need an argument: setconfig key val") errx("need an argument: setconfig key val")
} }
var val interface{} var val interface{}
var err error var err error
@ -155,66 +160,55 @@ func main() {
adduser() adduser()
case "deluser": case "deluser":
if len(args) < 2 { if len(args) < 2 {
fmt.Printf("usage: honk deluser username\n") errx("usage: honk deluser username\n")
return
} }
deluser(args[1]) deluser(args[1])
case "chpass": case "chpass":
if len(args) < 2 { if len(args) < 2 {
fmt.Printf("usage: honk chpass username\n") errx("usage: honk chpass username\n")
return
} }
chpass(args[1]) chpass(args[1])
case "follow": case "follow":
if len(args) < 3 { if len(args) < 3 {
fmt.Printf("usage: honk follow username url\n") errx("usage: honk follow username url\n")
return
} }
user, err := butwhatabout(args[1]) user, err := butwhatabout(args[1])
if err != nil { if err != nil {
fmt.Printf("user not found\n") errx("user %s not found\n", args[1])
return
} }
var meta HonkerMeta var meta HonkerMeta
mj, _ := jsonify(&meta) mj, _ := jsonify(&meta)
honkerid, err := savehonker(user, args[2], "", "presub", "", mj) honkerid, err := savehonker(user, args[2], "", "presub", "", mj)
if err != nil { if err != nil {
fmt.Printf("had some trouble with that: %s\n", err) errx("had some trouble with that: %s\n", err)
return
} }
followyou(user, honkerid, true) followyou(user, honkerid, true)
case "unfollow": case "unfollow":
if len(args) < 3 { if len(args) < 3 {
fmt.Printf("usage: honk unfollow username url\n") errx("usage: honk unfollow username url\n")
return
} }
user, err := butwhatabout(args[1]) user, err := butwhatabout(args[1])
if err != nil { if err != nil {
fmt.Printf("user not found\n") errx("user not found\n")
return
} }
row := db.QueryRow("select honkerid from honkers where xid = ? and userid = ? and flavor in ('sub')", args[2], user.ID) row := db.QueryRow("select honkerid from honkers where xid = ? and userid = ? and flavor in ('sub')", args[2], user.ID)
var honkerid int64 var honkerid int64
err = row.Scan(&honkerid) err = row.Scan(&honkerid)
if err != nil { if err != nil {
fmt.Printf("sorry couldn't find them\n") errx("sorry couldn't find them\n")
return
} }
unfollowyou(user, honkerid, true) unfollowyou(user, honkerid, true)
case "sendmsg": case "sendmsg":
if len(args) < 4 { if len(args) < 4 {
fmt.Printf("usage: honk send username filename rcpt\n") errx("usage: honk send username filename rcpt\n")
return
} }
user, err := butwhatabout(args[1]) user, err := butwhatabout(args[1])
if err != nil { if err != nil {
fmt.Printf("user not found\n") errx("user %s not found\n", args[1])
return
} }
data, err := os.ReadFile(args[2]) data, err := os.ReadFile(args[2])
if err != nil { if err != nil {
fmt.Printf("can't read file\n") errx("can't read file: %s\n", err)
return
} }
deliverate(user.ID, args[3], data) deliverate(user.ID, args[3], data)
case "cleanup": case "cleanup":
@ -225,29 +219,25 @@ func main() {
cleanupdb(arg) cleanupdb(arg)
case "unplug": case "unplug":
if len(args) < 2 { if len(args) < 2 {
fmt.Printf("usage: honk unplug servername\n") errx("usage: honk unplug servername\n")
return
} }
name := args[1] name := args[1]
unplugserver(name) unplugserver(name)
case "backup": case "backup":
if len(args) < 2 { if len(args) < 2 {
fmt.Printf("usage: honk backup dirname\n") errx("usage: honk backup dirname\n")
return
} }
name := args[1] name := args[1]
svalbard(name) svalbard(name)
case "ping": case "ping":
if len(args) < 3 { if len(args) < 3 {
fmt.Printf("usage: honk ping (from username) (to username or url)\n") errx("usage: honk ping (from username) (to username or url)\n")
return
} }
name := args[1] name := args[1]
targ := args[2] targ := args[2]
user, err := butwhatabout(name) user, err := butwhatabout(name)
if err != nil { if err != nil {
elog.Printf("unknown user") errx("unknown user %s", name)
return
} }
ping(user, targ) ping(user, targ)
case "run": case "run":
@ -257,6 +247,6 @@ func main() {
case "test": case "test":
ElaborateUnitTests() ElaborateUnitTests()
default: default:
elog.Fatal("unknown command") errx("unknown command")
} }
} }