13a71268669ab0faa003e8c69d8b47dfdf71fba8
[src/xds/xds-cli.git] / cmd-projects.go
1 /*
2  * Copyright (C) 2017 "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         "github.com/iotbzh/xds-agent/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                                         },
69                                 },
70                         },
71                         {
72                                 Name:    "list",
73                                 Aliases: []string{"ls"},
74                                 Usage:   "List existing projects",
75                                 Action:  projectsList,
76                                 Flags: []cli.Flag{
77                                         cli.BoolFlag{
78                                                 Name:  "verbose, v",
79                                                 Usage: "display verbose output",
80                                         },
81                                 },
82                         },
83                         {
84                                 Name:    "remove",
85                                 Aliases: []string{"rm"},
86                                 Usage:   "Remove an existing project",
87                                 Action:  projectsRemove,
88                                 Flags: []cli.Flag{
89                                         cli.StringFlag{
90                                                 Name:  "id",
91                                                 Usage: "project id",
92                                         },
93                                 },
94                         },
95                         {
96                                 Name:    "sync",
97                                 Aliases: []string{},
98                                 Usage:   "Force synchronization of project sources",
99                                 Action:  projectsSync,
100                                 Flags: []cli.Flag{
101                                         cli.StringFlag{
102                                                 Name:  "id",
103                                                 Usage: "project id",
104                                         },
105                                 },
106                         },
107                 },
108         })
109 }
110
111 func projectsList(ctx *cli.Context) error {
112         // Get projects list
113         prjs := []xaapiv1.ProjectConfig{}
114         if err := ProjectsListGet(&prjs); err != nil {
115                 return cli.NewExitError(err.Error(), 1)
116         }
117         _displayProjects(prjs, ctx.Bool("verbose"))
118         return nil
119 }
120
121 func projectsGet(ctx *cli.Context) error {
122         id := GetID(ctx)
123         if id == "" {
124                 return cli.NewExitError("id parameter or option must be set", 1)
125         }
126         prjs := make([]xaapiv1.ProjectConfig, 1)
127         if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
128                 return cli.NewExitError(err, 1)
129         }
130         _displayProjects(prjs, true)
131         return nil
132 }
133
134 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
135         // Display result
136         first := true
137         writer := NewTableWriter()
138         for _, folder := range prjs {
139                 if verbose {
140                         if !first {
141                                 fmt.Fprintln(writer)
142                         }
143                         fmt.Fprintln(writer, "ID:\t", folder.ID)
144                         fmt.Fprintln(writer, "Label:\t", folder.Label)
145                         fmt.Fprintln(writer, "Path type:\t", folder.Type)
146                         fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
147                         if folder.Type != xaapiv1.TypeCloudSync {
148                                 fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
149                         }
150                         fmt.Fprintln(writer, "Status:\t", folder.Status)
151                         fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
152                         ds := folder.DefaultSdk
153                         if ds == "" {
154                                 ds = "-"
155                         }
156                         fmt.Fprintln(writer, "Default Sdk:\t", ds)
157
158                 } else {
159                         if first {
160                                 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
161                         }
162                         fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
163                 }
164                 first = false
165         }
166         writer.Flush()
167 }
168
169 func projectsAdd(ctx *cli.Context) error {
170
171         // Decode project type
172         var ptype xaapiv1.ProjectType
173         switch strings.ToLower(ctx.String("type")) {
174         case "pathmap", "pm":
175                 ptype = xaapiv1.TypePathMap
176         case "cloudsync", "cs":
177                 ptype = xaapiv1.TypeCloudSync
178         default:
179                 return cli.NewExitError("Unknown project type", 1)
180         }
181
182         prj := xaapiv1.ProjectConfig{
183                 ServerID:   XdsServerIDGet(),
184                 Label:      ctx.String("label"),
185                 Type:       ptype,
186                 ClientPath: ctx.String("path"),
187                 ServerPath: ctx.String("server-path"),
188         }
189
190         Log.Infof("POST /project %v", prj)
191         newPrj := xaapiv1.ProjectConfig{}
192         err := HTTPCli.Post("/projects", prj, &newPrj)
193         if err != nil {
194                 return cli.NewExitError(err, 1)
195         }
196
197         fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
198
199         return nil
200 }
201
202 func projectsRemove(ctx *cli.Context) error {
203         var res xaapiv1.ProjectConfig
204         id := GetID(ctx)
205         if id == "" {
206                 return cli.NewExitError("id parameter or option must be set", 1)
207         }
208
209         if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
210                 return cli.NewExitError(err, 1)
211         }
212
213         fmt.Println("Project ID " + res.ID + " successfully deleted.")
214         return nil
215 }
216
217 func projectsSync(ctx *cli.Context) error {
218         id := GetID(ctx)
219         if id == "" {
220                 return cli.NewExitError("id parameter or option must be set", 1)
221         }
222         if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
223                 return cli.NewExitError(err, 1)
224         }
225         fmt.Println("Sync successfully resquested.")
226         return nil
227 }