Fix logfile setting
[src/xds/xds-agent.git] / main.go
1 /*
2  * Copyright (C) 2017-2019 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *
18  * xds-agent: X(cross) Development System client running on developer/local host.
19  */
20
21 package main
22
23 import (
24         "os"
25
26         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/agent"
27         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xdsconfig"
28         "github.com/Sirupsen/logrus"
29         "github.com/urfave/cli"
30 )
31
32 const (
33         appName        = "xds-agent"
34         appDescription = "X(cross) Development System Agent is a web server that allows to remotely cross build applications."
35         appCopyright   = "Copyright (C) 2017-2019 IoT.bzh - Apache-2.0"
36         appUsage       = "X(cross) Development System Agent"
37 )
38
39 var appAuthors = []cli.Author{
40         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
41 }
42
43 // AppVersion is the version of this application
44 var AppVersion = "?.?.?"
45
46 // AppSubVersion is the git tag id added to version string
47 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
48 var AppSubVersion = "unknown-dev"
49
50 // xdsAgent main routine
51 func xdsAgent(cliCtx *cli.Context) error {
52         var err error
53
54         // Create Agent context
55         ctxAgent := agent.NewAgent(cliCtx)
56
57         // Load config
58         ctxAgent.Config, err = xdsconfig.Init(cliCtx, ctxAgent.Log)
59         if err != nil {
60                 return cli.NewExitError(err, 2)
61         }
62
63         // Run Agent (main loop)
64         errCode, err := ctxAgent.Run()
65
66         return cli.NewExitError(err, errCode)
67 }
68
69 // main
70 func main() {
71
72         // Create a new instance of the logger
73         log := logrus.New()
74
75         // Create a new App instance
76         app := cli.NewApp()
77         app.Name = appName
78         app.Description = appDescription
79         app.Usage = appUsage
80         app.Version = AppVersion + " (" + AppSubVersion + ")"
81         app.Authors = appAuthors
82         app.Copyright = appCopyright
83         app.Metadata = make(map[string]interface{})
84         app.Metadata["version"] = AppVersion
85         app.Metadata["git-tag"] = AppSubVersion
86         app.Metadata["logger"] = log
87
88         app.Flags = []cli.Flag{
89                 cli.StringFlag{
90                         Name:   "config, c",
91                         Usage:  "JSON config file to use\n\t",
92                         EnvVar: "XDS_CONFIGFILE",
93                 },
94                 cli.StringFlag{
95                         Name:   "log, l",
96                         Value:  "error",
97                         Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
98                         EnvVar: "XDS_LOGLEVEL",
99                 },
100                 cli.StringFlag{
101                         Name:   "logfile",
102                         Value:  "stdout",
103                         Usage:  "filename where logs will be redirected (default stdout)\n\t",
104                         EnvVar: "XDS_LOGFILE",
105                 },
106         }
107
108         // only one action
109         app.Action = xdsAgent
110
111         app.Run(os.Args)
112 }