8000 feat(cli): project command improvements (#4167) · ovh/cds@34ffed0 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 34ffed0

Browse files
fsaminsguiheux
authored andcommitted
feat(cli): project command improvements (#4167)
1 parent 446f430 commit 34ffed0

File tree

10 files changed

+103
-31
lines changed

10 files changed

+103
-31
lines changed

cli/cdsctl/project.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"os"
7+
"strings"
78

89
"github.com/spf13/cobra"
910

@@ -64,20 +65,53 @@ var projectShowCmd = cli.Command{
6465

6566
func projectShowRun(v cli.Values) (interface{}, error) {
6667
mods := []cdsclient.RequestModifier{}
67-
if v.GetString("verbose") == "true" {
68-
mods = append(mods, func(r *http.Request) {
69-
q := r.URL.Query()
70-
q.Set("withApplications", "true")
71-
q.Set("withPipelines", "true")
72-
q.Set("withEnvironments", "true")
73-
r.URL.RawQuery = q.Encode()
74-
})
75-
}
68+
mods = append(mods, func(r *http.Request) {
69+
q := r.URL.Query()
70+
q.Set("withWorkflowNames", "true")
71+
q.Set("withIntegrations", "true")
72+
r.URL.RawQuery = q.Encode()
73+
})
74+
7675
proj, err := client.ProjectGet(v.GetString(_ProjectKey), mods...)
7776
if err != nil {
7877
return nil, err
7978
}
80-
return *proj, nil
79+
80+
var p = struct {
81+
Key string `cli:"key,key"`
82+
Name string `cli:"name"`
83+
Description string `cli:"description"`
84+
Favorite bool `cli:"favorite"`
85+
URL string `cli:"url"`
86+
API string `cli:"api"`
87+
Workflows string `cli:"workflows"`
88+
NbWorkflows int `cli:"nb_workflows"`
89+
RepoManagers string `cli:"repositories_manager"`
90+
Integrations string `cli:"integration"`
91+
}{
92+
Key: proj.Key,
93+
Name: proj.Name,
94+
Description: proj.Description,
95+
Favorite: proj.Favorite,
96+
NbWorkflows: len(proj.WorkflowNames),
97+
Workflows: cli.Ellipsis(strings.Join(proj.WorkflowNames.Names(), ","), 70),
98+
URL: proj.URLs.UIURL,
99+
API: proj.URLs.APIURL,
100+
}
101+
102+
var integrations []string
103+
for _, inte := range proj.Integrations {
104+
integrations = append(integrations, inte.Name)
105+
}
106+
p.Integrations = cli.Ellipsis(strings.Join(integrations, ","), 70)
107+
108+
var repomanagers []string
109+
for _, vcs := range proj.VCSServers {
110+
repomanagers = append(repomanagers, vcs.Name)
111+
}
112+
p.RepoManagers = cli.Ellipsis(strings.Join(repomanagers, ","), 70)
113+
114+
return p, nil
81115
}
82116

83117
var projectCreateCmd = cli.Command{

cli/cobra.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ func newCommand(c Command, run interface{}, subCommands SubCommands, mods ...Com
273273
} else {
274274
fmt.Println(string(b))
275275
}
276+
276277
case "yaml":
277278
b, err := yaml.Marshal(i)
278279
ExitOnError(err)
@@ -281,22 +282,25 @@ func newCommand(c Command, run interface{}, subCommands SubCommands, mods ...Com
281282
} else {
282283
fmt.Println(string(b))
283284
}
285+
284286
default:
285287
if quiet {
286288
fmt.Println(i.(map[string]string)["key"])
287289
return
288290
}
289291
w := tabwriter.NewWriter(cmd.OutOrStdout(), 10, 0, 1, ' ', 0)
290-
e := dump.NewDefaultEncoder()
291-
e.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultLowerCaseFormatter()}
292-
e.ExtraFields.DetailedMap = false
293-
e.ExtraFields.DetailedStruct = false
294-
e.ExtraFields.Len = false
295-
e.ExtraFields.Type = false
296-
m, err := e.ToStringMap(i)
292+
m, err := dump.ToStringMap(i)
297293
ExitOnError(err)
298-
for k, v := range m {
299-
fmt.Fprintln(w, k+"\t"+v)
294+
295+
itemKeys := []string{}
296+
for k := range m {
297+
itemKeys = append(itemKeys, k)
298+
}
299+
300+
sort.Strings(itemKeys)
301+
302+
for _, k := range itemKeys {
303+
fmt.Fprintln(w, k+"\t"+m[k])
300304
}
301305
w.Flush()
302306
return

cli/display.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ func (d *Display) Do(ctx context.Context) {
4949
}
5050
}(d)
5151
}
52+
53+
func Ellipsis(s string, i int) string {
54+
if len(s) > i {
55+
return s[:i] + "…"
56+
}
57+
return s
58+
}

engine/api/application/dao.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ func LoadAll(db gorp.SqlExecutor, store cache.Store, key string, opts ...LoadOpt
208208
}
209209

210210
// LoadAllNames returns all application names
211-
func LoadAllNames(db gorp.SqlExecutor, projID int64) ([]sdk.IDName, error) {
211+
func LoadAllNames(db gorp.SqlExecutor, projID int64) (sdk.IDNames, error) {
212212
query := `
213213
SELECT application.id, application.name, application.description, application.icon
214214
FROM application
215215
WHERE application.project_id= $1
216216
ORDER BY application.name ASC`
217217

218-
var res []sdk.IDName
218+
var res sdk.IDNames
219219
if _, err := db.Select(&res, query, projID); err != nil {
220220
if err == sql.ErrNoRows {
221221
return res, nil

engine/api/pipeline/pipeline.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ func LoadPipelines(db gorp.SqlExecutor, projectID int64, loadDependencies bool)
237237
}
238238

239239
// LoadAllNames returns all pipeline names
240-
func LoadAllNames(db gorp.SqlExecutor, store cache.Store, projID int64) ([]sdk.IDName, error) {
240+
func LoadAllNames(db gorp.SqlExecutor, store cache.Store, projID int64) (sdk.IDNames, error) {
241241
query := `SELECT pipeline.id, pipeline.name, pipeline.description
242242
FROM pipeline
243243
WHERE project_id = $1
244244
ORDER BY pipeline.name`
245245

246-
var res []sdk.IDName
246+
var res sdk.IDNames
247247
if _, err := db.Select(&res, query, projID); err != nil {
248248
if err == sql.ErrNoRows {
249249
return res, nil

engine/api/project.go

Lines changed: 3 additions & 0 deletions
253
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ func (api *API) getProjectHandler() service.Handler {
253
return sdk.WrapError(errProj, "getProjectHandler (%s)", key)
254254
}
255255

256+
p.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getProjectHandler, map[string]string{"permProjectKey": key})
257+
p.URLs.UIURL = api.Config.URL.UI + "/project/" + key
258+
256259
return service.WriteJSON(w, p, http.StatusOK)
257260
}
258261
}

engine/api/project/dao_dependencies.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var (
4040

4141
loadApplicationNames = func(db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, u *sdk.User) error {
4242
var err error
43-
var apps []sdk.IDName
43+
var apps sdk.IDNames
4444

4545
if apps, err = application.LoadAllNames(db, proj.ID); err != nil {
4646
return sdk.WrapError(err, "application.loadApplications")
@@ -130,7 +130,7 @@ var (
130130

131131
loadWorkflowNames = func(db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, u *sdk.User) error {
132132
var err error
133-
var wfs []sdk.IDName
133+
var wfs sdk.IDNames
134134

135135
if wfs, err = workflow.LoadAllNames(db, proj.ID, u); err != nil {
136136
return sdk.WrapError(err, "workflow.loadworkflownames")
@@ -182,7 +182,7 @@ var (
182182

183183
loadPipelineNames = func(db gorp.SqlExecutor, store cache.Store, proj *sdk.Project, u *sdk.User) error {
184184
var err error
185-
var pips []sdk.IDName
185+
var pips sdk.IDNames
186186

187187
if pips, err = pipeline.LoadAllNames(db, store, proj.ID); err != nil {
188188
return sdk.WrapError(err, "pipeline.loadpipelinenames")

engine/api/workflow/dao.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ func LoadAll(db gorp.SqlExecutor, projectKey string) ([]sdk.Workflow, error) {
240240
}
241241

242242
// LoadAllNames loads all workflow names for a project.
243-
func LoadAllNames(db gorp.SqlExecutor, projID int64, u *sdk.User) ([]sdk.IDName, error) {
243+
func LoadAllNames(db gorp.SqlExecutor, projID int64, u *sdk.User) (sdk.IDNames, error) {
244244
query := `
245245
SELECT workflow.name, workflow.id, workflow.description, workflow.icon
246246
FROM workflow
247247
WHERE workflow.project_id = $1
248248
AND workflow.to_delete = false
249249
ORDER BY workflow.name ASC`
250250

251-
var res []sdk.IDName
251+
var res sdk.IDNames
252252
if _, err := db.Select(&res, query, projID); err != nil {
253253
if err == sql.ErrNoRows {
254254
return res, nil

sdk/common.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ type IDName struct {
4545
Labels []Label `json:"labels,omitempty" db:"-"`
4646
}
4747

48+
type IDNames []IDName
49+
50+
func (idNames IDNames) IDs() []int64 {
51+
res := make([]int64, len(idNames))
52+
for i := range idNames {
53+
res[i] = idNames[i].ID
54+
}
55+
return res
56+
}
57+
58+
func (idNames IDNames) Names() []string {
59+
res := make([]string, len(idNames))
60+
for i := range idNames {
61+
res[i] = idNames[i].Name
62+
}
63+
return res
64+
}
65+
4866
// NamePattern Pattern for project/application/pipeline/group name
4967
const NamePattern = "^[a-zA-Z0-9._-]{1,}$"
5068

sdk/project.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type Project struct {
1616
Description string `json:"description" yaml:"description" db:"description" cli:"description"`
1717
Icon string `json:"icon" yaml:"icon" db:"icon" cli:"-"`
1818
Workflows []Workflow `json:"workflows,omitempty" yaml:"workflows,omitempty" db:"-" cli:"-"`
19-
WorkflowNames []IDName `json:"workflow_names,omitempty" yaml:"workflow_names,omitempty" db:"-" cli:"-"`
19+
WorkflowNames IDNames `json:"workflow_names,omitempty" yaml:"workflow_names,omitempty" db:"-" cli:"-"`
2020
Pipelines []Pipeline `json:"pipelines,omitempty" yaml:"pipelines,omitempty" db:"-" cli:"-"`
21-
PipelineNames []IDName `json:"pipeline_names,omitempty" yaml:"pipeline_names,omitempty" db:"-" cli:"-"`
21+
PipelineNames IDNames `json:"pipeline_names,omitempty" yaml:"pipeline_names,omitempty" db:"-" cli:"-"`
2222
Applications []Application `json:"applications,omitempty" yaml:"applications,omitempty" db:"-" cli:"-"`
23-
ApplicationNames []IDName `json:"application_names,omitempty" yaml:"application_names,omitempty" db:"-" cli:"-"`
23+
ApplicationNames IDNames `json:"application_names,omitempty" yaml:"application_names,omitempty" db:"-" cli:"-"`
2424
ProjectGroups []GroupPermission `json:"groups,omitempty" yaml:"permissions,omitempty" db:"-" cli:"-"`
2525
Variable []Variable `json:"variables,omitempty" yaml:"variables,omitempty" db:"-" cli:"-"`
2626
Environments []Environment `json:"environments,omitempty" yaml:"environments,omitempty" db:"-" cli:"-"`
@@ -34,6 +34,12 @@ type Project struct {
3434
Integrations []ProjectIntegration `json:"integrations" yaml:"integrations" db:"-" cli:"-"`
3535
Features map[string]bool `json:"features" yaml:"features" db:"-" cli:"-"`
3636
Favorite bool `json:"favorite" yaml:"favorite" db:"-" cli:"favorite"`
37+
URLs URL `json:"urls" yaml:"-" db:"-" cli:"-"`
38+
}
39+
40+
type URL struct {
41+
APIURL string `json:"api_url"`
42+
UIURL string `json:"ui_url"`
3743
}
3844

3945
// SetApplication data on project

0 commit comments

Comments
 (0)
0