3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 07:29:31 +01:00

convert some args to subcommands

This commit is contained in:
Jeremy Latt 2014-03-12 18:57:00 -07:00
parent 2006aff9f7
commit ab95fc8002

View File

@ -9,46 +9,68 @@ import (
"path/filepath" "path/filepath"
) )
func usage() {
fmt.Fprintln(os.Stderr, "ergonomadic <run|genpasswd|initdb|upgradedb> [options]")
fmt.Fprintln(os.Stderr, " run -conf <config> -- run server")
fmt.Fprintln(os.Stderr, " initdb -conf <config> -- initialize database")
fmt.Fprintln(os.Stderr, " upgrade -conf <config> -- upgrade database")
fmt.Fprintln(os.Stderr, " genpasswd <password> -- bcrypt a password")
flag.PrintDefaults()
}
func loadConfig(conf string) *irc.Config {
config, err := irc.LoadConfig(conf)
if err != nil {
log.Fatalln("error loading config:", err)
}
err = os.Chdir(filepath.Dir(conf))
if err != nil {
log.Fatalln("chdir error:", err)
}
return config
}
func genPasswd() {
}
func main() { func main() {
conf := flag.String("conf", "ergonomadic.conf", "ergonomadic config file") var conf string
initdb := flag.Bool("initdb", false, "initialize database") flag.Usage = usage
upgradedb := flag.Bool("upgradedb", false, "update database")
passwd := flag.String("genpasswd", "", "bcrypt a password") runFlags := flag.NewFlagSet("run", flag.ExitOnError)
runFlags.Usage = usage
runFlags.StringVar(&conf, "conf", "ergonomadic.conf", "ergonomadic config file")
flag.Parse() flag.Parse()
if *passwd != "" { switch flag.Arg(0) {
encoded, err := irc.GenerateEncodedPassword(*passwd) case "genpasswd":
encoded, err := irc.GenerateEncodedPassword(flag.Arg(1))
if err != nil { if err != nil {
log.Fatal("encoding error: ", err) log.Fatalln("encoding error:", err)
} }
fmt.Println(encoded) fmt.Println(encoded)
return
}
config, err := irc.LoadConfig(*conf) case "initdb":
if err != nil { runFlags.Parse(flag.Args()[1:])
log.Fatal("error loading config: ", err) config := loadConfig(conf)
}
err = os.Chdir(filepath.Dir(*conf))
if err != nil {
log.Fatal("chdir error: ", err)
}
if *initdb {
irc.InitDB(config.Server.Database) irc.InitDB(config.Server.Database)
log.Println("database initialized: ", config.Server.Database) log.Println("database initialized: ", config.Server.Database)
return
}
if *upgradedb { case "upgradedb":
runFlags.Parse(flag.Args()[1:])
config := loadConfig(conf)
irc.UpgradeDB(config.Server.Database) irc.UpgradeDB(config.Server.Database)
log.Println("database upgraded: ", config.Server.Database) log.Println("database upgraded: ", config.Server.Database)
return
}
default:
runFlags.Parse(flag.Args()[1:])
config := loadConfig(conf)
irc.Log.SetLevel(config.Server.Log) irc.Log.SetLevel(config.Server.Log)
server := irc.NewServer(config) server := irc.NewServer(config)
log.Println(irc.SEM_VER, "running") log.Println(irc.SEM_VER, "running")
defer log.Println(irc.SEM_VER, "exiting") defer log.Println(irc.SEM_VER, "exiting")
server.Run() server.Run()
}
} }