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 1 commit
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
32 changes: 23 additions & 9 deletions cli/cdsctl/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package main

import (
"fmt"
"io"
"os"
"reflect"
"regexp"
"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 +104,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 +122,30 @@ var applicationImportCmd = cli.Command{
}

func applicationImportRun(c cli.Values) error {
path := c.GetString("filename")
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
var contentFile io.Reader
path := c.GetString("path")

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

msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), f, format, c.GetBool("force"))
if isURL, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, path); isURL {
var errF error
contentFile, _, errF = exportentities.OpenURL(path, format)
if errF != nil {
return errF
}
} else {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
contentFile = f
}

msgs, err := client.ApplicationImport(c.GetString(_ProjectKey), contentFile, format, c.GetBool("force"))
if err != nil {
if msgs != nil {
for _, msg := range msgs {
Expand Down
35 changes: 24 additions & 11 deletions cli/cdsctl/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package main

import (
"fmt"
"io"
"os"
"reflect"
"regexp"
"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 +92,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 +110,29 @@ var environmentImportCmd = cli.Command{
}

func environmentImportRun(c cli.Values) error {
path := c.GetString("filename")
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

var format = "yaml"
var contentFile io.Reader
path := c.GetString("path")
format := "yaml"
if strings.HasSuffix(path, ".json") {
format = "json"
}

msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), f, format, c.GetBool("force"))
if isURL, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, path); isURL {
var errF error
contentFile, _, errF = exportentities.OpenURL(path, format)
if errF != nil {
return errF
}
} else {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
contentFile = f
}

msgs, err := client.EnvironmentImport(c.GetString(_ProjectKey), contentFile, format, c.GetBool("force"))
if err != nil {
return err
}
Expand Down
35 changes: 25 additions & 10 deletions cli/cdsctl/worker_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"fmt"
"io"
"reflect"
"regexp"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,7 +68,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 +83,7 @@ For admin:
"add",
},
VariadicArgs: cli.Arg{
Name: "filepath",
Name: "path",
},
Flags: []cli.Flag{
{
Expand All @@ -101,19 +103,32 @@ 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)
if err != nil {
return fmt.Errorf("Error: Cannot read file %s (%v)", filepath, err)
var contentFile io.Reader
var errF error

formatStr := "yaml"
if strings.HasSuffix(filepath, ".json") {
formatStr = "json"
}

if isURL, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, filepath); isURL {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling regexp.MatchString in a loop has poor performance, consider using regexp.Compile (from megacheck)

contentFile, _, errF = exportentities.OpenURL(filepath, formatStr)
} else {
var format exportentities.Format
contentFile, format, errF = exportentities.OpenFile(filepath)
formatStr, _ = exportentities.GetFormatStr(format)
}
if errF != nil {
return fmt.Errorf("Error: Cannot read file %s (%v)", filepath, errF)
}

formatStr, _ := exportentities.GetFormatStr(format)
wm, err := client.WorkerModelImport(reader, formatStr, force)
wm, err := client.WorkerModelImport(contentFile, formatStr, force)
if err != nil {
return err
}
Expand Down
35 changes: 24 additions & 11 deletions cli/cdsctl/workflow_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package main

import (
"fmt"
"io"
"os"
"reflect"
"regexp"
"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 +25,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 +38,29 @@ 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)
if err != nil {
return err
}
defer f.Close()

var format = "yaml"
var contentFile io.Reader
path := c.GetString("path")
format := "yaml"
if strings.HasSuffix(path, ".json") {
format = "json"
}

msgs, err := client.WorkflowImport(c.GetString(_ProjectKey), f, format, c.GetBool("force"))
if isURL, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, path); isURL {
var errF error
contentFile, _, errF = exportentities.OpenURL(path, format)
if errF != nil {
return errF
}
} else {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
contentFile = f
}

msgs, err := client.WorkflowImport(c.GetString(_ProjectKey), contentFile, format, c.GetBool("force"))
if err != nil {
return err
}
Expand Down