2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
26 "github.com/urfave/cli"
29 func initCmdProjects(cmdDef *[]cli.Command) {
30 *cmdDef = append(*cmdDef, cli.Command{
32 Aliases: []string{"prj"},
34 Usage: "project commands group",
35 Subcommands: []cli.Command{
38 Aliases: []string{"a"},
39 Usage: "Add a new project",
44 Usage: "project label (free form string)",
48 Usage: "project local path",
51 Name: "server-path, sp",
52 Usage: "project server path (only used with pathmap type)",
56 Usage: "short output, only print create project id (useful from scripting)",
60 Usage: "project type (pathmap|pm, cloudsync|cs)",
66 Usage: "Get properties of a project",
72 EnvVar: "XDS_PROJECT_ID",
78 Aliases: []string{"ls"},
79 Usage: "List existing projects",
84 Usage: "display verbose output",
90 Aliases: []string{"rm"},
91 Usage: "Remove an existing project",
92 Action: projectsRemove,
97 EnvVar: "XDS_PROJECT_ID",
101 Usage: "remove confirmation prompt before removal",
108 Usage: "Force synchronization of project sources",
109 Action: projectsSync,
114 EnvVar: "XDS_PROJECT_ID",
122 func projectsList(ctx *cli.Context) error {
124 prjs := []xaapiv1.ProjectConfig{}
125 if err := ProjectsListGet(&prjs); err != nil {
126 return cli.NewExitError(err.Error(), 1)
128 _displayProjects(prjs, ctx.Bool("verbose"))
132 func projectsGet(ctx *cli.Context) error {
133 id := GetID(ctx, "XDS_PROJECT_ID")
135 return cli.NewExitError("id parameter or option must be set", 1)
137 prjs := make([]xaapiv1.ProjectConfig, 1)
138 if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
139 return cli.NewExitError(err, 1)
141 _displayProjects(prjs, true)
145 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
148 writer := NewTableWriter()
149 for _, folder := range prjs {
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)
161 fmt.Fprintln(writer, "Status:\t", folder.Status)
162 fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
163 ds := folder.DefaultSdk
167 fmt.Fprintln(writer, "Default Sdk:\t", ds)
171 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
173 fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
180 func projectsAdd(ctx *cli.Context) error {
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
190 return cli.NewExitError("Unknown project type", 1)
193 prj := xaapiv1.ProjectConfig{
194 ServerID: XdsServerIDGet(),
195 Label: ctx.String("label"),
197 ClientPath: ctx.String("path"),
198 ServerPath: ctx.String("server-path"),
201 Log.Infof("POST /project %v", prj)
202 newPrj := xaapiv1.ProjectConfig{}
203 err := HTTPCli.Post("/projects", prj, &newPrj)
205 return cli.NewExitError(err, 1)
208 if ctx.Bool("short") {
209 fmt.Println(newPrj.ID)
211 fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
217 func projectsRemove(ctx *cli.Context) error {
218 var res xaapiv1.ProjectConfig
219 id := GetID(ctx, "XDS_PROJECT_ID")
221 return cli.NewExitError("id parameter or option must be set", 1)
224 if !ctx.Bool("force") {
225 if !Confirm("Do you permanently remove project id '" + id + "' [yes/No] ? ") {
230 if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
231 return cli.NewExitError(err, 1)
234 fmt.Println("Project ID " + res.ID + " successfully deleted.")
238 func projectsSync(ctx *cli.Context) error {
239 id := GetID(ctx, "XDS_PROJECT_ID")
241 return cli.NewExitError("id parameter or option must be set", 1)
243 if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
244 return cli.NewExitError(err, 1)
246 fmt.Println("Sync successfully resquested.")