Added Copyright headers.
[src/xds/xds-cli.git] / cmd-misc.go
1 /*
2  * Copyright (C) 2017 "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         "github.com/iotbzh/xds-agent/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         })
48 }
49
50 func xdsVersion(ctx *cli.Context) error {
51         verbose := ctx.Bool("verbose")
52
53         // Get version
54         ver := xaapiv1.XDSVersion{}
55         if err := XdsVersionGet(&ver); err != nil {
56                 return cli.NewExitError(err.Error(), 1)
57         }
58
59         writer := NewTableWriter()
60         fmt.Fprintln(writer, "Agent:")
61         fmt.Fprintln(writer, "      ID:\t", ver.Client.ID)
62         v := ver.Client.Version
63         if verbose {
64                 v += " (" + ver.Client.VersionGitTag + ")"
65         }
66         fmt.Fprintln(writer, "      Version:\t", v)
67         if verbose {
68                 fmt.Fprintln(writer, "      API Version:\t", ver.Client.APIVersion)
69         }
70
71         for _, svr := range ver.Server {
72                 fmt.Fprintln(writer, "Server:")
73                 fmt.Fprintln(writer, "       ID:\t", svr.ID)
74                 v = svr.Version
75                 if verbose {
76                         v += " (" + svr.VersionGitTag + ")"
77                 }
78                 fmt.Fprintln(writer, "       Version:\t", v)
79                 if verbose {
80                         fmt.Fprintln(writer, "       API Version:\t", svr.APIVersion)
81                 }
82         }
83         writer.Flush()
84
85         return nil
86 }