Update .gitreview file
[src/xds/xds-cli.git] / cmd-projects.go
index 9a113db..fd46d9a 100644 (file)
@@ -1,10 +1,28 @@
+/*
+ * Copyright (C) 2017-2018 "IoT.bzh"
+ * Author Sebastien Douheret <sebastien@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
 package main
 
 import (
        "fmt"
        "strings"
 
-       "github.com/iotbzh/xds-agent/lib/apiv1"
+       "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
        "github.com/urfave/cli"
 )
 
@@ -22,31 +40,36 @@ func initCmdProjects(cmdDef *[]cli.Command) {
                                Action:  projectsAdd,
                                Flags: []cli.Flag{
                                        cli.StringFlag{
-                                               Name:  "label",
+                                               Name:  "label, l",
                                                Usage: "project label (free form string)",
                                        },
                                        cli.StringFlag{
-                                               Name:  "path",
+                                               Name:  "path, p",
                                                Usage: "project local path",
                                        },
                                        cli.StringFlag{
-                                               Name:  "server-path",
+                                               Name:  "server-path, sp",
                                                Usage: "project server path (only used with pathmap type)",
                                        },
+                                       cli.BoolFlag{
+                                               Name:  "short, s",
+                                               Usage: "short output, only print create project id (useful from scripting)",
+                                       },
                                        cli.StringFlag{
-                                               Name:  "type",
-                                               Usage: "project type (pathmap|pm, cloudsync|sc)",
+                                               Name:  "type, t",
+                                               Usage: "project type (pathmap|pm, cloudsync|cs)",
                                        },
                                },
                        },
                        {
                                Name:   "get",
-                               Usage:  "Get a property of a project",
+                               Usage:  "Get properties of a project",
                                Action: projectsGet,
                                Flags: []cli.Flag{
                                        cli.StringFlag{
-                                               Name:  "id",
-                                               Usage: "project id",
+                                               Name:   "id",
+                                               Usage:  "project id",
+                                               EnvVar: "XDS_PROJECT_ID",
                                        },
                                },
                        },
@@ -69,8 +92,13 @@ func initCmdProjects(cmdDef *[]cli.Command) {
                                Action:  projectsRemove,
                                Flags: []cli.Flag{
                                        cli.StringFlag{
-                                               Name:  "id",
-                                               Usage: "project id",
+                                               Name:   "id",
+                                               Usage:  "project id",
+                                               EnvVar: "XDS_PROJECT_ID",
+                                       },
+                                       cli.BoolFlag{
+                                               Name:  "force, f",
+                                               Usage: "remove confirmation prompt before removal",
                                        },
                                },
                        },
@@ -81,8 +109,9 @@ func initCmdProjects(cmdDef *[]cli.Command) {
                                Action:  projectsSync,
                                Flags: []cli.Flag{
                                        cli.StringFlag{
-                                               Name:  "id",
-                                               Usage: "project id",
+                                               Name:   "id",
+                                               Usage:  "project id",
+                                               EnvVar: "XDS_PROJECT_ID",
                                        },
                                },
                        },
@@ -92,7 +121,7 @@ func initCmdProjects(cmdDef *[]cli.Command) {
 
 func projectsList(ctx *cli.Context) error {
        // Get projects list
-       prjs := []apiv1.ProjectConfig{}
+       prjs := []xaapiv1.ProjectConfig{}
        if err := ProjectsListGet(&prjs); err != nil {
                return cli.NewExitError(err.Error(), 1)
        }
@@ -101,11 +130,11 @@ func projectsList(ctx *cli.Context) error {
 }
 
 func projectsGet(ctx *cli.Context) error {
-       id := GetID(ctx)
+       id := GetID(ctx, "XDS_PROJECT_ID")
        if id == "" {
                return cli.NewExitError("id parameter or option must be set", 1)
        }
-       prjs := make([]apiv1.ProjectConfig, 1)
+       prjs := make([]xaapiv1.ProjectConfig, 1)
        if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
                return cli.NewExitError(err, 1)
        }
@@ -113,7 +142,7 @@ func projectsGet(ctx *cli.Context) error {
        return nil
 }
 
-func _displayProjects(prjs []apiv1.ProjectConfig, verbose bool) {
+func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
        // Display result
        first := true
        writer := NewTableWriter()
@@ -126,7 +155,7 @@ func _displayProjects(prjs []apiv1.ProjectConfig, verbose bool) {
                        fmt.Fprintln(writer, "Label:\t", folder.Label)
                        fmt.Fprintln(writer, "Path type:\t", folder.Type)
                        fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
-                       if folder.Type != apiv1.TypeCloudSync {
+                       if folder.Type != xaapiv1.TypeCloudSync {
                                fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
                        }
                        fmt.Fprintln(writer, "Status:\t", folder.Status)
@@ -151,43 +180,53 @@ func _displayProjects(prjs []apiv1.ProjectConfig, verbose bool) {
 func projectsAdd(ctx *cli.Context) error {
 
        // Decode project type
-       var ptype apiv1.ProjectType
+       var pType xaapiv1.ProjectType
        switch strings.ToLower(ctx.String("type")) {
        case "pathmap", "pm":
-               ptype = apiv1.TypePathMap
+               pType = xaapiv1.TypePathMap
        case "cloudsync", "cs":
-               ptype = apiv1.TypeCloudSync
+               pType = xaapiv1.TypeCloudSync
        default:
                return cli.NewExitError("Unknown project type", 1)
        }
 
-       prj := apiv1.ProjectConfig{
+       prj := xaapiv1.ProjectConfig{
                ServerID:   XdsServerIDGet(),
                Label:      ctx.String("label"),
-               Type:       ptype,
+               Type:       pType,
                ClientPath: ctx.String("path"),
                ServerPath: ctx.String("server-path"),
        }
 
        Log.Infof("POST /project %v", prj)
-       newPrj := apiv1.ProjectConfig{}
+       newPrj := xaapiv1.ProjectConfig{}
        err := HTTPCli.Post("/projects", prj, &newPrj)
        if err != nil {
                return cli.NewExitError(err, 1)
        }
 
-       fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
+       if ctx.Bool("short") {
+               fmt.Println(newPrj.ID)
+       } else {
+               fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
+       }
 
        return nil
 }
 
 func projectsRemove(ctx *cli.Context) error {
-       var res apiv1.ProjectConfig
-       id := GetID(ctx)
+       var res xaapiv1.ProjectConfig
+       id := GetID(ctx, "XDS_PROJECT_ID")
        if id == "" {
                return cli.NewExitError("id parameter or option must be set", 1)
        }
 
+       if !ctx.Bool("force") {
+               if !Confirm("Do you permanently remove project id '" + id + "' [yes/No] ? ") {
+                       return nil
+               }
+       }
+
        if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
                return cli.NewExitError(err, 1)
        }
@@ -197,7 +236,7 @@ func projectsRemove(ctx *cli.Context) error {
 }
 
 func projectsSync(ctx *cli.Context) error {
-       id := GetID(ctx)
+       id := GetID(ctx, "XDS_PROJECT_ID")
        if id == "" {
                return cli.NewExitError("id parameter or option must be set", 1)
        }