Disable completion (not working well)
[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.BoolFlag{
55                                                 Name:  "short, s",
56                                                 Usage: "short output, only print create project id (useful from scripting)",
57                                         },
58                                         cli.StringFlag{
59                                                 Name:  "type, t",
60                                                 Usage: "project type (pathmap|pm, cloudsync|cs)",
61                                         },
62                                 },
63                         },
64                         {
65                                 Name:   "get",
66                                 Usage:  "Get properties of a project",
67                                 Action: projectsGet,
68                                 Flags: []cli.Flag{
69                                         cli.StringFlag{
70                                                 Name:   "id",
71                                                 Usage:  "project id",
72                                                 EnvVar: "XDS_PROJECT_ID",
73                                         },
74                                 },
75                         },
76                         {
77                                 Name:    "list",
78                                 Aliases: []string{"ls"},
79                                 Usage:   "List existing projects",
80                                 Action:  projectsList,
81                                 Flags: []cli.Flag{
82                                         cli.BoolFlag{
83                                                 Name:  "verbose, v",
84                                                 Usage: "display verbose output",
85                                         },
86                                 },
87                         },
88                         {
89                                 Name:    "remove",
90                                 Aliases: []string{"rm"},
91                                 Usage:   "Remove an existing project",
92                                 Action:  projectsRemove,
93                                 Flags: []cli.Flag{
94                                         cli.StringFlag{
95                                                 Name:   "id",
96                                                 Usage:  "project id",
97                                                 EnvVar: "XDS_PROJECT_ID",
98                                         },
99                                         cli.BoolFlag{
100                                                 Name:  "force, f",
101                                                 Usage: "remove confirmation prompt before removal",
102                                         },
103                                 },
104                         },
105                         {
106                                 Name:    "sync",
107                                 Aliases: []string{},
108                                 Usage:   "Force synchronization of project sources",
109                                 Action:  projectsSync,
110                                 Flags: []cli.Flag{
111                                         cli.StringFlag{
112                                                 Name:   "id",
113                                                 Usage:  "project id",
114                                                 EnvVar: "XDS_PROJECT_ID",
115                                         },
116                                 },
117                         },
118                 },
119         })
120 }
121
122 func projectsList(ctx *cli.Context) error {
123         // Get projects list
124         prjs := []xaapiv1.ProjectConfig{}
125         if err := ProjectsListGet(&prjs); err != nil {
126                 return cli.NewExitError(err.Error(), 1)
127         }
128         _displayProjects(prjs, ctx.Bool("verbose"))
129         return nil
130 }
131
132 func projectsGet(ctx *cli.Context) error {
133         id := GetID(ctx, "XDS_PROJECT_ID")
134         if id == "" {
135                 return cli.NewExitError("id parameter or option must be set", 1)
136         }
137         prjs := make([]xaapiv1.ProjectConfig, 1)
138         if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
139                 return cli.NewExitError(err, 1)
140         }
141         _displayProjects(prjs, true)
142         return nil
143 }
144
145 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
146         // Display result
147         first := true
148         writer := NewTableWriter()
149         for _, folder := range prjs {
150                 if verbose {
151                         if !first {
152                                 fmt.Fprintln(writer)
153                         }
154                         fmt.Fprintln(writer, "ID:\t", folder.ID)
155                         fmt.Fprintln(writer, "Label:\t", folder.Label)
156                         fmt.Fprintln(writer, "Path type:\t", folder.Type)
157                         fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
158                         if folder.Type != xaapiv1.TypeCloudSync {
159                                 fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
160                         }
161                         fmt.Fprintln(writer, "Status:\t", folder.Status)
162                         fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
163                         ds := folder.DefaultSdk
164                         if ds == "" {
165                                 ds = "-"
166                         }
167                         fmt.Fprintln(writer, "Default Sdk:\t", ds)
168
169                 } else {
170                         if first {
171                                 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
172                         }
173                         fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
174                 }
175                 first = false
176         }
177         writer.Flush()
178 }
179
180 func projectsAdd(ctx *cli.Context) error {
181
182         // Decode project type
183         var pType xaapiv1.ProjectType
184         switch strings.ToLower(ctx.String("type")) {
185         case "pathmap", "pm":
186                 pType = xaapiv1.TypePathMap
187         case "cloudsync", "cs":
188                 pType = xaapiv1.TypeCloudSync
189         default:
190                 return cli.NewExitError("Unknown project type", 1)
191         }
192
193         prj := xaapiv1.ProjectConfig{
194                 ServerID:   XdsServerIDGet(),
195                 Label:      ctx.String("label"),
196                 Type:       pType,
197                 ClientPath: ctx.String("path"),
198                 ServerPath: ctx.String("server-path"),
199         }
200
201         Log.Infof("POST /project %v", prj)
202         newPrj := xaapiv1.ProjectConfig{}
203         err := HTTPCli.Post("/projects", prj, &newPrj)
204         if err != nil {
205                 return cli.NewExitError(err, 1)
206         }
207
208         if ctx.Bool("short") {
209                 fmt.Println(newPrj.ID)
210         } else {
211                 fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
212         }
213
214         return nil
215 }
216
217 func projectsRemove(ctx *cli.Context) error {
218         var res xaapiv1.ProjectConfig
219         id := GetID(ctx, "XDS_PROJECT_ID")
220         if id == "" {
221                 return cli.NewExitError("id parameter or option must be set", 1)
222         }
223
224         if !ctx.Bool("force") {
225                 if !Confirm("Do you permanently remove project id '" + id + "' [yes/No] ? ") {
226                         return nil
227                 }
228         }
229
230         if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
231                 return cli.NewExitError(err, 1)
232         }
233
234         fmt.Println("Project ID " + res.ID + " successfully deleted.")
235         return nil
236 }
237
238 func projectsSync(ctx *cli.Context) error {
239         id := GetID(ctx, "XDS_PROJECT_ID")
240         if id == "" {
241                 return cli.NewExitError("id parameter or option must be set", 1)
242         }
243         if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
244                 return cli.NewExitError(err, 1)
245         }
246         fmt.Println("Sync successfully resquested.")
247         return nil
248 }