forked from cristianoliveira/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (114 loc) · 2.56 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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/cristianoliveira/ergo/commands"
"github.com/cristianoliveira/ergo/proxy"
)
//VERSION of ergo.
var VERSION string
//USAGE details the usage for ergo proxy.
const USAGE = `
Ergo proxy.
The local proxy agent for multiple services development.
Usage:
ergo run [options]
ergo list
ergo list-names
ergo url <name>
ergo setup [options] [linux-gnome|osx|windows] [-remove]
ergo add [options] <service-name> <host:port>
Options:
-h Shows this message.
-v Shows ergo's version.
-config Set the config file to the proxy.
run:
-p Set ports to proxy.
-V Set verbosity on output.
setup:
-remove Set remove proxy configurations.
`
func command() func() {
// Fail fast if we didn't receive a command argument
if len(os.Args) == 1 {
return nil
}
config := proxy.NewConfig()
command := flag.NewFlagSet(os.Args[1], flag.ExitOnError)
configFile := command.String("config", "./.ergo", "Set the services file")
command.Parse(os.Args[2:])
services, err := proxy.LoadServices(*configFile)
if err != nil {
log.Printf("Could not load services: %v\n", err)
}
config.Services = services
config.ConfigFile = *configFile
switch os.Args[1] {
case "list":
return func() {
commands.List(config)
}
case "list-names":
return func() {
commands.ListNames(config)
}
case "setup":
if len(os.Args) <= 2 {
return nil
}
system := command.Args()[0]
setupRemove := command.Bool("remove", false, "Set remove proxy configurations.")
command.Parse(command.Args()[1:])
return func() {
commands.Setup(system, *setupRemove, config)
}
case "url":
if len(os.Args) != 3 {
return nil
}
name := os.Args[2]
return func() {
commands.URL(name, config)
}
case "run":
command.StringVar(&config.Port, "p", "2000", "Set port to the proxy")
command.BoolVar(&config.Verbose, "V", false, "Set verbosity on proxy output")
command.Parse(os.Args[2:])
return func() {
commands.Run(config)
}
case "add":
if len(os.Args) <= 3 {
return nil
}
name := os.Args[2]
url := os.Args[3]
service := proxy.NewService(name, url)
return func() {
commands.AddService(config, service, *configFile)
}
}
return nil
}
func main() {
help := flag.Bool("h", false, "Shows ergo's help.")
version := flag.Bool("v", false, "Shows ergo's version.")
flag.Parse()
if *version {
fmt.Println(VERSION)
return
}
if *help {
fmt.Println(USAGE)
return
}
cmd := command()
if cmd == nil {
fmt.Println(USAGE)
os.Exit(1)
} else {
cmd()
}
}