Initial commit
[src/xds/xds-cli.git] / cmd-projects.go
1 package main
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/iotbzh/xds-agent/lib/apiv1"
8         "github.com/urfave/cli"
9 )
10
11 func initCmdProjects(cmdDef *[]cli.Command) {
12         *cmdDef = append(*cmdDef, cli.Command{
13                 Name:     "projects",
14                 Aliases:  []string{"prj"},
15                 HideHelp: true,
16                 Usage:    "project commands group",
17                 Subcommands: []cli.Command{
18                         {
19                                 Name:    "add",
20                                 Aliases: []string{"a"},
21                                 Usage:   "Add a new project",
22                                 Action:  projectsAdd,
23                                 Flags: []cli.Flag{
24                                         cli.StringFlag{
25                                                 Name:  "label",
26                                                 Usage: "project label (free form string)",
27                                         },
28                                         cli.StringFlag{
29                                                 Name:  "path",
30                                                 Usage: "project local path",
31                                         },
32                                         cli.StringFlag{
33                                                 Name:  "server-path",
34                                                 Usage: "project server path (only used with pathmap type)",
35                                         },
36                                         cli.StringFlag{
37                                                 Name:  "type",
38                                                 Usage: "project type (pathmap|pm, cloudsync|sc)",
39                                         },
40                                 },
41                         },
42                         {
43                                 Name:   "get",
44                                 Usage:  "Get a property of a project",
45                                 Action: projectsGet,
46                                 Flags: []cli.Flag{
47                                         cli.StringFlag{
48                                                 Name:  "id",
49                                                 Usage: "project id",
50                                         },
51                                 },
52                         },
53                         {
54                                 Name:    "list",
55                                 Aliases: []string{"ls"},
56                                 Usage:   "List existing projects",
57                                 Action:  projectsList,
58                                 Flags: []cli.Flag{
59                                         cli.BoolFlag{
60                                                 Name:  "verbose, v",
61                                                 Usage: "display verbose output",
62                                         },
63                                 },
64                         },
65                         {
66                                 Name:    "remove",
67                                 Aliases: []string{"rm"},
68                                 Usage:   "Remove an existing project",
69                                 Action:  projectsRemove,
70                                 Flags: []cli.Flag{
71                                         cli.StringFlag{
72                                                 Name:  "id",
73                                                 Usage: "project id",
74                                         },
75                                 },
76                         },
77                         {
78                                 Name:    "sync",
79                                 Aliases: []string{},
80                                 Usage:   "Force synchronization of project sources",
81                                 Action:  projectsSync,
82                                 Flags: []cli.Flag{
83                                         cli.StringFlag{
84                                                 Name:  "id",
85                                                 Usage: "project id",
86                                         },
87                                 },
88                         },
89                 },
90         })
91 }
92
93 func projectsList(ctx *cli.Context) error {
94         // Get projects list
95         prjs := []apiv1.ProjectConfig{}
96         if err := ProjectsListGet(&prjs); err != nil {
97                 return cli.NewExitError(err.Error(), 1)
98         }
99         _displayProjects(prjs, ctx.Bool("verbose"))
100         return nil
101 }
102
103 func projectsGet(ctx *cli.Context) error {
104         id := GetID(ctx)
105         if id == "" {
106                 return cli.NewExitError("id parameter or option must be set", 1)
107         }
108         prjs := make([]apiv1.ProjectConfig, 1)
109         if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
110                 return cli.NewExitError(err, 1)
111         }
112         _displayProjects(prjs, true)
113         return nil
114 }
115
116 func _displayProjects(prjs []apiv1.ProjectConfig, verbose bool) {
117         // Display result
118         first := true
119         writer := NewTableWriter()
120         for _, folder := range prjs {
121                 if verbose {
122                         if !first {
123                                 fmt.Fprintln(writer)
124                         }
125                         fmt.Fprintln(writer, "ID:\t", folder.ID)
126                         fmt.Fprintln(writer, "Label:\t", folder.Label)
127                         fmt.Fprintln(writer, "Path type:\t", folder.Type)
128                         fmt.Fprintln(writer, "Local Path:\t", folder.ClientPath)
129                         if folder.Type != apiv1.TypeCloudSync {
130                                 fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
131                         }
132                         fmt.Fprintln(writer, "Status:\t", folder.Status)
133                         fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
134                         ds := folder.DefaultSdk
135                         if ds == "" {
136                                 ds = "-"
137                         }
138                         fmt.Fprintln(writer, "Default Sdk:\t", ds)
139
140                 } else {
141                         if first {
142                                 fmt.Fprintln(writer, "ID\t Label\t LocalPath")
143                         }
144                         fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
145                 }
146                 first = false
147         }
148         writer.Flush()
149 }
150
151 func projectsAdd(ctx *cli.Context) error {
152
153         // Decode project type
154         var ptype apiv1.ProjectType
155         switch strings.ToLower(ctx.String("type")) {
156         case "pathmap", "pm":
157                 ptype = apiv1.TypePathMap
158         case "cloudsync", "cs":
159                 ptype = apiv1.TypeCloudSync
160         default:
161                 return cli.NewExitError("Unknown project type", 1)
162         }
163
164         prj := apiv1.ProjectConfig{
165                 ServerID:   XdsServerIDGet(),
166                 Label:      ctx.String("label"),
167                 Type:       ptype,
168                 ClientPath: ctx.String("path"),
169                 ServerPath: ctx.String("server-path"),
170         }
171
172         Log.Infof("POST /project %v", prj)
173         newPrj := apiv1.ProjectConfig{}
174         err := HTTPCli.Post("/projects", prj, &newPrj)
175         if err != nil {
176                 return cli.NewExitError(err, 1)
177         }
178
179         fmt.Printf("New project '%s' (id %v) successfully created.\n", newPrj.Label, newPrj.ID)
180
181         return nil
182 }
183
184 func projectsRemove(ctx *cli.Context) error {
185         var res apiv1.ProjectConfig
186         id := GetID(ctx)
187         if id == "" {
188                 return cli.NewExitError("id parameter or option must be set", 1)
189         }
190
191         if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
192                 return cli.NewExitError(err, 1)
193         }
194
195         fmt.Println("Project ID " + res.ID + " successfully deleted.")
196         return nil
197 }
198
199 func projectsSync(ctx *cli.Context) error {
200         id := GetID(ctx)
201         if id == "" {
202                 return cli.NewExitError("id parameter or option must be set", 1)
203         }
204         if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
205                 return cli.NewExitError(err, 1)
206         }
207         fmt.Println("Sync successfully resquested.")
208         return nil
209 }