~blowry/sourcehut-vanity

3d77adbc1b22a8d558bc70485ff51a7e13c12eb9 — Benjamin Lowry 1 year, 5 months ago f5495b0
Parse command-line options with getopt
3 files changed, 20 insertions(+), 14 deletions(-)

M go.mod
A go.sum
M main.go
M go.mod => go.mod +2 -0
@@ 1,3 1,5 @@
module ben.gmbh/sourcehut-vanity

go 1.13

require git.sr.ht/~sircmpwn/getopt v0.0.0-20190808004552-daaf1274538b

A go.sum => go.sum +6 -0
@@ 0,0 1,6 @@
git.sr.ht/~sircmpwn/getopt v0.0.0-20190808004552-daaf1274538b h1:da5JBQ6dcW14aWnEf/pFRIMV2PsqTQEWmR+V2sw5oxU=
git.sr.ht/~sircmpwn/getopt v0.0.0-20190808004552-daaf1274538b/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

M main.go => main.go +12 -14
@@ 12,26 12,24 @@ package main
import (
    "fmt"
    "net/http"
    "git.sr.ht/~sircmpwn/getopt"
    "ben.gmbh/sourcehut-vanity/vanityserver"
    "os"
)

func printusage() {
    fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity username [0.0.0.0:8080]\n")
    os.Exit(1)
}

func main() {
    var port string
    if (len(os.Args) < 2) {
        printusage()
    var user string
    getopt.StringVar(&port, "p", "0.0.0.0:8080", "address and port to bind to")
    getopt.StringVar(&user, "u", "", "username to redirect to")
    if e := getopt.Parse(); e != nil {
        panic(e)
    }
    if (len(os.Args) == 3) {
        port = os.Args[2]
    } else {
        port = "0.0.0.0:8080"
    if (user == "" || port == "") {
        fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username [-p 0.0.0.0:8080]\n")
        getopt.PrintDefaults()
        os.Exit(1)
    }

    fmt.Printf("Serving %v's projects on %v\n", os.Args[1], port)
    fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: os.Args[1]}))
    fmt.Printf("Serving %v's projects on %v\n", user, port)
    fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user}))
}