fc4bc2272e3561c96221477aeadc4815b82d0afe
[src/xds/xds-cli.git] / cmd-projects.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         "strings"
24
25         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/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                                         cli.BoolFlag{
96                                                 Name:  "force, f",
97                                                 Usage: "remove confirmation prompt before removal",
98                                         },
99                                 },
100                         },
101                         {
102                                 Name:    "sync",
103                                 Aliases: []string{},
104                                 Usage:   "Force synchronization of project sources",
105                                 Action:  projectsSync,
106                                 Flags: []cli.Flag{
107                                         cli.StringFlag{
108                                                 Name:   "id",
109                                                 Usage:  "project id",
110                                                 EnvVar: "XDS_PROJECT_ID",
111                                         },
112                                 },
113                         },
114                 },
115         })
116 }
117
118 func projectsList(ctx *cli.Context) error {
119         // Get projects list
120         prjs := []xaapiv1.ProjectConfig{}
121         if err := ProjectsListGet(&prjs); err != nil {
122                 return cli.NewExitError(err.Error(), 1)
123         }
124         _displayProjects(prjs, ctx.Bool("verbose"))
125         return nil
126 }
127
128 func projectsGet(ctx *cli.Context) error {
129         id := GetID(ctx)
130         if id == "" {
131                 return cli.NewExitError("id parameter or option must be set", 1)
132         }
133         prjs := make([]xaapiv1.ProjectConfig, 1)
134         if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
135                 return cli.NewExitError(err, 1)
136         }
137         _displayProjects(prjs, true)
138         return nil
139 }
140
141 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
142         // Display result
143         first := true
144         writer := NewTableWriter()
145         for _, folder := range prjs {
146                 if verbose {
147                         if !first {
148                                 fmt.Fprintln(writer)
149                         }
150                         fmt.Fprintln(writer, "ID:\t", folder.ID)
151                         fmt.Fprintln(writer, "Label:\t", folder.Label)
152                         fmt.Fprintln(writer, "Path type:\t", folder.Type)
153                         fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
154                         if folder.Type != xaapiv1.TypeCloudSync {
155                                 fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
156                         }
157                         fmt.Fprintln(writer, "Status:\t", folder.Status)
158                         fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
159                         ds := folder.DefaultSdk
160                         if ds == "" {
161                                 ds = "-"
162                         }
163                         fmt.Fprintln(writer, "Default Sdk:\t", ds)
164
165                 } else {
166                         if first {
167                                 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
168                         }
169                         fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
170                 }
171                 first = false
172         }
173         writer.Flush()
174 }
175
176 func projectsAdd(ctx *cli.Context) error {
177
178         // Decode project type
179         var ptype xaapiv1.ProjectType
180         switch strings.ToLower(ctx.String("type")) {
181         case "pathmap", "pm":
182                 ptype = xaapiv1.TypePathMap
183         case "cloudsync", "cs":
184                 ptype = xaapiv1.TypeCloudSync
185         default:
186                 return cli.NewExitError("Unknown project type", 1)
187         }
188
189         prj := xaapiv1.ProjectConfig{
190                 ServerID:   XdsServerIDGet(),
191                 Label:      ctx.String("label"),
192                 Type:       ptype,
193                 ClientPath: ctx.String("path"),
194                 ServerPath: ctx.String("server-path"),
195         }
196
197         Log.Infof("POST /project %v", prj)
198         newPrj := xaapiv1.ProjectConfig{}
199         err := HTTPCli.Post("/projects", prj, &newPrj)
200         if err != nil {
201                 return cli.NewExitError(err, 1)
202         }
203
204         fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
205
206         return nil
207 }
208
209 func projectsRemove(ctx *cli.Context) error {
210         var res xaapiv1.ProjectConfig
211         id := GetID(ctx)
212         if id == "" {
213                 return cli.NewExitError("id parameter or option must be set", 1)
214         }
215
216         if !ctx.Bool("force") {
217                 if !Confirm("Do you permanently remove project id '" + id + "' [yes/No] ? ") {
218                         return nil
219                 }
220         }
221
222         if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
223                 return cli.NewExitError(err, 1)
224         }
225
226         fmt.Println("Project ID " + res.ID + " successfully deleted.")
227         return nil
228 }
229
230 func projectsSync(ctx *cli.Context) error {
231         id := GetID(ctx)
232         if id == "" {
233                 return cli.NewExitError("id parameter or option must be set", 1)
234         }
235         if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
236                 return cli.NewExitError(err, 1)
237         }
238         fmt.Println("Sync successfully resquested.")
239         return nil
240 }