-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
95 lines (85 loc) · 2.33 KB
/
config.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
var buildVersion string
const delimiter = "="
type Config struct {
client bool
port int
appUrl string
path string
name string
action socketAction
data string
}
func LoadConfig(args []string) *Config {
c := &Config{port: 5000, name: "lurch"}
c.setPath("workdir")
parseArgs(args, func(arg, value string) {
switch arg {
case "-p", "--port":
c.port, _ = strconv.Atoi(value)
case "-t", "--path":
c.setPath(value)
case "-a", "--app-url":
c.appUrl = value
case "-n", "--name":
c.name = value
case "-sj", "--start-job":
c.client = true
c.action = socketActionStart
c.data = value
case "-h", "--help":
fmt.Printf("Usage: lurch [options]\nOptions:\n\t-h, --help\t\t\tprint this help\n\t-v, --version\t\t\tprint version\n\t-t, --path [PATH]\t\tabsolute path to work dir\n\t-p, --port [PORT]\t\tsets port for listening\n\t-a, --app-url [APP_URL]\t\tapplication url (if behind proxy)\n\t-n, --name [NAME]\t\tname of application to be displayed\n\t-sj, --start-job [PROJECT]\tmakes client call to origin server and starts the build of [PROJECT]\n")
os.Exit(0)
case "-v", "--version":
fmt.Printf("lurch %s\nhttps://github.com/tvrzna/lurch\n\nReleased under the MIT License.\n", c.GetVersion())
os.Exit(0)
}
})
return c
}
func (c *Config) setPath(value string) {
if path, err := filepath.Abs(value); err != nil {
log.Fatal("wrong path", err)
} else {
c.path = path
}
}
func parseArgs(args []string, handler func(arg, nextArg string)) {
for i, arg := range args {
nextArg := ""
if len(args) > i+1 {
val := strings.TrimSpace(args[i+1])
if !strings.HasPrefix(val, "-") {
nextArg = val
}
}
if strings.Contains(arg, delimiter) {
nextArg = arg[strings.Index(arg, delimiter)+1:]
arg = arg[0:strings.Index(arg, delimiter)]
if (strings.HasPrefix(nextArg, "'") && strings.HasSuffix(nextArg, "'")) || (strings.HasPrefix(nextArg, "\"") && strings.HasSuffix(nextArg, "\"")) {
nextArg = nextArg[1 : len(nextArg)-1]
}
}
handler(arg, nextArg)
}
}
func (c *Config) getAppUrl() string {
return c.appUrl
}
func (c *Config) getServerUri() string {
return "localhost:" + strconv.Itoa(c.port)
}
func (c *Config) GetVersion() string {
if buildVersion == "" {
return "develop"
}
return buildVersion
}