Skip to content

Commit

Permalink
feat(api,cdsctl,ui): workflow template with as code workflow (#5053)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Mar 26, 2020
1 parent 8a6f308 commit 9c88e9e
Show file tree
Hide file tree
Showing 125 changed files with 3,089 additions and 2,573 deletions.
18 changes: 9 additions & 9 deletions cli/cdsctl/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"fmt"
"io/ioutil"
"path"
"time"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
actionSDK "github.com/ovh/cds/sdk/action"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/exportentities"
"github.com/ovh/cds/sdk/slug"
)
Expand Down Expand Up @@ -110,20 +111,20 @@ func actionUsageRun(v cli.Values) (cli.ListResult, error) {

type ActionUsageDisplay struct {
Type string `cli:"Type"`
Path string `cli:"Path"`
Path string `cli:"Path"`
}

au := []ActionUsageDisplay{}
for _, v := range usages.Pipelines {
au = append(au, ActionUsageDisplay{
Type: "pipeline",
Path: strings.Replace(fmt.Sprintf("%s - %s - %s", v.ProjectName, v.PipelineName, v.ActionName)," "," ",-1),
Type: "pipeline",
Path: strings.Replace(fmt.Sprintf("%s - %s - %s", v.ProjectName, v.PipelineName, v.ActionName), " ", " ", -1),
})
}
for _, v := range usages.Actions {
au = append(au, ActionUsageDisplay{
Type: "action",
Path: fmt.Sprintf("%s/%s", v.GroupName, v.ParentActionName),
Type: "action",
Path: fmt.Sprintf("%s/%s", v.GroupName, v.ParentActionName),
})
}
return cli.AsListResult(au), nil
Expand Down Expand Up @@ -243,9 +244,8 @@ func actionImportRun(v cli.Values) error {
return err
}
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)

if err := client.ActionImport(contentFile, formatStr); err != nil {
if err := client.ActionImport(contentFile, cdsclient.ContentType(format.ContentType())); err != nil {
return err
}

Expand Down Expand Up @@ -274,7 +274,7 @@ func actionExportRun(v cli.Values) error {
return err
}

b, err := client.ActionExport(groupName, actionName, v.GetString("format"))
b, err := client.ActionExport(groupName, actionName, cdsclient.Format(v.GetString("format")))
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions cli/cdsctl/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/exportentities"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/exportentities"
)

var applicationCmd = cli.Command{
Expand Down Expand Up @@ -143,9 +143,15 @@ func applicationImportRun(c cli.Values) error {
return err
}
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)

msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
mods := []cdsclient.RequestModifier{
cdsclient.ContentType(format.ContentType()),
}
if c.GetBool("force") {
mods = append(mods, cdsclient.Force())
}

msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), contentFile, mods...)
for _, msg := range msgs {
fmt.Println(msg)
}
Expand All @@ -170,7 +176,8 @@ var applicationExportCmd = cli.Command{
}

func applicationExportRun(c cli.Values) error {
btes, err := client.ApplicationExport(c.GetString(_ProjectKey), c.GetString(_ApplicationName), c.GetString("format"))
btes, err := client.ApplicationExport(c.GetString(_ProjectKey), c.GetString(_ApplicationName),
cdsclient.Format(c.GetString("format")))
if err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions cli/cdsctl/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/exportentities"
)

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

msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
mods := []cdsclient.RequestModifier{
cdsclient.ContentType(format.ContentType()),
}
if c.GetBool("force") {
mods = append(mods, cdsclient.Force())
}

msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), contentFile, mods...)
for _, s := range msgs {
fmt.Println(s)
}
Expand All @@ -140,7 +147,8 @@ var environmentExportCmd = cli.Command{
}

func environmentExportRun(c cli.Values) error {
btes, err := client.EnvironmentExport(c.GetString(_ProjectKey), c.GetString("environment-name"), c.GetString("format"))
btes, err := client.EnvironmentExport(c.GetString(_ProjectKey), c.GetString("environment-name"),
cdsclient.Format(c.GetString("format")))
if err != nil {
return err
}
Expand Down
64 changes: 29 additions & 35 deletions cli/cdsctl/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/exportentities"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/exportentities"
)

var pipelineCmd = cli.Command{
Expand Down Expand Up @@ -76,21 +75,17 @@ var pipelineExportCmd = cli.Command{
},
Flags: []cli.Flag{
{
Name: "format",
Usage: "yml or json",
IsValid: func(s string) bool {
if s != "json" && s != "yml" {
return false
}
return true
},
Default: "yml",
Type: cli.FlagString,
Name: "format",
Usage: "Specify export format (json or yaml)",
Default: "yaml",
},
},
}

