Update .gitreview file
[src/xds/xds-cli.git] / cmd-misc.go
1 /*
2  * Copyright (C) 2017-2018 "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
19 package main
20
21 import (
22         "fmt"
23
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
25         "github.com/urfave/cli"
26 )
27
28 func initCmdMisc(cmdDef *[]cli.Command) {
29         *cmdDef = append(*cmdDef, cli.Command{
30                 Name:     "misc",
31                 HideHelp: true,
32                 Usage:    "miscellaneous commands group",
33                 Subcommands: []cli.Command{
34                         {
35                                 Name:    "version",
36                                 Aliases: []string{"v"},
37                                 Usage:   "Get version of XDS agent and XDS server",
38                                 Action:  xdsVersion,
39                                 Flags: []cli.Flag{
40                                         cli.BoolFlag{
41                                                 Name:  "verbose, v",
42                                                 Usage: "display verbose output",
43                                         },
44                                 },
45                         },
46                         {
47                                 Name:    "status",
48                                 Aliases: []string{"sts"},
49                                 Usage:   "Get XDS configuration status (including XDS server connection)",
50                                 Action:  xdsStatus,
51                                 Flags: []cli.Flag{
52                                         cli.BoolFlag{
53                                                 Name:  "verbose, v",
54                                                 Usage: "display verbose output",
55                                         },
56                                 },
57                         },
58                 },
59         })
60 }
61
62 func xdsVersion(ctx *cli.Context) error {
63         verbose := ctx.Bool("verbose")
64
65         // Get version
66         ver := xaapiv1.XDSVersion{}
67         if err := XdsVersionGet(&ver); err != nil {
68                 return cli.NewExitError(err.Error(), 1)
69         }
70
71         writer := NewTableWriter()
72         fmt.Fprintln(writer, "Agent:")
73         fmt.Fprintln(writer, "      ID:\t", ver.Client.ID)
74         v := ver.Client.Version
75         if verbose {
76                 v += " (" + ver.Client.VersionGitTag + ")"
77         }
78         fmt.Fprintln(writer, "      Version:\t", v)
79         if verbose {
80                 fmt.Fprintln(writer, "      API Version:\t", ver.Client.APIVersion)
81         }
82
83         for _, svr := range ver.Server {
84                 fmt.Fprintln(writer, "Server:")
85                 fmt.Fprintln(writer, "       ID:\t", svr.ID)
86                 v = svr.Version
87                 if verbose {
88                         v += " (" + svr.VersionGitTag + ")"
89                 }
90                 fmt.Fprintln(writer, "       Version:\t", v)
91                 if verbose {
92                         fmt.Fprintln(writer, "       API Version:\t", svr.APIVersion)
93                 }
94         }
95         writer.Flush()
96
97         return nil
98 }
99
100 func xdsStatus(ctx *cli.Context) error {
101         cfg := xaapiv1.APIConfig{}
102         if err := XdsConfigGet(&cfg); err != nil {
103                 return cli.NewExitError(err.Error(), 1)
104         }
105
106         writer := NewTableWriter()
107         fmt.Fprintln(writer, "XDS Server:")
108         for _, svr := range cfg.Servers {
109                 fmt.Fprintln(writer, "       ID:\t", svr.ID)
110                 fmt.Fprintln(writer, "       URL:\t", svr.URL)
111                 fmt.Fprintln(writer, "       Connected:\t", svr.Connected)
112                 fmt.Fprintln(writer, "       Connection retry:\t", svr.ConnRetry)
113                 fmt.Fprintln(writer, "       Disabled:\t", svr.Disabled)
114         }
115         writer.Flush()
116
117         return nil
118 }