grim/devweb

Swap out kingpin for kong
draft
2020-07-11, Gary Kramlich
4fa325daec62
Parents e85cdaf6e3d5
Children bcc9fd893b18
Swap out kingpin for kong
  • +3 -1
    consts/consts.go
  • +6 -0
    db/options.go
  • +7 -1
    db/types.go
  • +3 -3
    go.mod
  • +19 -7
    go.sum
  • +21 -57
    main.go
  • +48 -0
    server/cmd.go
  • +1 -0
    v1/books/books.go
  • --- a/consts/consts.go Tue Jun 30 05:48:37 2020 -0500
    +++ b/consts/consts.go Sat Jul 11 04:43:38 2020 -0500
    @@ -1,5 +1,7 @@
    package consts
    const (
    - Version = "0.0.1dev"
    + Name = "devweb"
    + Version = "0.0.1dev"
    + Description = "A web application for devhelp books"
    )
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/db/options.go Sat Jul 11 04:43:38 2020 -0500
    @@ -0,0 +1,6 @@
    +package db
    +
    +type Options struct {
    + Driver string `kong:"flag,name='db-driver',env='DB_DRIVER',help='The name of the database engine to use.',default='sqlite3'"`
    + Options string `kong:"flag,name='db-options',env='DB_OPTIONS',help='The options to pass to the database engine.',default='./webdev.db'"`
    +}
    --- a/db/types.go Tue Jun 30 05:48:37 2020 -0500
    +++ b/db/types.go Sat Jul 11 04:43:38 2020 -0500
    @@ -1,7 +1,13 @@
    package db
    type Book struct {
    - Name string `db:"name"`
    + ID string `db:"id"`
    + Name string `db:"name"`
    + DefaultVersion string `db:"default_version"`
    +}
    +
    +type BookVersion struct {
    + Book string `db:"book"`
    Type string `db:"type"`
    Version string `db:"version"`
    }
    --- a/go.mod Tue Jun 30 05:48:37 2020 -0500
    +++ b/go.mod Sat Jul 11 04:43:38 2020 -0500
    @@ -3,10 +3,10 @@
    go 1.14
    require (
    - github.com/alecthomas/kingpin v2.2.6+incompatible
    - github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
    - github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
    + github.com/alecthomas/kong v0.2.11
    github.com/gorilla/mux v1.7.4
    github.com/jmoiron/sqlx v1.2.0
    github.com/mattn/go-sqlite3 v1.14.0
    + github.com/stretchr/testify v1.4.0 // indirect
    + google.golang.org/appengine v1.6.6 // indirect
    )
    --- a/go.sum Tue Jun 30 05:48:37 2020 -0500
    +++ b/go.sum Sat Jul 11 04:43:38 2020 -0500
    @@ -1,31 +1,43 @@
    github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
    -github.com/alecthomas/kingpin v1.3.7 h1:GLMgiQ5nZb3rg5pozvF7KDbseQ12eNO1p7bB2t5K1fw=
    -github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI=
    -github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
    -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
    -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
    -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
    -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
    +github.com/alecthomas/kong v0.2.11 h1:RKeJXXWfg9N47RYfMm0+igkxBCTF4bzbneAxaqid0c4=
    +github.com/alecthomas/kong v0.2.11/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
    github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
    github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
    +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
    +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
    +github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
    github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
    +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
    github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
    github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
    github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
    github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
    +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
    github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
    github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
    github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
    github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
    +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
    +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
    +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
    github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
    github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
    +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
    +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
    github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
    golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
    golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
    +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
    golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
    golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
    golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
    golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
    golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
    +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
    +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
    +google.golang.org/appengine v1.6.6 h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc=
    +google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
    +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
    gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
    +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
    gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
    --- a/main.go Tue Jun 30 05:48:37 2020 -0500
    +++ b/main.go Sat Jul 11 04:43:38 2020 -0500
    @@ -2,71 +2,35 @@
    import (
    "fmt"
    - "os"
    - "os/signal"
    - "syscall"
    - "github.com/alecthomas/kingpin"
    + "github.com/alecthomas/kong"
    "keep.imfreedom.org/grim/devweb/consts"
    - "keep.imfreedom.org/grim/devweb/db"
    "keep.imfreedom.org/grim/devweb/server"
    )
    -var (
    - app = kingpin.New("devweb", "A web based documentation viewer").Version(consts.Version)
    - listenAddr = app.Flag("listen-addr", "Address to listen on").Default(":1234").Envar("LISTEN_ADDR").String()
    +type cli struct {
    + Serve server.Cmd `kong:"cmd,help='Run the server.',default='1'"`
    + Version versionCmd `kong:"cmd,help='Display the version number.'"`
    +}
    - db_engine = app.Flag("database-engine", "The name of the database engine to use").Default("sqlite3").Envar("DB_ENGINE").String()
    - db_options = app.Flag("database-options", "The options to pass to the database engine").Default("./webdev.db").Envar("DB_OPTIONS").String()
    -)
    +type versionCmd struct{}
    +
    +func (c *versionCmd) Run() error {
    + fmt.Printf("%s version %s\n", consts.Name, consts.Version)
    +
    + return nil
    +}
    func main() {
    - var err error
    -
    - _, err = app.Parse(os.Args[1:])
    - if err != nil {
    - fmt.Printf("%v\n", err)
    -
    - os.Exit(1)
    - }
    -
    - err = db.Setup(*db_engine, *db_options)
    - if err != nil {
    - fmt.Printf("%v\n", err)
    -
    - os.Exit(1)
    - }
    - defer db.Teardown()
    -
    - sigChan := make(chan os.Signal, 1)
    - signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
    -
    - errChan := make(chan error, 10)
    + cli := cli{}
    + ctx := kong.Parse(
    + &cli,
    + kong.Name(consts.Name),
    + kong.Description(consts.Description),
    + kong.UsageOnError(),
    + )
    - server := server.NewServer(*listenAddr)
    - defer server.Close()
    -
    - go func() {
    - if err := server.Listen(); err != nil {
    - errChan <- err
    - }
    - }()
    -
    - for {
    - select {
    - case err := <-errChan:
    - ec := 0
    -
    - if err != nil {
    - fmt.Printf("error: %v\n", err)
    - ec = 1
    - }
    -
    - os.Exit(ec)
    - case s := <-sigChan:
    - fmt.Printf("Captured signal %v. Exiting...\n", s)
    - errChan <- nil
    - }
    - }
    + err := ctx.Run()
    + ctx.FatalIfErrorf(err)
    }
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/server/cmd.go Sat Jul 11 04:43:38 2020 -0500
    @@ -0,0 +1,48 @@
    +package server
    +
    +import (
    + "fmt"
    + "os"
    + "os/signal"
    + "syscall"
    +
    + "keep.imfreedom.org/grim/devweb/db"
    +)
    +
    +type Cmd struct {
    + db.Options
    +
    + ListenAddr string `kong:"flag,name='listen-addr',help='The address to listen on.',default=':1234'"`
    +}
    +
    +func (c *Cmd) Run() error {
    + err := db.Setup(c.Options.Driver, c.Options.Options)
    + if err != nil {
    + return err
    + }
    + defer db.Teardown()
    +
    + sigChan := make(chan os.Signal, 1)
    + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
    +
    + errChan := make(chan error, 10)
    +
    + server := NewServer(c.ListenAddr)
    + defer server.Close()
    +
    + go func() {
    + if err := server.Listen(); err != nil {
    + errChan <- err
    + }
    + }()
    +
    + for {
    + select {
    + case err := <-errChan:
    + return err
    + case s := <-sigChan:
    + fmt.Printf("Captured signal %v. Exiting...\n", s)
    + errChan <- nil
    + }
    + }
    +}
    --- a/v1/books/books.go Tue Jun 30 05:48:37 2020 -0500
    +++ b/v1/books/books.go Sat Jul 11 04:43:38 2020 -0500
    @@ -5,6 +5,7 @@
    )
    func getBooks(w http.ResponseWriter, r *http.Request) {
    +
    }
    func createBook(w http.ResponseWriter, r *http.Request) {