func pipelineExportRun(v cli.Values) error {
btes, err := client.PipelineExport(v.GetString(_ProjectKey), v.GetString("pipeline-name"), v.GetString("format"))
btes, err := client.PipelineExport(v.GetString(_ProjectKey), v.GetString("pipeline-name"),
cdsclient.Format(v.GetString("format")))
if err != nil {
return err
}
Expand Down Expand Up @@ -125,33 +120,33 @@ var pipelineImportCmd = cli.Command{
}

func pipelineImportRun(v cli.Values) error {
path := v.GetString("path")

var reader io.ReadCloser
defer func() {
if reader != nil {
reader.Close()
}
}()
var format = "yaml"

if strings.HasSuffix(v.GetString("path"), ".json") {
format = "json"
var err error
if sdk.IsURL(path) {
reader, err = exportentities.OpenURL(path)
} else {
reader, err = exportentities.OpenFile(path)
}
if err != nil {
return err
}
defer reader.Close() // nolint

if sdk.IsURL(v.GetString("path")) {
var err error
reader, _, err = exportentities.OpenURL(v.GetString("path"), format)
if err != nil {
return err
}
} else {
var err error
reader, _, err = exportentities.OpenFile(v.GetString("path"))
if err != nil {
return err
}
format, err := exportentities.GetFormatFromPath(path)
if err != nil {
return err
}

msgs, err := client.PipelineImport(v.GetString(_ProjectKey), reader, format, v.GetBool("force"))
mods := []cdsclient.RequestModifier{
cdsclient.ContentType(format.ContentType()),
}
if v.GetBool("force") {
mods = append(mods, cdsclient.Force())
}

msgs, err := client.PipelineImport(v.GetString(_ProjectKey), reader, mods...)
for _, m := range msgs {
fmt.Println(m)
}
Expand All @@ -175,6 +170,5 @@ func pipelineDeleteRun(v cli.Values) error {
fmt.Println(err.Error())
os.Exit(0)
}

return err
}
44 changes: 22 additions & 22 deletions cli/cdsctl/project_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"fmt"
"io"
"strings"

"github.com/spf13/cobra"

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/exportentities"
)

Expand Down Expand Up @@ -49,33 +49,33 @@ var projectGroupImportCmd = cli.Command{
}

func projectGroupImportRun(v cli.Values) error {
path := v.GetString("path")

var reader io.ReadCloser
defer func() {
if reader != nil {
reader.Close()
}
}()
var format = "yaml"
var err error
if sdk.IsURL(path) {
reader, err = exportentities.OpenURL(path)
} else {
reader, err = exportentities.OpenFile(path)
}
if err != nil {
return err
}
defer reader.Close() // nolint

if strings.HasSuffix(v.GetString("path"), ".json") {
format = "json"
format, err := exportentities.GetFormatFromPath(path)
if err != nil {
return err
}

if sdk.IsURL(v.GetString("path")) {
var err error
reader, _, err = exportentities.OpenURL(v.GetString("path"), format)
if err != nil {
return err
}
} else {
var err error
reader, _, err = exportentities.OpenFile(v.GetString("path"))
if err != nil {
return err
}
mods := []cdsclient.RequestModifier{
cdsclient.ContentType(format.ContentType()),
}
if v.GetBool("force") {
mods = append(mods, cdsclient.Force())
}

if _, err := client.ProjectGroupsImport(v.GetString(_ProjectKey), reader, format, v.GetBool("force")); err != nil {
if _, err := client.ProjectGroupsImport(v.GetString(_ProjectKey), reader, mods...); err != nil {
return err
}
fmt.Printf("Groups imported in project %s with success\n", v.GetString(_ProjectKey))
Expand Down
10 changes: 8 additions & 2 deletions cli/cdsctl/project_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk/cdsclient"
"github.com/ovh/cds/sdk/exportentities"
)

Expand Down Expand Up @@ -74,7 +74,13 @@ func projectIntegrationImportFunc(v cli.Values) error {
return fmt.Errorf("unable to open file %s: %v", v.GetString("filename"), err)
}
defer f.Close()
_, err = client.ProjectIntegrationImport(v.GetString(_ProjectKey), f, filepath.Ext(v.GetString("filename")), v.GetBool("force"))

var mods []cdsclient.RequestModifier
if v.GetBool("force") {
mods = append(mods, cdsclient.Force())
}

_, err = client.ProjectIntegrationImport(v.GetString(_ProjectKey), f, mods...)
return err
}

Expand Down
5 changes: 3 additions & 2 deletions cli/cdsctl/template_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func templateApplyRun(v cli.Values) error {

importPush := v.GetBool("import-push")
importAsCode := v.GetBool("import-as-code")
detached := v.GetBool("detach")

// try to find existing .git repository
var localRepoURL string
Expand Down Expand Up @@ -259,7 +260,7 @@ func templateApplyRun(v cli.Values) error {
}
}

if !importAsCode && !importPush {
if !importAsCode && !importPush && !detached {
if localRepoURL != "" {
importAsCode = cli.AskConfirm(fmt.Sprintf("Import the generated workflow as code to the %s project", projectKey))
}
Expand All @@ -286,7 +287,7 @@ func templateApplyRun(v cli.Values) error {
ProjectKey: projectKey,
WorkflowName: workflowName,
Parameters: params,
Detached: v.GetBool("detach"),
Detached: detached,
}
if err := wt.CheckParams(req); err != nil {
return err
Expand Down
Loading

0 comments on commit 9c88e9e

Please sign in to comment.