Rewrote the whole CLI app code.
This commit is contained in:
parent
9043b8b10d
commit
f7e57b1a22
123
ss13.go
123
ss13.go
@ -1,100 +1,83 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/codegangsta/cli"
|
||||||
"github.com/lmas/ss13_se/src"
|
"github.com/lmas/ss13_se/src"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// General flags
|
|
||||||
f_debug = flag.Bool("debug", false, "Run in debug mode")
|
|
||||||
f_verbose = flag.Bool("verbose", false, "Show verbose messages")
|
|
||||||
|
|
||||||
// Flags for the run (webserver) command
|
|
||||||
f_addr = flag.String("addr", "127.0.0.1:8000", "Server's listening address")
|
|
||||||
|
|
||||||
// Flags for the update command
|
|
||||||
f_daemon = flag.Bool("daemon", false, "Continuously update")
|
|
||||||
f_timeout = flag.Int64("timeout", 15, "Time (in minutes) between each update in daemon mode")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Command struct {
|
|
||||||
Name string
|
|
||||||
Desc string
|
|
||||||
DoFunc func()
|
|
||||||
}
|
|
||||||
|
|
||||||
var commands = []Command{
|
|
||||||
// Main commands
|
|
||||||
Command{"serve", "Run a local web server.", doserve},
|
|
||||||
Command{"update", "Update the population stats.", doupdate},
|
|
||||||
|
|
||||||
// Misc. commands
|
|
||||||
Command{"version", "Show current version and exit.", doversion},
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
app := cli.NewApp()
|
||||||
ss13.SetDebug(*f_debug)
|
app.Version = ss13.VERSION
|
||||||
|
app.Usage = "" // TODO
|
||||||
command := flag.Arg(0)
|
app.Flags = []cli.Flag{
|
||||||
for _, a := range commands {
|
cli.BoolFlag{
|
||||||
if command == a.Name {
|
Name: "debug",
|
||||||
a.DoFunc()
|
Usage: "Run in debug mode",
|
||||||
return
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "verbose",
|
||||||
|
Usage: "Show verbose messages",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "addr",
|
||||||
|
Usage: "Set the listen address for the web server",
|
||||||
|
Value: ":8000",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "daemon",
|
||||||
|
Usage: "Continuously run when using the update command",
|
||||||
|
},
|
||||||
|
cli.IntFlag{
|
||||||
|
Name: "timeout",
|
||||||
|
Usage: "Time (in minutes) between each update in daemon mode",
|
||||||
|
Value: 15,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
app.Commands = []cli.Command{
|
||||||
|
{
|
||||||
|
Name: "run",
|
||||||
|
Usage: "Run the web server",
|
||||||
|
Action: run_server,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "update",
|
||||||
|
Usage: "Update the server stats",
|
||||||
|
Action: update_stats,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
dohelp()
|
app.Run(os.Args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dohelp() {
|
func run_server(c *cli.Context) {
|
||||||
fmt.Println(`SS13 population stats server.
|
if c.GlobalBool("verbose") {
|
||||||
|
fmt.Printf("Listening on %s.\n\n", c.GlobalString("addr"))
|
||||||
Usage:
|
|
||||||
TODO_NAME [flags] command [arguments]
|
|
||||||
|
|
||||||
Available commands:`)
|
|
||||||
for _, a := range commands {
|
|
||||||
fmt.Printf("\t%s\t%s\n", a.Name, a.Desc)
|
|
||||||
}
|
|
||||||
fmt.Println(`
|
|
||||||
Run TODO_NAME -h to get a list of command line flags.
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func doversion() {
|
|
||||||
fmt.Println("TODO: Use a fancy name, add a banner and set a version.")
|
|
||||||
}
|
|
||||||
|
|
||||||
func doserve() {
|
|
||||||
if *f_verbose {
|
|
||||||
fmt.Printf("Listening on %s.\n\n", *f_addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
instance := &ss13.Instance{
|
instance := &ss13.Instance{
|
||||||
Debug: *f_debug,
|
Debug: c.GlobalBool("debug"),
|
||||||
DB: ss13.OpenSqliteDB("new.db"), // TODO
|
DB: ss13.OpenSqliteDB("new.db"), // TODO
|
||||||
}
|
}
|
||||||
instance.Init()
|
instance.Init()
|
||||||
instance.Serve(*f_addr)
|
instance.Serve(c.GlobalString("addr"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func doupdate() {
|
func update_stats(c *cli.Context) {
|
||||||
td := time.Duration(*f_timeout) * time.Minute
|
td := time.Duration(c.GlobalInt("timeout")) * time.Minute
|
||||||
if *f_verbose {
|
if c.GlobalBool("verbose") {
|
||||||
if *f_daemon {
|
if c.GlobalBool("daemon") {
|
||||||
fmt.Printf("Running updates every %v minutes\n", *f_timeout)
|
fmt.Printf("Running updates every %v minutes\n", c.GlobalInt("timeout"))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Updating...")
|
fmt.Println("Updating...")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
instance := &ss13.Instance{
|
instance := &ss13.Instance{
|
||||||
Debug: *f_debug,
|
Debug: c.GlobalBool("debug"),
|
||||||
DB: ss13.OpenSqliteDB("new.db"), // TODO
|
DB: ss13.OpenSqliteDB("new.db"), // TODO
|
||||||
}
|
}
|
||||||
instance.Init()
|
instance.Init()
|
||||||
@ -103,11 +86,11 @@ func doupdate() {
|
|||||||
start := time.Now()
|
start := time.Now()
|
||||||
instance.UpdateServers()
|
instance.UpdateServers()
|
||||||
stop := time.Now()
|
stop := time.Now()
|
||||||
if *f_verbose {
|
if c.GlobalBool("verbose") {
|
||||||
fmt.Printf("Update completed in %v\n", stop.Sub(start))
|
fmt.Printf("Update completed in %v\n", stop.Sub(start))
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*f_daemon {
|
if !c.GlobalBool("daemon") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Next at ", time.Now().Add(td))
|
fmt.Println("Next at ", time.Now().Add(td))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user