Skip to content

Commit

Permalink
feat(cdsctl): add action import from URL (#3770) (#3771)
Browse files Browse the repository at this point in the history
close #3770 
Signed-off-by: Benjamin Coenen <[email protected]>
  • Loading branch information
bnjjj authored Dec 26, 2018
1 parent 40594f6 commit 3d0f311
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions cli/cdsctl/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"reflect"
"regexp"
"strings"

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

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

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

if errImport := client.ActionImport(f, format); errImport != nil {
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
}

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

Expand Down

0 comments on commit 3d0f311

Please sign in to comment.