-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.go
113 lines (93 loc) · 2.89 KB
/
flags.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
package main
import (
"os"
"path/filepath"
"github.com/alecthomas/kingpin/v2"
)
var (
hostFlag = kingpin.Flag("host", "The host to bind to.").Short('h').Default("0.0.0.0").Envar("HOST").String()
host string
portFlag = kingpin.Flag("port", "The port to serve.").Short('p').Default("8080").Envar("PORT").String()
port string
goFileServerFlag = kingpin.Flag("go", "Use Go's http.FileServer.").Short('g').Bool()
goFileServer bool
uploadFlag = kingpin.Flag("upload", "Allow file uploading.").Short('u').Bool()
upload bool
maxUploadMemoryFlag = kingpin.Flag("max-upload-memory", "The maximum amount of memory allowed to be used when saving uploaded files.").Short('m').Default("10MiB").Bytes()
maxUploadMemory int64
indexFlag = kingpin.Flag("index", "Automatically serve index files.").Short('i').Bool()
index bool
useTLSFlag = kingpin.Flag("tls", "Enable TLS.").Short('t').Bool()
useTLS bool
tlsCertFlag = kingpin.Flag("cert", "Path to TLS certificate file.").Short('c').Envar("TLS_CERT").ExistingFile()
tlsCert string
tlsKeyFlag = kingpin.Flag("key", "Path to the TLS key file.").Short('k').Envar("TLS_KEY").ExistingFile()
tlsKey string
exportsArg = kingpin.Arg("files", "The files or directories to share.").Default(".").ExistingFilesOrDirs()
exports []string
)
func parseFlags() {
kingpin.Parse()
host = *hostFlag
port = *portFlag
goFileServer = *goFileServerFlag
upload = *uploadFlag
maxUploadMemory = int64(*maxUploadMemoryFlag)
index = *indexFlag
useTLS = *useTLSFlag
tlsCert = *tlsCertFlag
tlsKey = *tlsKeyFlag
for _, export := range *exportsArg {
abs, err := filepath.Abs(export)
if err != nil {
kingpin.Fatalf("error while getting export %s's absolute path: %s", export, err)
}
for _, export := range exports {
if filepath.Base(export) == filepath.Base(abs) {
kingpin.Fatalf("can't have 2 exports with same base name: %s, %s", export, abs)
}
}
exports = append(exports, abs)
}
if len(exports) == 1 {
info, err := os.Stat(exports[0])
if err != nil {
kingpin.Fatalf("error while finding root mode: %s", err)
}
if info.IsDir() {
rootMode = rootModeSingleDir
} else {
rootMode = rootModeSingleFile
}
} else {
rootMode = rootModeExports
}
}
func checkForFlagIncompatabilities() {
var err bool
if useTLS && (tlsCert == "" || tlsKey == "") {
kingpin.Errorf("flags --cert and --key are required when --tls is set")
err = true
}
if goFileServer {
if rootMode != rootModeSingleDir {
kingpin.Errorf("flag --go only compatible with rootModeSingleDir")
err = true
}
if upload {
kingpin.Errorf("flag --upload incompatible with --go")
err = true
}
if index {
kingpin.Errorf("flag --index incompatible with --go")
err = true
}
}
if upload && rootMode == rootModeSingleFile {
kingpin.Errorf("flag --upload incompatible with rootModeSingleFile")
err = true
}
if err {
os.Exit(1)
}
}