-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
75 lines (64 loc) · 1.89 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
package main
import (
"flag"
"fmt"
"os"
"runtime"
"github.com/antsankov/go-live/lib"
"github.com/pkg/browser"
)
// VERSION of Package
const VERSION = "1.2.1"
func main() {
var _quiet bool
flag.BoolVar(&_quiet, "q", false, "Quiet stops go-live from opening the browser when started.")
flag.BoolVar(&_quiet, "quiet", false, "")
var _cache bool
flag.BoolVar(&_cache, "c", false, "Allow browser caching of pages. Can lead to stale results, off by default.")
flag.BoolVar(&_cache, "cache", false, "")
var _port string
flag.StringVar(&_port, "p", "9000", "Set port to serve on.")
flag.StringVar(&_port, "port", "9000", "")
var _version bool
flag.BoolVar(&_version, "v", false, "Print the version of go-live.")
flag.BoolVar(&_version, "version", false, "")
var _dir string
flag.StringVar(&_dir, "d", "./", "Select the directory you want to serve. Serves all subpaths that user has read permissions for.")
flag.StringVar(&_dir, "dir", "./", "")
var _serve bool
flag.BoolVar(&_serve, "s", false, "Start in server mode on port 80 and in quiet.")
flag.BoolVar(&_serve, "serve", false, "")
flag.Parse()
if _version || (len(os.Args) >= 2 && os.Args[1] == "version") {
fmt.Printf("v%s (%s/%s)\n", VERSION, runtime.GOOS, runtime.GOARCH)
return
}
if _dir != "./" {
// Check if last char is a slash, if not add it.
if _dir[len(_dir)-1] != '/' {
_dir = _dir + "/"
}
}
// Check if port begins with ":", if not add it.
if _port[0] != ':' {
_port = ":" + _port
}
var err error
if _serve {
err = lib.StartServer(_dir, ":80", true)
} else {
// If user is sudo we don't launch the browser.
if !_quiet && !isSudo() {
browser.OpenURL(fmt.Sprintf("http://localhost%s", _port))
}
err = lib.StartServer(_dir, _port, _cache)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// IsSudo checks if user is sudo
func isSudo() bool {
return (os.Geteuid() == 0)
}