-
Notifications
You must be signed in to change notification settings - Fork 4
/
md.go
67 lines (57 loc) · 1.82 KB
/
md.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
const VERSION = "v1.2.1"
var helpText = `
Usage: md FILE.md
md -p 3000 -n 5 FILE.md
-p, --port Port to serve from (default 8080)
-n, --interval Set update interval in seconds (default 1)
-h, --help Output usage information
-v, --verbose Enable verbose log output
--version Show application version
`
// entry point & validation
func main() {
var port, interval int
var help, verbose, version bool
flag.IntVar(&port, "port", 8080, "Port to serve from (default 8080)")
flag.IntVar(&port, "p", 8080, "Port to serve from (default 8080)")
flag.IntVar(&interval, "interval", 1, "Update interval in seconds (default 1)")
flag.IntVar(&interval, "n", 1, "Update interval in seconds (default 1)")
flag.BoolVar(&help, "help", false, "Output usage information")
flag.BoolVar(&help, "h", false, "Output usage information")
flag.BoolVar(&verbose, "verbose", false, "Enable verbose log output")
flag.BoolVar(&verbose, "v", false, "Enable verbose log output")
flag.BoolVar(&version, "version", false, "Show application version")
flag.Parse()
args := flag.Args()
if help {
usage("")
} else if version {
fmt.Printf("md version %s\n", VERSION)
} else if len(args) == 0 {
usage("Please provide a file as an argument e.g. README.md")
} else if len(args) > 1 {
usage("Please limit to a single file")
} else {
filename := args[0]
// Serve MarkdownHandler
log.Printf("Starting Markdown Server for '%s' at http://localhost:%d", filename, port)
http.Handle("/", NewMarkdownHandler(filename, interval, verbose))
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
log.Fatal(err)
}
}
func usage(note string) {
if len(note) > 0 {
fmt.Println("Error: " + note)
}
fmt.Println(helpText)
os.Exit(0)
}