Skip to content

Commit 9c88e9e

Browse files
authored
feat(api,cdsctl,ui): workflow template with as code workflow (#5053)
1 parent 8a6f308 commit 9c88e9e

File tree

125 files changed

+3089
-2573
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+3089
-2573
lines changed

cli/cdsctl/action.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"fmt"
55
"io/ioutil"
66
"path"
7-
"time"
87
"strings"
8+
"time"
99

1010
"github.com/spf13/cobra"
1111

1212
"github.com/ovh/cds/cli"
1313
"github.com/ovh/cds/sdk"
1414
actionSDK "github.com/ovh/cds/sdk/action"
15+
"github.com/ovh/cds/sdk/cdsclient"
1516
"github.com/ovh/cds/sdk/exportentities"
1617
"github.com/ovh/cds/sdk/slug"
1718
)
@@ -110,20 +111,20 @@ func actionUsageRun(v cli.Values) (cli.ListResult, error) {
110111

111112
type ActionUsageDisplay struct {
112113
Type string `cli:"Type"`
113-
Path string `cli:"Path"`
114+
Path string `cli:"Path"`
114115
}
115116

116117
au := []ActionUsageDisplay{}
117118
for _, v := range usages.Pipelines {
118119
au = append(au, ActionUsageDisplay{
119-
Type: "pipeline",
120-
Path: strings.Replace(fmt.Sprintf("%s - %s - %s", v.ProjectName, v.PipelineName, v.ActionName)," "," ",-1),
120+
Type: "pipeline",
121+
Path: strings.Replace(fmt.Sprintf("%s - %s - %s", v.ProjectName, v.PipelineName, v.ActionName), " ", " ", -1),
121122
})
122123
}
123124
for _, v := range usages.Actions {
124125
au = append(au, ActionUsageDisplay{
125-
Type: "action",
126-
Path: fmt.Sprintf("%s/%s", v.GroupName, v.ParentActionName),
126+
Type: "action",
127+
Path: fmt.Sprintf("%s/%s", v.GroupName, v.ParentActionName),
127128
})
128129
}
129130
return cli.AsListResult(au), nil
@@ -243,9 +244,8 @@ func actionImportRun(v cli.Values) error {
243244
return err
244245
}
245246
defer contentFile.Close() //nolint
246-
formatStr, _ := exportentities.GetFormatStr(format)
247247

248-
if err := client.ActionImport(contentFile, formatStr); err != nil {
248+
if err := client.ActionImport(contentFile, cdsclient.ContentType(format.ContentType())); err != nil {
249249
return err
250250
}
251251

@@ -274,7 +274,7 @@ func actionExportRun(v cli.Values) error {
274274
return err
275275
}
276276

277-
b, err := client.ActionExport(groupName, actionName, v.GetString("format"))
277+
b, err := client.ActionExport(groupName, actionName, cdsclient.Format(v.GetString("format")))
278278
if err != nil {
279279
return err
280280
}

cli/cdsctl/application.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
"github.com/ovh/cds/cli"
1010
"github.com/ovh/cds/sdk"
11-
"github.com/ovh/cds/sdk/exportentities"
1211
"github.com/ovh/cds/sdk/cdsclient"
12+
"github.com/ovh/cds/sdk/exportentities"
1313
)
1414

1515
var applicationCmd = cli.Command{
@@ -143,9 +143,15 @@ func applicationImportRun(c cli.Values) error {
143143
return err
144144
}
145145
defer contentFile.Close() //nolint
146-
formatStr, _ := exportentities.GetFormatStr(format)
147146

148-
msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
147+
mods := []cdsclient.RequestModifier{
148+
cdsclient.ContentType(format.ContentType()),
149+
}
150+
if c.GetBool("force") {
151+
mods = append(mods, cdsclient.Force())
152+
}
153+
154+
msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), contentFile, mods...)
149155
for _, msg := range msgs {
150156
fmt.Println(msg)
151157
}
@@ -170,7 +176,8 @@ var applicationExportCmd = cli.Command{
170176
}
171177

172178
func applicationExportRun(c cli.Values) error {
173-
btes, err := client.ApplicationExport(c.GetString(_ProjectKey), c.GetString(_ApplicationName), c.GetString("format"))
179+
btes, err := client.ApplicationExport(c.GetString(_ProjectKey), c.GetString(_ApplicationName),
180+
cdsclient.Format(c.GetString("format")))
174181
if err != nil {
175182
return err
176183
}

cli/cdsctl/environment.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ovh/cds/cli"
1010
"github.com/ovh/cds/sdk"
11+
"github.com/ovh/cds/sdk/cdsclient"
1112
"github.com/ovh/cds/sdk/exportentities"
1213
)
1314

