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: "project type (pathmap|pm, cloudsync|sc)",
62 Usage: "Get a property of a project",
68 EnvVar: "XDS_PROJECT_ID",
74 Aliases: []string{"ls"},
75 Usage: "List existing projects",
80 Usage: "display verbose output",
86 Aliases: []string{"rm"},
87 Usage: "Remove an existing project",
88 Action: projectsRemove,
93 EnvVar: "XDS_PROJECT_ID",
97 Usage: "remove confirmation prompt before removal",
104 Usage: "Force synchronization of project sources",
105 Action: projectsSync,
110 EnvVar: "XDS_PROJECT_ID",
118 func projectsList(ctx *cli.Context) error {
120 prjs := []xaapiv1.ProjectConfig{}
121 if err := ProjectsListGet(&prjs); err != nil {
122 return cli.NewExitError(err.Error(), 1)
124 _displayProjects(prjs, ctx.Bool("verbose"))
128 func projectsGet(ctx *cli.Context) error {
131 return cli.NewExitError("id parameter or option must be set", 1)
133 prjs := make([]xaapiv1.ProjectConfig, 1)
134 if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
135 return cli.NewExitError(err, 1)
137 _displayProjects(prjs, true)
141 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
144 writer := NewTableWriter()
145 for _, folder := range prjs {
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)
157 fmt.Fprintln(writer, "Status:\t", folder.Status)
158 fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
159 ds := folder.DefaultSdk
163 fmt.Fprintln(writer, "Default Sdk:\t", ds)
167 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
169 fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
176 func projectsAdd(ctx *cli.Context) error {
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
186 return cli.NewExitError("Unknown project type", 1)
189 prj := xaapiv1.ProjectConfig{
190 ServerID: XdsServerIDGet(),
191 Label: ctx.String("label"),
193 ClientPath: ctx.String("path"),
194 ServerPath: ctx.String("server-path"),
197 Log.Infof("POST /project %v", prj)
198 newPrj := xaapiv1.ProjectConfig{}
199 err := HTTPCli.Post("/projects", prj, &newPrj)
201 return cli.NewExitError(err, 1)
204 fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
209 func projectsRemove(ctx *cli.Context) error {
210 var res xaapiv1.ProjectConfig
213 return cli.NewExitError("id parameter or option must be set", 1)
216 if !ctx.Bool("force") {
217 if !Confirm("Do you permanently remove project id '" + id + "' [yes/No] ? ") {
222 if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
223 return cli.NewExitError(err, 1)
226 fmt.Println("Project ID " + res.ID + " successfully deleted.")
230 func projectsSync(ctx *cli.Context) error {
233 return cli.NewExitError("id parameter or option must be set", 1)
235 if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
236 return cli.NewExitError(err, 1)
238 fmt.Println("Sync successfully resquested.")