Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdsctl): add import with url for all entities (#3777) #3778

Merged
merged 4 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions cli/cdsctl/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"reflect"
"regexp"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -158,29 +155,14 @@ cdsctl action import myAction.yml`,

func actionImportRun(v cli.Values) error {
path := v.GetString("path")
var contentFile io.Reader
var err error

format := "yaml"
if strings.HasSuffix(path, ".json") {
format = "json"
}

if isURL, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, path); isURL {
contentFile, _, err = exportentities.OpenURL(path, format)
if err != nil {
return err
}
} else {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
contentFile = f
contentFile, format, err := exportentities.OpenPath(path)
if err != nil {
return err
}
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)

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

Expand Down
20 changes: 8 additions & 12 deletions cli/cdsctl/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"os"
"reflect"
"strings"

"github.com/spf13/cobra"

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

var applicationCmd = cli.Command{
Expand Down Expand Up @@ -101,12 +101,12 @@ func applicationDeleteRun(v cli.Values) error {

var applicationImportCmd = cli.Command{
Name: "import",
Short: "Import an application",
Short: "Import an application with a local filepath or an URL",
Ctx: []cli.Arg{
{Name: _ProjectKey},
},
Args: []cli.Arg{
{Name: "filename"},
{Name: "path"},
},
Flags: []cli.Flag{
{
Expand All @@ -119,19 +119,15 @@ var applicationImportCmd = cli.Command{
}

func applicationImportRun(c cli.Values) error {
path := c.GetString("filename")
f, err := os.Open(path)
path := c.GetString("path")
contentFile, format, err := exportentities.OpenPath(path)
if err != nil {
return err
}
defer f.Close()
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)

var format = "yaml"
if strings.HasSuffix(path, ".json") {
format = "json"
}

msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), f, format, c.GetBool("force"))
msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
if err != nil {
if msgs != nil {
for _, msg := range msgs {
Expand Down
20 changes: 8 additions & 12 deletions cli/cdsctl/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"os"
"reflect"
"strings"

"github.com/spf13/cobra"

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

var environmentCmd = cli.Command{
Expand Down Expand Up @@ -89,12 +89,12 @@ func environmentDeleteRun(v cli.Values) error {

var environmentImportCmd = cli.Command{
Name: "import",
Short: "Import an environment",
Short: "Import an environment with local filepath or URL",
Ctx: []cli.Arg{
{Name: _ProjectKey},
},
Args: []cli.Arg{
{Name: "filename"},
{Name: "path"},
},
Flags: []cli.Flag{
{
Expand All @@ -107,19 +107,15 @@ var environmentImportCmd = cli.Command{
}

func environmentImportRun(c cli.Values) error {
path := c.GetString("filename")
f, err := os.Open(path)
path := c.GetString("path")
contentFile, format, err := exportentities.OpenPath(path)
if err != nil {
return err
}
defer f.Close()
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)

var format = "yaml"
if strings.HasSuffix(path, ".json") {
format = "json"
}

msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), f, format, c.GetBool("force"))
msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions cli/cdsctl/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func workerModelListRun(v cli.Values) (cli.ListResult, error) {

var workerModelImportCmd = cli.Command{
Name: "import",
Example: "cdsctl worker model import my_worker_model_file.yml",
Example: "cdsctl worker model import my_worker_model_file.yml https://mydomain.com/myworkermodel.yml",
Long: `
Available model type :
- Docker images ("docker")
Expand All @@ -81,7 +81,7 @@ For admin:
"add",
},
VariadicArgs: cli.Arg{
Name: "filepath",
Name: "path",
},
Flags: []cli.Flag{
{
Expand All @@ -101,23 +101,25 @@ For admin:

func workerModelImportRun(c cli.Values) error {
force := c.GetBool("force")
if c.GetString("filepath") == "" {
return fmt.Errorf("filepath for worker model is mandatory")
if c.GetString("path") == "" {
return fmt.Errorf("path for worker model is mandatory")
}
files := strings.Split(c.GetString("filepath"), ",")
files := strings.Split(c.GetString("path"), ",")

for _, filepath := range files {
reader, format, err := exportentities.OpenFile(filepath)
contentFile, format, err := exportentities.OpenPath(filepath)
if err != nil {
return fmt.Errorf("Error: Cannot read file %s (%v)", filepath, err)
return err
}

formatStr, _ := exportentities.GetFormatStr(format)
wm, err := client.WorkerModelImport(reader, formatStr, force)

wm, err := client.WorkerModelImport(contentFile, formatStr, force)
if err != nil {
_ = contentFile.Close()
return err
}
fmt.Printf("Worker model %s imported with success\n", wm.Name)
_ = contentFile.Close()
}

return nil
Expand Down
21 changes: 8 additions & 13 deletions cli/cdsctl/workflow_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package main

import (
"fmt"
"os"
"reflect"
"strings"

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

var workflowImportCmd = cli.Command{
Name: "import",
Short: "Import a workflow",
Long: `
In case you want to import just your workflow.
In case you want to import just your workflow. Instead of use a local file you can also use an URL to your yaml file.

If you want to update also dependencies likes pipelines, applications or environments at same time you have to use workflow push instead workflow import.

Expand All @@ -22,7 +21,7 @@ If you want to update also dependencies likes pipelines, applications or environ
{Name: _ProjectKey},
},
Args: []cli.Arg{
{Name: "filename"},
{Name: "path"},
},
Flags: []cli.Flag{
{
Expand All @@ -35,19 +34,15 @@ If you want to update also dependencies likes pipelines, applications or environ
}

func workflowImportRun(c cli.Values) error {
path := c.GetString("filename")
f, err := os.Open(path)
path := c.GetString("path")
contentFile, format, err := exportentities.OpenPath(path)
if err != nil {
return err
}
defer f.Close()
defer contentFile.Close() //nolint
formatStr, _ := exportentities.GetFormatStr(format)

var format = "yaml"
if strings.HasSuffix(path, ".json") {
format = "json"
}

msgs, err := client.WorkflowImport(c.GetString(_ProjectKey), f, format, c.GetBool("force"))
msgs, err := client.WorkflowImport(c.GetString(_ProjectKey), contentFile, formatStr, c.GetBool("force"))
if err != nil {
return err
}
Expand Down
29 changes: 29 additions & 0 deletions sdk/exportentities/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"

Expand All @@ -14,6 +15,8 @@ import (
"github.com/ovh/cds/sdk"
)

var rxURL = regexp.MustCompile(`http[s]?:\/\/(.*)`)

//GetFormat return a format
func GetFormat(f string) (Format, error) {
s := strings.ToLower(f)
Expand Down Expand Up @@ -141,3 +144,29 @@ func OpenURL(u string, f string) (io.ReadCloser, Format, error) {
response, err := netClient.Get(u)
return response.Body, format, err
}

// OpenPath opens an URL or a file
func OpenPath(path string) (io.ReadCloser, Format, error) {
var contentFile io.ReadCloser
formatStr := "yaml"
if strings.HasSuffix(path, ".json") {
formatStr = "json"
}
format, _ := GetFormat(formatStr)

if rxURL.MatchString(path) {
var err error
contentFile, format, err = OpenURL(path, formatStr)
if err != nil {
return nil, format, err
}
} else {
f, err := os.Open(path)
if err != nil {
return nil, format, err
}
contentFile = f
}

return contentFile, format, nil
}