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",
73 Aliases: []string{"ls"},
74 Usage: "List existing projects",
79 Usage: "display verbose output",
85 Aliases: []string{"rm"},
86 Usage: "Remove an existing project",
87 Action: projectsRemove,
98 Usage: "Force synchronization of project sources",
111 func projectsList(ctx *cli.Context) error {
113 prjs := []xaapiv1.ProjectConfig{}
114 if err := ProjectsListGet(&prjs); err != nil {
115 return cli.NewExitError(err.Error(), 1)
117 _displayProjects(prjs, ctx.Bool("verbose"))
121 func projectsGet(ctx *cli.Context) error {
124 return cli.NewExitError("id parameter or option must be set", 1)
126 prjs := make([]xaapiv1.ProjectConfig, 1)
127 if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
128 return cli.NewExitError(err, 1)
130 _displayProjects(prjs, true)
134 func _displayProjects(prjs []xaapiv1.ProjectConfig, verbose bool) {
137 writer := NewTableWriter()
138 for _, folder := range prjs {
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)
150 fmt.Fprintln(writer, "Status:\t", folder.Status)
151 fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
152 ds := folder.DefaultSdk
156 fmt.Fprintln(writer, "Default Sdk:\t", ds)
160 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
162 fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
169 func projectsAdd(ctx *cli.Context) error {
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
179 return cli.NewExitError("Unknown project type", 1)
182 prj := xaapiv1.ProjectConfig{
183 ServerID: XdsServerIDGet(),
184 Label: ctx.String("label"),
186 ClientPath: ctx.String("path"),
187 ServerPath: ctx.String("server-path"),
190 Log.Infof("POST /project %v", prj)
191 newPrj := xaapiv1.ProjectConfig{}
192 err := HTTPCli.Post("/projects", prj, &newPrj)
194 return cli.NewExitError(err, 1)
197 fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
202 func projectsRemove(ctx *cli.Context) error {
203 var res xaapiv1.ProjectConfig
206 return cli.NewExitError("id parameter or option must be set", 1)
209 if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
210 return cli.NewExitError(err, 1)
213 fmt.Println("Project ID " + res.ID + " successfully deleted.")
217 func projectsSync(ctx *cli.Context) error {
220 return cli.NewExitError("id parameter or option must be set", 1)
222 if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
223 return cli.NewExitError(err, 1)
225 fmt.Println("Sync successfully resquested.")