Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>
  • Loading branch information
ludfjig committed Sep 5, 2023
1 parent 60a9ce0 commit f7ae24c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 75 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
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
4 changes: 2 additions & 2 deletions pkg/porter/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 +50,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
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
65 changes: 35 additions & 30 deletions pkg/storage/installation_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,43 @@ var exampleBundle = bundle.Bundle{
// it returns a InstallationStorageProvider, and a test cleanup function.
//
// installations/
// foo/
// CLAIM_ID_1 (install)
// CLAIM_ID_2 (upgrade)
// CLAIM_ID_3 (invoke - test)
// CLAIM_ID_4 (uninstall)
// bar/
// CLAIM_ID_10 (install)
// baz/
// CLAIM_ID_20 (install)
// CLAIM_ID_21 (install)
//
// foo/
// CLAIM_ID_1 (install)
// CLAIM_ID_2 (upgrade)
// CLAIM_ID_3 (invoke - test)
// CLAIM_ID_4 (uninstall)
// bar/
// CLAIM_ID_10 (install)
// baz/
// CLAIM_ID_20 (install)
// CLAIM_ID_21 (install)
//
// results/
// CLAIM_ID_1/
// RESULT_ID_1 (success)
// CLAIM_ID_2/
// RESULT_ID 2 (success)
// CLAIM_ID_3/
// RESULT_ID_3 (failed)
// CLAIM_ID_4/
// RESULT_ID_4 (success)
// CLAIM_ID_10/
// RESULT_ID_10 (running)
// RESULT_ID_11 (success)
// CLAIM_ID_20/
// RESULT_ID_20 (failed)
// CLAIM_ID_21/
// NO RESULT YET
//
// CLAIM_ID_1/
// RESULT_ID_1 (success)
// CLAIM_ID_2/
// RESULT_ID 2 (success)
// CLAIM_ID_3/
// RESULT_ID_3 (failed)
// CLAIM_ID_4/
// RESULT_ID_4 (success)
// CLAIM_ID_10/
// RESULT_ID_10 (running)
// RESULT_ID_11 (success)
// CLAIM_ID_20/
// RESULT_ID_20 (failed)
// CLAIM_ID_21/
// NO RESULT YET
//
// outputs/
// RESULT_ID_1/
// RESULT_ID_1_OUTPUT_1
// RESULT_ID_2/
// RESULT_ID_2_OUTPUT_1
// RESULT_ID_2_OUTPUT_2
//
// RESULT_ID_1/
// RESULT_ID_1_OUTPUT_1
// RESULT_ID_2/
// RESULT_ID_2_OUTPUT_1
// RESULT_ID_2_OUTPUT_2
func generateInstallationData(t *testing.T) *TestInstallationProvider {
cp := NewTestInstallationProvider(t)

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 f7ae24c

Please sign in to comment.