Skip to content

Commit

Permalink
Merge pull request #187 from youreddy/issue-#174
Browse files Browse the repository at this point in the history
Accept urls and file paths for porter run
  • Loading branch information
carolynvs-msft authored Feb 28, 2019
2 parents f88fc45 + 227b536 commit e61b9d3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
4 changes: 0 additions & 4 deletions cmd/porter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func buildRunCommand(p *porter.Porter) *cobra.Command {
return err
}

if exists, _ := p.FileSystem.Exists(opts.file); !exists {
return fmt.Errorf("invalid --file: the specified porter configuration file %q doesn't exist\n", opts.file)
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
12 changes: 0 additions & 12 deletions cmd/porter/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ func TestRun_Validate(t *testing.T) {
require.Nil(t, err)
}

func TestRun_Validate_MissingFile(t *testing.T) {
p := porter.NewTestPorter(t)
p.TestConfig.SetupPorterHome()
cmd := buildRunCommand(p.Porter)

cmd.Flags().Set("action", string(config.ActionInstall))

err := cmd.PreRunE(cmd, []string{})
require.NotNil(t, err)
assert.Contains(t, err.Error(), "invalid --file")
}

func TestRun_Validate_MissingAction(t *testing.T) {
p := porter.NewTestPorter(t)
p.TestConfig.SetupPorterHome()
Expand Down
7 changes: 5 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/deislabs/porter/pkg/context"
"github.com/gobuffalo/packr/v2"
Expand Down Expand Up @@ -132,12 +133,14 @@ func (c *Config) GetBundlesCache() (string, error) {
// - ./bundles/
// - PORTER_HOME/bundles/
func (c *Config) GetBundleDir(bundle string) (string, error) {
urlPath := strings.HasPrefix(c.Manifest.path, "http")

// Check for a local bundle next to the current manifest
if c.Manifest != nil {
if c.Manifest != nil || urlPath == false {
localDir := c.Manifest.GetManifestDir()
localBundleDir := filepath.Join(localDir, "bundles", bundle)

dirExists, err := c.FileSystem.DirExists(localBundleDir)

if err != nil {
return "", errors.Wrapf(err, "could not check if directory %s exists", localBundleDir)
}
Expand Down
44 changes: 41 additions & 3 deletions pkg/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package config

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"

"github.com/deislabs/porter/pkg/mixin"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/reflectwalk"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

type Manifest struct {
Expand Down Expand Up @@ -135,7 +137,11 @@ type BundleConnection struct {
// TODO: Need to add type once it's completed in #20
}

func (c *Config) ReadManifest(path string) (*Manifest, error) {
func (c *Config) readFromFile(path string) (*Manifest, error) {
if exists, _ := c.FileSystem.Exists(path); !exists {
return nil, errors.Errorf("the specified porter configuration file %s does not exist", path)
}

data, err := c.FileSystem.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "could not read manifest at %q", path)
Expand All @@ -151,6 +157,38 @@ func (c *Config) ReadManifest(path string) (*Manifest, error) {
return m, nil
}

func (c *Config) readFromURL(path string) (*Manifest, error) {
resp, err := http.Get(path)
if err != nil {
return nil, errors.Wrapf(err, "could not reach url %s", path)
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "could not read from url %s", path)
}

m := &Manifest{}
err = yaml.Unmarshal(body, m)
if err != nil {
return nil, errors.Wrapf(err, "could not parse manifest yaml in %q", path)
}
m.path = path

return m, nil
}

// ReadManifest determines if specified path is a URL or a filepath.
// After reading the data in the path it returns a Manifest and any errors
func (c *Config) ReadManifest(path string) (*Manifest, error) {
if strings.HasPrefix(path, "http") {
return c.readFromURL(path)
}

return c.readFromFile(path)
}

func (c *Config) LoadManifest() error {
return c.LoadManifestFrom(Name)
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/config/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ import (
"github.com/stretchr/testify/require"
)

func TestReadManifest_URL(t *testing.T) {
c := NewTestConfig(t)
url := "https://raw.githubusercontent.com/deislabs/porter/master/pkg/config/testdata/simple.porter.yaml"
m, err := c.ReadManifest(url)

require.NoError(t, err)
assert.Equal(t, "hello", m.Name)
assert.Equal(t, url, m.path)
}

func TestReadManifest_Validate_InvalidURL(t *testing.T) {
c := NewTestConfig(t)
_, err := c.ReadManifest("http://fake-example-porter")

assert.Error(t, err)
assert.Regexp(t, "could not reach url http://fake-example-porter", err)
}

func TestReadManifest_File(t *testing.T) {
c := NewTestConfig(t)
c.TestContext.AddTestFile("testdata/simple.porter.yaml", Name)
m, err := c.ReadManifest(Name)

require.NoError(t, err)
assert.Equal(t, "hello", m.Name)
assert.Equal(t, Name, m.path)
}

func TestReadManifest_Validate_MissingFile(t *testing.T) {
c := NewTestConfig(t)
_, err := c.ReadManifest("fake-porter.yaml")

assert.EqualError(t, err, "the specified porter configuration file fake-porter.yaml does not exist")
}

func TestLoadManifest(t *testing.T) {
c := NewTestConfig(t)
c.SetupPorterHome()
Expand Down

0 comments on commit e61b9d3

Please sign in to comment.