-
Notifications
You must be signed in to change notification settings - Fork 0
/
op.go
72 lines (65 loc) · 1.37 KB
/
op.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
package op
import (
"errors"
"fmt"
"os"
"github.com/blitz-frost/op/cli"
"github.com/blitz-frost/op/lib"
"github.com/blitz-frost/op/srv"
)
func Run() {
// on print switch, print routes found in config file and exit
//
// on meta switch with no further arguments - print variants and active variant
// otherwise applies the next arg as template variant
switch lib.ArgSwitch {
case lib.CmdPrint:
manifest, err := lib.DecodeConfig()
if err != nil {
fmt.Println(err)
return
}
for name, rt := range manifest.Routes {
s := ""
if rt.Default {
s = " - default"
}
fmt.Println(rt.Namespace + ": " + name + s)
}
return
case lib.CmdMeta:
if lib.ArgMajor == "" {
meta, err := lib.DecodeMeta()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Defined variants:")
for k, _ := range meta.Variants {
fmt.Println(k)
}
fmt.Println("Active: " + meta.Active)
} else {
if err := lib.ExecuteTemplate(lib.ArgMajor); err != nil {
fmt.Println(err)
return
}
}
return
}
// if lock file already exists, run as client
// otherwise run as server
asSrv := true
if _, err := os.OpenFile(lib.BasePath+"/lock", os.O_CREATE|os.O_EXCL, 0000); err != nil {
if !errors.Is(err, os.ErrExist) {
fmt.Println("lock file creation error:", err)
return
}
asSrv = false
}
if asSrv {
srv.Run()
} else {
cli.Run()
}
}