@@ -111,9 +112,15 @@ func environmentImportRun(c cli.Values) error {
111112
return err
112113
}
113114
defer contentFile.Close() //nolint
114-
formatStr, _ := exportentities.GetFormatStr(format)
115115

116-
msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
116+
mods := []cdsclient.RequestModifier{
117+
cdsclient.ContentType(format.ContentType()),
118+
}
119+
if c.GetBool("force") {
120+
mods = append(mods, cdsclient.Force())
121+
}
122+
123+
msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), contentFile, mods...)
117124
for _, s := range msgs {
118125
fmt.Println(s)
119126
}
@@ -140,7 +147,8 @@ var environmentExportCmd = cli.Command{
140147
}
141148

142149
func environmentExportRun(c cli.Values) error {
143-
btes, err := client.EnvironmentExport(c.GetString(_ProjectKey), c.GetString("environment-name"), c.GetString("format"))
150+
btes, err := client.EnvironmentExport(c.GetString(_ProjectKey), c.GetString("environment-name"),
151+
cdsclient.Format(c.GetString("format")))
144152
if err != nil {
145153
return err
146154
}

cli/cdsctl/pipeline.go

+29-35
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"fmt"
55
"io"
66
"os"
7-
"strings"
87

98
"github.com/spf13/cobra"
109

1110
"github.com/ovh/cds/cli"
1211
"github.com/ovh/cds/sdk"
13-
"github.com/ovh/cds/sdk/exportentities"
1412
"github.com/ovh/cds/sdk/cdsclient"
13+
"github.com/ovh/cds/sdk/exportentities"
1514
)
1615

1716
var pipelineCmd = cli.Command{
@@ -76,21 +75,17 @@ var pipelineExportCmd = cli.Command{
7675
},
7776
Flags: []cli.Flag{
7877
{
79-
Name: "format",
80-
Usage: "yml or json",
81-
IsValid: func(s string) bool {
82-
if s != "json" && s != "yml" {
83-
return false
84-
}
85-
return true
86-
},
87-
Default: "yml",
78+
Type: cli.FlagString,
79+
Name: "format",
80+
Usage: "Specify export format (json or yaml)",
81+
Default: "yaml",
8882
},
8983
},
9084
}
9185

