forked from leodotcloud/swiss-army-knife
-
Notifications
You must be signed in to change notification settings - Fork 98
/
main.go
92 lines (82 loc) · 1.83 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
package main
import (
"fmt"
"os"
"github.com/leodotcloud/log"
logserver "github.com/leodotcloud/log/server"
"github.com/leodotcloud/swiss-army-knife/server"
"github.com/urfave/cli"
)
// VERSION of the application, that can defined during build time
var VERSION = "v0.0.0-dev"
const (
appName = "swiss-army-knife"
portArg = "port"
useMetadataArg = "use-rancher-metadata"
metadataAddressArg = "metadata-address"
alphabetArg = "alphabet"
)
func main() {
app := cli.NewApp()
app.Name = appName
app.Version = VERSION
app.Flags = []cli.Flag{
cli.StringFlag{
Name: metadataAddressArg,
Value: server.DefaultMetadataAddress,
EnvVar: "RANCHER_METADATA_ADDRESS",
},
cli.StringFlag{
Name: portArg,
Value: server.DefaultServerPort,
EnvVar: "PORT",
},
cli.StringFlag{
Name: alphabetArg,
Usage: "Run the web server with the given alphabet",
EnvVar: "ALPHABET",
},
cli.BoolFlag{
Name: useMetadataArg,
Usage: "Use Rancher metadata for querying information about self",
EnvVar: "USE_RANCHER_METADATA",
},
cli.BoolFlag{
Name: "debug",
Usage: "Turn on debug logging",
EnvVar: "DEBUG",
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
os.Exit(1)
}
}
func run(c *cli.Context) error {
logserver.StartServerWithDefaults()
if c.Bool("debug") {
if err := log.SetLevelString("debug"); err != nil {
return err
}
}
if c.Bool("version") {
fmt.Println(c.App.Version)
return nil
}
s, err := server.NewServer(
c.String(portArg),
c.String(metadataAddressArg),
c.String(alphabetArg),
c.Bool(useMetadataArg),
)
if err != nil {
log.Errorf("Error creating new server")
return err
}
if err := s.Run(); err != nil {
log.Errorf("Failed to start: %v", err)
}
<-s.GetExitChannel()
log.Infof("Program exiting")
return nil
}