Rework Version and Sub-version setup.
[src/xds/xds-server.git] / main.go
1 // TODO add Doc
2 //
3 package main
4
5 import (
6         "log"
7         "os"
8
9         "github.com/Sirupsen/logrus"
10         "github.com/codegangsta/cli"
11         "github.com/iotbzh/xds-server/lib/xdsconfig"
12         "github.com/iotbzh/xds-server/lib/xdsserver"
13 )
14
15 const (
16         appName        = "xds-server"
17         appDescription = "X(cross) Development System Server is a web server that allows to remotely cross build applications."
18         appCopyright   = "Apache-2.0"
19         appUsage       = "X(cross) Development System Server"
20 )
21
22 var appAuthors = []cli.Author{
23         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
24 }
25
26 // AppVersion is the version of this application
27 var AppVersion = "?.?.?"
28
29 // AppSubVersion is the git tag id added to version string
30 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
31 var AppSubVersion = "unknown-dev"
32
33 // Web server main routine
34 func webServer(ctx *cli.Context) error {
35
36         // Init config
37         cfg, err := xdsconfig.Init(ctx)
38         if err != nil {
39                 return cli.NewExitError(err, 2)
40         }
41
42         // Create and start Web Server
43         svr := xdsserver.NewServer(cfg)
44         if err = svr.Serve(); err != nil {
45                 log.Println(err)
46                 return cli.NewExitError(err, 3)
47         }
48
49         return cli.NewExitError("Program exited ", 4)
50 }
51
52 // main
53 func main() {
54
55         // Create a new instance of the logger
56         log := logrus.New()
57
58         // Create a new App instance
59         app := cli.NewApp()
60         app.Name = appName
61         app.Description = appDescription
62         app.Usage = appUsage
63         app.Version = AppVersion + " (" + AppSubVersion + ")"
64         app.Authors = appAuthors
65         app.Copyright = appCopyright
66         app.Metadata = make(map[string]interface{})
67         app.Metadata["version"] = AppVersion
68         app.Metadata["git-tag"] = AppSubVersion
69         app.Metadata["logger"] = log
70
71         app.Flags = []cli.Flag{
72                 cli.StringFlag{
73                         Name:   "config, c",
74                         Usage:  "JSON config file to use\n\t",
75                         EnvVar: "APP_CONFIG",
76                 },
77                 cli.StringFlag{
78                         Name:   "log, l",
79                         Value:  "error",
80                         Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
81                         EnvVar: "LOG_LEVEL",
82                 },
83         }
84
85         // only one action: Web Server
86         app.Action = webServer
87
88         app.Run(os.Args)
89 }