summaryrefslogtreecommitdiffstats
path: root/cmd-projects.go
blob: 9a113db21b73c237aca9a58814b097b06001efe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main

import (
	"fmt"
	"strings"

	"github.com/iotbzh/xds-agent/lib/apiv1"
	"github.com/urfave/cli"
)

func initCmdProjects(cmdDef *[]cli.Command) {
	*cmdDef = append(*cmdDef, cli.Command{
		Name:     "projects",
		Aliases:  []string{"prj"},
		HideHelp: true,
		Usage:    "project commands group",
		Subcommands: []cli.Command{
			{
				Name:    "add",
				Aliases: []string{"a"},
				Usage:   "Add a new project",
				Action:  projectsAdd,
				Flags: []cli.Flag{
					cli.StringFlag{
						Name:  "label",
						Usage: "project label (free form string)",
					},
					cli.StringFlag{
						Name:  "path",
						Usage: "project local path",
					},
					cli.StringFlag{
						Name:  "server-path",
						Usage: "project server path (only used with pathmap type)",
					},
					cli.StringFlag{
						Name:  "type",
						Usage: "project type (pathmap|pm, cloudsync|sc)",
					},
				},
			},
			{
				Name:   "get",
				Usage:  "Get a property of a project",
				Action: projectsGet,
				Flags: []cli.Flag{
					cli.StringFlag{
						Name:  "id",
						Usage: "project id",
					},
				},
			},
			{
				Name:    "list",
				Aliases: []string{"ls"},
				Usage:   "List existing projects",
				Action:  projectsList,
				Flags: []cli.Flag{
					cli.BoolFlag{
						Name:  "verbose, v",
						Usage: "display verbose output",
					},
				},
			},
			{
				Name:    "remove",
				Aliases: []string{"rm"},
				Usage:   "Remove an existing project",
				Action:  projectsRemove,
				Flags: []cli.Flag{
					cli.StringFlag{
						Name:  "id",
						Usage: "project id",
					},
				},
			},
			{
				Name:    "sync",
				Aliases: []string{},
				Usage:   "Force synchronization of project sources",
				Action:  projectsSync,
				Flags: []cli.Flag{
					cli.StringFlag{
						Name:  "id",
						Usage: "project id",
					},
				},
			},
		},
	})
}

func projectsList(ctx *cli.Context) error {
	// Get projects list
	prjs := []apiv1.ProjectConfig{}
	if err := ProjectsListGet(&prjs); err != nil {
		return cli.NewExitError(err.Error(), 1)
	}
	_displayProjects(prjs, ctx.Bool("verbose"))
	return nil
}

func projectsGet(ctx *cli.Context) error {
	id := GetID(ctx)
	if id == "" {
		return cli.NewExitError("id parameter or option must be set", 1)
	}
	prjs := make([]apiv1.ProjectConfig, 1)
	if err := HTTPCli.Get("/projects/"+id, &prjs[0]); err != nil {
		return cli.NewExitError(err, 1)
	}
	_displayProjects(prjs, true)
	return nil
}

func _displayProjects(prjs []apiv1.ProjectConfig, verbose bool) {
	// Display result
	first := true
	writer := NewTableWriter()
	for _, folder := range prjs {
		if verbose {
			if !first {
				fmt.Fprintln(writer)
			}
			fmt.Fprintln(writer, "ID:\t", folder.ID)
			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 {
				fmt.Fprintln(writer, "Server Path:\t", folder.ServerPath)
			}
			fmt.Fprintln(writer, "Status:\t", folder.Status)
			fmt.Fprintln(writer, "Is in Sync:\t", folder.IsInSync)
			ds := folder.DefaultSdk
			if ds == "" {
				ds = "-"
			}
			fmt.Fprintln(writer, "Default Sdk:\t", ds)

		} else {
			if first {
				fmt.Fprintln(writer, "ID\t Label\t LocalPath")
			}
			fmt.Fprintln(writer, folder.ID, "\t", folder.Label, "\t", folder.ClientPath)
		}
		first = false
	}
	writer.Flush()
}

func projectsAdd(ctx *cli.Context) error {

	// Decode project type
	var ptype apiv1.ProjectType
	switch strings.ToLower(ctx.String("type")) {
	case "pathmap", "pm":
		ptype = apiv1.TypePathMap
	case "cloudsync", "cs":
		ptype = apiv1.TypeCloudSync
	default:
		return cli.NewExitError("Unknown project type", 1)
	}

	prj := apiv1.ProjectConfig{
		ServerID:   XdsServerIDGet(),
		Label:      ctx.String("label"),
		Type:       ptype,
		ClientPath: ctx.String("path"),
		ServerPath: ctx.String("server-path"),
	}

	Log.Infof("POST /project %v", prj)
	newPrj := apiv1.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)

	return nil
}

func projectsRemove(ctx *cli.Context) error {
	var res apiv1.ProjectConfig
	id := GetID(ctx)
	if id == "" {
		return cli.NewExitError("id parameter or option must be set", 1)
	}

	if err := HTTPCli.Delete("/projects/"+id, &res); err != nil {
		return cli.NewExitError(err, 1)
	}

	fmt.Println("Project ID " + res.ID + " successfully deleted.")
	return nil
}

func projectsSync(ctx *cli.Context) error {
	id := GetID(ctx)
	if id == "" {
		return cli.NewExitError("id parameter or option must be set", 1)
	}
	if err := HTTPCli.Post("/projects/sync/"+id, "", nil); err != nil {
		return cli.NewExitError(err, 1)
	}
	fmt.Println("Sync successfully resquested.")
	return nil
}