2 * Copyright (C) 2017 "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 "github.com/iotbzh/xds-agent/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",
100 Usage: "Force synchronization of project sources",
101 Action: projectsSync,
106 EnvVar: "XDS_PROJECT_ID",
114 func projectsList(ctx *cli.Context) error {
116 prjs := []xaapiv1.ProjectConfig{}
117 if err := ProjectsListGet(&prjs); err != nil {
118 return cli.NewExitError(err.Error(), 1)
120 _displayProjects(prjs, ctx.Bool("verbose"))
124 func projectsGet(ctx *cli.Context) error {
127 return cli.NewExitError("id parameter or option must be set", 1)
129 prjs := make([]xaapiv1.ProjectConfig, 1)
130 if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
131 return cli.NewExitError(err, 1)
133 _displayProjects(prjs, true)
137 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
140 writer := NewTableWriter()
141 for _, folder := range prjs {
146 fmt.Fprintln(writer, "ID:\t", folder.ID)
147 fmt.Fprintln(writer, "Label:\t", folder.Label)
148 fmt.Fprintln(writer, "Path type:\t", folder.Type)
149 fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
150 if folder.Type != xaapiv1.TypeCloudSync {
151 fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
153 fmt.Fprintln(writer, "Status:\t", folder.Status)
154 fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
155 ds := folder.DefaultSdk
159 fmt.Fprintln(writer, "Default Sdk:\t", ds)
163 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
165 fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
172 func projectsAdd(ctx *cli.Context) error {
174 // Decode project type
175 var ptype xaapiv1.ProjectType
176 switch strings.ToLower(ctx.String("type")) {
177 case "pathmap", "pm":
178 ptype = xaapiv1.TypePathMap
179 case "cloudsync", "cs":
180 ptype = xaapiv1.TypeCloudSync
182 return cli.NewExitError("Unknown project type", 1)
185 prj := xaapiv1.ProjectConfig{
186 ServerID: XdsServerIDGet(),
187 Label: ctx.String("label"),
189 ClientPath: ctx.String("path"),
190 ServerPath: ctx.String("server-path"),
193 Log.Infof("POST /project %v", prj)
194 newPrj := xaapiv1.ProjectConfig{}
195 err := HTTPCli.Post("/projects", prj, &newPrj)
197 return cli.NewExitError(err, 1)
200 fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
205 func projectsRemove(ctx *cli.Context) error {
206 var res xaapiv1.ProjectConfig
209 return cli.NewExitError("id parameter or option must be set", 1)
212 if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
213 return cli.NewExitError(err, 1)
216 fmt.Println("Project ID " + res.ID + " successfully deleted.")
220 func projectsSync(ctx *cli.Context) error {
223 return cli.NewExitError("id parameter or option must be set", 1)
225 if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
226 return cli.NewExitError(err, 1)
228 fmt.Println("Sync successfully resquested.")