Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Ludvig Liljenberg <[email protected]>
  • Loading branch information
ludfjig committed Sep 5, 2023
1 parent 60a9ce0 commit 7fa75da
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 54 deletions.
13 changes: 6 additions & 7 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ func buildBundleCreateCommand(p *porter.Porter) *cobra.Command {
return &cobra.Command{
Use: "create [bundle-name]",
Short: "Create a bundle",
Long: "Create a bundle. This command creates a new porter bundle with the specified bundle-name, in the directory with the specified bundle-name. If no bundle-name is provided, the bundle will be created in current directory and the bundle name will be 'porter-hello'.",
Args: cobra.MaximumNArgs(1), // Expect at most one argument for the bundle name
Long: "Create a bundle. This command creates a new porter bundle with the specified bundle-name, in the directory with the specified bundle-name." +
" The directory will be created if it doesn't already exist. If no bundle-name is provided, the bundle will be created in current directory and the bundle name will be 'porter-hello'.",
Args: cobra.MaximumNArgs(1), // Expect at most one argument for the bundle name
RunE: func(cmd *cobra.Command, args []string) error {
// By default we create the bundle in the current directory
bundleName := ""
if len(args) > 0 {
bundleName = args[0]
bundleName := args[0]
return p.CreateInDir(bundleName)
}

return p.Create(bundleName)
return p.Create()
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/porter/build_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestPorter_LintDuringBuild(t *testing.T) {
testMixins := p.Mixins.(*mixin.TestMixinProvider)
testMixins.LintResults = lintResults

err := p.Create("mybundle")
err := p.Create()
require.NoError(t, err, "Create failed")

opts := BuildOptions{NoLint: false}
Expand All @@ -163,7 +163,7 @@ func TestPorter_LintDuringBuild(t *testing.T) {
testMixins := p.Mixins.(*mixin.TestMixinProvider)
testMixins.LintResults = lintResults

err := p.Create("mybundle")
err := p.Create()
require.NoError(t, err, "Create failed")

opts := BuildOptions{NoLint: true}
Expand Down
76 changes: 42 additions & 34 deletions pkg/porter/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,71 @@ import (
"get.porter.sh/porter/pkg/config"
)

// Create creates a new bundle configuration with the specified bundleName. A directory with the given bundleName will be created if it does not already exist.
// If bundleName is the empty string, the configuration will be created in the current directory, and the name will be "porter-hello".
func (p *Porter) Create(bundleName string) error {
// Normalize the bundleName by removing trailing slashes
bundleName = strings.TrimSuffix(bundleName, "/")

// If given a bundleName, create directory if it doesn't exist
if bundleName != "" {
_, err := os.Stat(bundleName)
if err != nil && errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(bundleName, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory for bundle: %w", err)
}
}
// Create creates a new bundle configuration in the current directory
func (p *Porter) Create() error {
destinationDir := "."

if err := p.CopyTemplate(p.Templates.GetManifest, filepath.Join(destinationDir, config.Name)); err != nil {
return err
}
return p.copyAllTemplatesExceptPorterYaml(destinationDir)
}

// CreateInDir creates a new bundle configuration in the specified directory. The directory will be created if it
// doesn't already exist. For example, if dir is "foo/bar/baz", the directory structure "foo/bar/baz" will be created.
// The bundle name will be set to the "base" of the given directory, which is "baz" in the example above.
func (p *Porter) CreateInDir(dir string) error {
bundleName := filepath.Base(dir)

var err error
if bundleName == "" {
// create bundle with default name "porter_hello"
err = p.CopyTemplate(p.Templates.GetManifest, filepath.Join(bundleName, config.Name))
} else {
// create bundle with given name
err = p.CopyTemplate(func() ([]byte, error) {
content, err := p.Templates.GetManifest()
if err != nil {
return nil, err
}
content = []byte(strings.ReplaceAll(string(content), "porter-hello", bundleName))
return content, nil
}, filepath.Join(bundleName, config.Name))
// Create dirs if they don't exist
_, err := os.Stat(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(dir, os.ModePerm)
}
if err != nil {
return fmt.Errorf("failed to create directory for bundle: %w", err)
}
}

// create porter.yaml, using base of given dir as the bundle name
err = p.CopyTemplate(func() ([]byte, error) {
content, err := p.Templates.GetManifest()
if err != nil {
return nil, err
}
content = []byte(strings.ReplaceAll(string(content), "porter-hello", bundleName))
return content, nil
}, filepath.Join(dir, config.Name))
if err != nil {
return err
}

err = p.CopyTemplate(p.Templates.GetManifestHelpers, filepath.Join(bundleName, "helpers.sh"))
return p.copyAllTemplatesExceptPorterYaml(dir)
}

func (p *Porter) copyAllTemplatesExceptPorterYaml(destinationDir string) error {
err := p.CopyTemplate(p.Templates.GetManifestHelpers, filepath.Join(destinationDir, "helpers.sh"))
if err != nil {
return err
}

err = p.CopyTemplate(p.Templates.GetReadme, filepath.Join(bundleName, "README.md"))
err = p.CopyTemplate(p.Templates.GetReadme, filepath.Join(destinationDir, "README.md"))
if err != nil {
return err
}

err = p.CopyTemplate(p.Templates.GetDockerfileTemplate, filepath.Join(bundleName, "template.Dockerfile"))
err = p.CopyTemplate(p.Templates.GetDockerfileTemplate, filepath.Join(destinationDir, "template.Dockerfile"))
if err != nil {
return err
}

err = p.CopyTemplate(p.Templates.GetDockerignore, filepath.Join(bundleName, ".dockerignore"))
err = p.CopyTemplate(p.Templates.GetDockerignore, filepath.Join(destinationDir, ".dockerignore"))
if err != nil {
return err
}

return p.CopyTemplate(p.Templates.GetGitignore, filepath.Join(bundleName, ".gitignore"))
return p.CopyTemplate(p.Templates.GetGitignore, filepath.Join(destinationDir, ".gitignore"))
}

func (p *Porter) CopyTemplate(getTemplate func() ([]byte, error), dest string) error {
Expand Down
15 changes: 7 additions & 8 deletions pkg/porter/create_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package porter

import (
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/yaml"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"

"get.porter.sh/porter/pkg"
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/yaml"
"get.porter.sh/porter/tests"
"github.com/stretchr/testify/require"
)
Expand All @@ -16,7 +15,7 @@ func TestCreateInWorkingDirectory(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

err := p.Create("")
err := p.Create()
require.NoError(t, err)

// Verify that files are present in the root directory
Expand Down Expand Up @@ -50,7 +49,7 @@ func TestCreateWithBundleName(t *testing.T) {
bundleName := "mybundle"

p := NewTestPorter(t)
err := p.Create(bundleName)
err := p.Create()
require.NoError(t, err)

// Verify that files are present in the "mybundle" directory
Expand Down Expand Up @@ -81,7 +80,7 @@ func TestCreateWithBundleName(t *testing.T) {
// verify "name" inside porter.yaml is set to "mybundle"
porterYaml := &manifest.Manifest{}
data, err := p.FileSystem.ReadFile(filepath.Join(bundleName, "porter.yaml"))
assert.NoError(t, err)
assert.NoError(t, yaml.Unmarshal(data, &porterYaml))
assert.True(t, porterYaml.Name == bundleName)
require.NoError(t, err)
require.NoError(t, yaml.Unmarshal(data, &porterYaml))
require.True(t, porterYaml.Name == bundleName)
}
2 changes: 1 addition & 1 deletion pkg/porter/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestInstallFromTagIgnoresCurrentBundle(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

err := p.Create("")
err := p.Create()
require.NoError(t, err)

installOpts := NewInstallOptions()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestInvokeCustomAction(t *testing.T) {
ctx := p.SetupIntegrationTest()

// Install a bundle with a custom action defined
err := p.Create("")
err := p.Create()
require.NoError(t, err)

bundleName := p.AddTestBundleDir("testdata/bundles/bundle-with-custom-action", true)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/outputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func CleanupCurrentBundle(ctx context.Context, p *porter.TestPorter) {
}

func installExecOutputsBundle(ctx context.Context, p *porter.TestPorter) string {
err := p.Create("")
err := p.Create()
require.NoError(p.T(), err)

bundleName := p.AddTestBundleDir("testdata/bundles/exec-outputs", true)
Expand Down

0 comments on commit 7fa75da

Please sign in to comment.