-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
177 lines (148 loc) · 3.52 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/russross/blackfriday"
"gopkg.in/fsnotify.v1"
)
func main() {
var dontOpen bool
var port int
flags := flag.NewFlagSet("readme", flag.ContinueOnError)
flags.SetOutput(os.Stdout)
flags.Usage = usage
flags.IntVar(&port, "port", 5678, "port number")
flags.BoolVar(&dontOpen, "dont-open", false, "dont open")
if err := flags.Parse(os.Args[1:]); err != nil {
log.Printf("%s", err)
return
}
// Check if we were provided the markdown path
file := ""
if args := flags.Args(); len(args) > 0 {
file = args[0]
}
os.Exit(run(port, dontOpen, file))
}
func run(port int, dontOpen bool, filePath string) int {
changeCh := make(chan string)
go serve(port, changeCh)
if !dontOpen {
url := fmt.Sprintf("http://localhost:%d", port)
if err := exec.Command(openCmd, url).Run(); err != nil {
log.Printf("%s", err)
return 1
}
}
if filePath == "" {
var err error
filePath, err = findReadme()
if err != nil {
log.Printf("%s", err)
return 1
}
}
if err := sendChanges(filePath, changeCh); err != nil {
log.Printf("%s", err)
return 1
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("%s", err)
return 1
}
defer watcher.Close()
for {
if err := watcher.Add(filePath); err != nil {
log.Printf("%s", err)
return 1
}
select {
case event := <-watcher.Events:
time.Sleep(10 * time.Millisecond)
switch {
case event.Op&fsnotify.Write == fsnotify.Write:
case event.Op&fsnotify.Create == fsnotify.Create:
case event.Op&fsnotify.Rename == fsnotify.Rename:
default:
continue
}
log.Printf("Detected file change")
if err := sendChanges(filePath, changeCh); err != nil {
log.Printf("%s", err)
return 1
}
case err := <-watcher.Errors:
log.Printf("%s", err)
return 1
}
}
return 0
}
func serve(port int, changeCh <-chan string) {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.URL.String() != "/" {
path := filepath.Join(".", req.URL.String())
if _, err := os.Stat(path); err == nil {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("%s", err)
} else {
io.WriteString(w, string(content))
}
}
return
}
io.WriteString(w, <-changeCh)
})
s := &http.Server{Addr: fmt.Sprintf(":%d", port)}
if err := s.ListenAndServe(); err != nil {
panic(fmt.Sprintf("error serving: %s", err))
}
}
func sendChanges(path string, changeCh chan<- string) error {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
html := header
html += string(blackfriday.MarkdownCommon(content))
html += footer
changeCh <- html
return nil
}
func findReadme() (string, error) {
files, err := ioutil.ReadDir(".")
if err != nil {
return "", err
}
for _, file := range files {
if strings.HasPrefix(file.Name(), "README") {
return file.Name(), nil
}
}
return "", fmt.Errorf("No README files found")
}
func usage() {
helpText := `
usage: readme [options] [file]
Starts an HTTP server to display live updates to your README file.
By default, the current working directory is searched for a file
with the prefix 'README'. The file can be specified explicilty by
passing an additional argument with the path to the desired file.
Options:
-port=<number> The port number to start the server on.
-dont-open Do not automatically open the page in a browser
`
os.Stderr.WriteString(strings.TrimSpace(helpText) + "\n")
os.Exit(1)
}