Minor fixes in sdks command output.
[src/xds/xds-cli.git] / cmd-projects.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         "strings"
24
25         "github.com/iotbzh/xds-agent/lib/xaapiv1"
26         "github.com/urfave/cli"
27 )
28
29 func initCmdProjects(cmdDef *[]cli.Command) {
30         *cmdDef = append(*cmdDef, cli.Command{
31                 Name:     "projects",
32                 Aliases:  []string{"prj"},
33                 HideHelp: true,
34                 Usage:    "project commands group",
35                 Subcommands: []cli.Command{
36                         {
37                                 Name:    "add",
38                                 Aliases: []string{"a"},
39                                 Usage:   "Add a new project",
40                                 Action:  projectsAdd,
41                                 Flags: []cli.Flag{
42                                         cli.StringFlag{
43                                                 Name:  "label, l",
44                                                 Usage: "project label (free form string)",
45                                         },
46                                         cli.StringFlag{
47                                                 Name:  "path, p",
48                                                 Usage: "project local path",
49                                         },
50                                         cli.StringFlag{
51                                                 Name:  "server-path, sp",
52                                                 Usage: "project server path (only used with pathmap type)",
53                                         },
54                                         cli.StringFlag{
55                                                 Name:  "type, t",
56                                                 Usage: "project type (pathmap|pm, cloudsync|sc)",
57                                         },
58                                 },
59                         },
60                         {
61                                 Name:   "get",
62                                 Usage:  "Get a property of a project",
63                                 Action: projectsGet,
64                                 Flags: []cli.Flag{
65                                         cli.StringFlag{
66                                                 Name:   "id",
67                                                 Usage:  "project id",
68                                                 EnvVar: "XDS_PROJECT_ID",
69                                         },
70                                 },
71                         },
72                         {
73                                 Name:    "list",
74                                 Aliases: []string{"ls"},
75                                 Usage:   "List existing projects",
76                                 Action:  projectsList,
77                                 Flags: []cli.Flag{
78                                         cli.BoolFlag{
79                                                 Name:  "verbose, v",
80                                                 Usage: "display verbose output",
81                                         },
82                                 },
83                         },
84                         {
85                                 Name:    "remove",
86                                 Aliases: []string{"rm"},
87                                 Usage:   "Remove an existing project",
88                                 Action:  projectsRemove,
89                                 Flags: []cli.Flag{
90                                         cli.StringFlag{
91                                                 Name:   "id",
92                                                 Usage:  "project id",
93                                                 EnvVar: "XDS_PROJECT_ID",
94                                         },
95                                 },
96                         },
97                         {
98                                 Name:    "sync",
99                                 Aliases: []string{},
100                                 Usage:   "Force synchronization of project sources",
101                                 Action:  projectsSync,
102                                 Flags: []cli.Flag{
103                                         cli.StringFlag{
104                                                 Name:   "id",
105                                                 Usage:  "project id",
106                                                 EnvVar: "XDS_PROJECT_ID",
107                                         },
108                                 },
109                         },
110                 },
111         })
112 }
113
114 func projectsList(ctx *cli.Context) error {
115         // Get projects list
116         prjs := []xaapiv1.ProjectConfig{}
117         if err := ProjectsListGet(&prjs); err != nil {
118                 return cli.NewExitError(err.Error(), 1)
119         }
120         _displayProjects(prjs, ctx.Bool("verbose"))
121         return nil
122 }
123
124 func projectsGet(ctx *cli.Context) error {
125         id := GetID(ctx)
126         if id == "" {
127                 return cli.NewExitError("id parameter or option must be set", 1)
128         }
129         prjs := make([]xaapiv1.ProjectConfig, 1)
130         if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
131                 return cli.NewExitError(err, 1)
132         }
133         _displayProjects(prjs, true)
134         return nil
135 }
136
137 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
138         // Display result
139         first := true
140         writer := NewTableWriter()
141         for _, folder := range prjs {
142                 if verbose {
143                         if !first {
144                                 fmt.Fprintln(writer)
145                         }
146                         fmt.Fprintln(writer, "ID:\t", folder.ID)
147                         fmt.Fprintln(writer, "Label:\t", folder.Label)
148                         fmt.Fprintln(writer, "Path type:\t", folder.Type)
149                         fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
150                         if folder.Type != xaapiv1.TypeCloudSync {
151                                 fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
152                         }
153                         fmt.Fprintln(writer, "Status:\t", folder.Status)
154                         fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
155                         ds := folder.DefaultSdk
156                         if ds == "" {
157                                 ds = "-"
158                         }
159                         fmt.Fprintln(writer, "Default Sdk:\t", ds)
160
161                 } else {
162                         if first {
163                                 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
164                         }
165                         fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
166                 }
167                 first = false
168         }
169         writer.Flush()
170 }
171
172 func projectsAdd(ctx *cli.Context) error {
173
174         // Decode project type
175         var ptype xaapiv1.ProjectType
176         switch strings.ToLower(ctx.String("type")) {
177         case "pathmap", "pm":
178                 ptype = xaapiv1.TypePathMap
179         case "cloudsync", "cs":
180                 ptype = xaapiv1.TypeCloudSync
181         default:
182                 return cli.NewExitError("Unknown project type", 1)
183         }
184
185         prj := xaapiv1.ProjectConfig{
186                 ServerID:   XdsServerIDGet(),
187                 Label:      ctx.String("label"),
188                 Type:       ptype,
189                 ClientPath: ctx.String("path"),
190                 ServerPath: ctx.String("server-path"),
191         }
192
193         Log.Infof("POST /project %v", prj)
194         newPrj := xaapiv1.ProjectConfig{}
195         err := HTTPCli.Post("/projects", prj, &newPrj)
196         if err != nil {
197                 return cli.NewExitError(err, 1)
198         }
199
200         fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
201
202         return nil
203 }
204
205 func projectsRemove(ctx *cli.Context) error {
206         var res xaapiv1.ProjectConfig
207         id := GetID(ctx)
208         if id == "" {
209                 return cli.NewExitError("id parameter or option must be set", 1)
210         }
211
212         if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
213                 return cli.NewExitError(err, 1)
214         }
215
216         fmt.Println("Project ID " + res.ID + " successfully deleted.")
217         return nil
218 }
219
220 func projectsSync(ctx *cli.Context) error {
221         id := GetID(ctx)
222         if id == "" {
223                 return cli.NewExitError("id parameter or option must be set", 1)
224         }
225         if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
226                 return cli.NewExitError(err, 1)
227         }
228         fmt.Println("Sync successfully resquested.")
229         return nil
230 }