9286
func pipelineExportRun(v cli.Values) error {
93-
btes, err := client.PipelineExport(v.GetString(_ProjectKey), v.GetString("pipeline-name"), v.GetString("format"))
87+
btes, err := client.PipelineExport(v.GetString(_ProjectKey), v.GetString("pipeline-name"),
88+
cdsclient.Format(v.GetString("format")))
9489
if err != nil {
9590
return err
9691
}
@@ -125,33 +120,33 @@ var pipelineImportCmd = cli.Command{
125120
}
126121

127122
func pipelineImportRun(v cli.Values) error {
123+
path := v.GetString("path")
124+
128125
var reader io.ReadCloser
129-
defer func() {
130-
if reader != nil {
131-
reader.Close()
132-
}
133-
}()
134-
var format = "yaml"
135-
136-
if strings.HasSuffix(v.GetString("path"), ".json") {
137-
format = "json"
126+
var err error
127+
if sdk.IsURL(path) {
128+
reader, err = exportentities.OpenURL(path)
129+
} else {
130+
reader, err = exportentities.OpenFile(path)
138131
}
132+
if err != nil {
133+
return err
134+
}
135+
defer reader.Close() // nolint
139136

140-
if sdk.IsURL(v.GetString("path")) {
141-
var err error
142-
reader, _, err = exportentities.OpenURL(v.GetString("path"), format)
143-
if err != nil {
144-
return err
145-
}
146-
} else {
147-
var err error
148-
reader, _, err = exportentities.OpenFile(v.GetString("path"))
149-
if err != nil {
150-
return err
151-
}
137+
format, err := exportentities.GetFormatFromPath(path)
138+
if err != nil {
139+
return err
152140
}
153141

154-
msgs, err := client.PipelineImport(v.GetString(_ProjectKey), reader, format, v.GetBool("force"))
142+
mods := []cdsclient.RequestModifier{
143+
cdsclient.ContentType(format.ContentType()),
144+
}
145+
if v.GetBool("force") {
146+
mods = append(mods, cdsclient.Force())
147+
}
148+
149+
msgs, err := client.PipelineImport(v.GetString(_ProjectKey), reader, mods...)
155150
for _, m := range msgs {
156151
fmt.Println(m)
157152
}
@@ -175,6 +170,5 @@ func pipelineDeleteRun(v cli.Values) error {
175170
fmt.Println(err.Error())
176171
os.Exit(0)
177172
}
178-
179173
return err
180174
}

cli/cdsctl/project_group.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"fmt"
55
"io"
6-
"strings"
76

87
"github.com/spf13/cobra"
98

109
"github.com/ovh/cds/cli"
1110
"github.com/ovh/cds/sdk"
11+
"github.com/ovh/cds/sdk/cdsclient"
1212
"github.com/ovh/cds/sdk/exportentities"
1313
)
1414

@@ -49,33 +49,33 @@ var projectGroupImportCmd = cli.Command{
4949
}
5050

5151
func projectGroupImportRun(v cli.Values) error {
52+
path := v.GetString("path")
53+
5254
var reader io.ReadCloser
53-
defer func() {
54-
if reader != nil {
55-
reader.Close()
56-
}
57-
}()
58-
var format = "yaml"
55+
var err error
56+
if sdk.IsURL(path) {
57+
reader, err = exportentities.OpenURL(path)
58+
} else {
59+
reader, err = exportentities.OpenFile(path)
60+
}
61+
if err != nil {
62+
return err
63+
}
64+
defer reader.Close() // nolint
5965

60-
if strings.HasSuffix(v.GetString("path"), ".json") {
61-
format = "json"
66+
format, err := exportentities.GetFormatFromPath(path)
67+
if err != nil {
68+
return err
6269
}
6370

64-
if sdk.IsURL(v.GetString("path")) {
65-
var err error
66-
reader, _, err = exportentities.OpenURL(v.GetString("path"), format)
67-
if err != nil {
68-
return err
69-
}
70-
} else {
71-
var err error
72-
reader, _, err = exportentities.OpenFile(v.GetString("path"))
73-
if err != nil {
74-
return err
75-
}
71+
mods := []cdsclient.RequestModifier{
72+
cdsclient.ContentType(format.ContentType()),
73+
}
74+
if v.GetBool("force") {
75+
mods = append(mods, cdsclient.Force())
7676
}
7777

78-
if _, err := client.ProjectGroupsImport(v.GetString(_ProjectKey), reader, format, v.GetBool("force")); err != nil {
78+
if _, err := client.ProjectGroupsImport(v.GetString(_ProjectKey), reader, mods...); err != nil {
7979
return err
8080
}
8181
fmt.Printf("Groups imported in project %s with success\n", v.GetString(_ProjectKey))

cli/cdsctl/project_integration.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
76

87
"github.com/spf13/cobra"
98

109
"github.com/ovh/cds/cli"
10+
"github.com/ovh/cds/sdk/cdsclient"
1111
"github.com/ovh/cds/sdk/exportentities"
1212
)
1313

@@ -74,7 +74,13 @@ func projectIntegrationImportFunc(v cli.Values) error {
7474
return fmt.Errorf("unable to open file %s: %v", v.GetString("filename"), err)
7575
}
7676
defer f.Close()
77-
_, err = client.ProjectIntegrationImport(v.GetString(_ProjectKey), f, filepath.Ext(v.GetString("filename")), v.GetBool("force"))
77+
78+
var mods []cdsclient.RequestModifier
79+
if v.GetBool("force") {
80+
mods = append(mods, cdsclient.Force())
81+
}
82+
83+
_, err = client.ProjectIntegrationImport(v.GetString(_ProjectKey), f, mods...)
7884
return err
7985
}
8086

cli/cdsctl/template_apply.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func templateApplyRun(v cli.Values) error {
167167

168168
importPush := v.GetBool("import-push")
169169
importAsCode := v.GetBool("import-as-code")
170+
detached := v.GetBool("detach")
170171

171172
// try to find existing .git repository
172173
var localRepoURL string
@@ -259,7 +260,7 @@ func templateApplyRun(v cli.Values) error {
259260
}
260261
}
261262

262-
if !importAsCode && !importPush {
263+
if !importAsCode && !importPush && !detached {
263264
if localRepoURL != "" {
264265
importAsCode = cli.AskConfirm(fmt.Sprintf("Import the generated workflow as code to the %s project", projectKey))
265266
}
@@ -286,7 +287,7 @@ func templateApplyRun(v cli.Values) error {
286287
ProjectKey: projectKey,
287288
WorkflowName: workflowName,
288289
Parameters: params,
289-
Detached: v.GetBool("detach"),
290+
Detached: detached,
290291
}
291292
if err := wt.CheckParams(req); err != nil {
292293
return err

0 commit comments

Comments
 (0)