Skip to content

Commit

Permalink
adding functionality to port create in a specified directory
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskar Bhushan <[email protected]>
  • Loading branch information
sbdtu5498 committed Jun 20, 2023
1 parent 390a88d commit ddceee7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 25 deletions.
17 changes: 13 additions & 4 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ func buildBundleCommands(p *porter.Porter) *cobra.Command {
}

func buildBundleCreateCommand(p *porter.Porter) *cobra.Command {
return &cobra.Command{
Use: "create",
cmd := &cobra.Command{
Use: "create [bundle-name]",
Short: "Create a bundle",
Long: "Create a bundle. This generates a porter bundle in the current directory.",
Long: "Create a bundle. This generates a porter bundle with the directory with the specified name or in the root directory if no name is provided.",
Args: cobra.MaximumNArgs(1), // Expect at most one argument for the bundle name
RunE: func(cmd *cobra.Command, args []string) error {
return p.Create()
bundleName := "" // Default value

if len(args) > 0 {
bundleName = args[0]
}

return p.Create(bundleName)
},
}

return cmd
}

func buildBundleBuildCommand(p *porter.Porter) *cobra.Command {
Expand Down
4 changes: 2 additions & 2 deletions docs/content/cli/bundles_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Create a bundle

### Synopsis

Create a bundle. This generates a porter bundle in the current directory.
Create a bundle. This generates a porter bundle with the directory with the specified name or in the root directory if no name is provided.

```
porter bundles create [flags]
porter bundles create [bundle-name] [flags]
```

### Options
Expand Down
4 changes: 2 additions & 2 deletions docs/content/cli/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Create a bundle

### Synopsis

Create a bundle. This generates a porter bundle in the current directory.
Create a bundle. This generates a porter bundle with the directory with the specified name or in the root directory if no name is provided.

```
porter create [flags]
porter create [bundle-name] [flags]
```

### Options
Expand Down
2 changes: 0 additions & 2 deletions docs/content/cli/explain.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Explain a bundle

Explain how to use a bundle by printing the parameters, credentials, outputs, actions.

The [Custom](../bundle/manifest/_index.md#custom) section data is only shown when the output format is JSON or YAML. Because the data can have an arbitrary structure, there is no coherent way to display it in a table.

```
porter explain REFERENCE [flags]
```
Expand Down
29 changes: 19 additions & 10 deletions pkg/porter/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,46 @@ import (
"get.porter.sh/porter/pkg/config"
)

func (p *Porter) Create() error {
fmt.Fprintln(p.Out, "creating porter configuration in the current directory")
func (p *Porter) Create(bundleName string) error {
if bundleName == "" {
bundleName = "."
}

fmt.Fprintf(p.Out, "creating porter configuration for %s\n", bundleName)

err := p.CopyTemplate(p.Templates.GetManifest, config.Name)
// Create a directory with the given bundle name
err := os.MkdirAll(bundleName, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory for bundle: %w", err)
}

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

err = p.CopyTemplate(p.Templates.GetManifestHelpers, "helpers.sh")
err = p.CopyTemplate(p.Templates.GetManifestHelpers, filepath.Join(bundleName, "helpers.sh"))
if err != nil {
return err
}

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

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

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

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

func (p *Porter) CopyTemplate(getTemplate func() ([]byte, error), dest string) error {
tmpl, err := getTemplate()
if err != nil {
Expand All @@ -56,4 +65,4 @@ func (p *Porter) CopyTemplate(getTemplate func() ([]byte, error), dest string) e
return fmt.Errorf("failed to write template to %s: %w", dest, err)
}
return nil
}
}
42 changes: 37 additions & 5 deletions pkg/porter/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"github.com/stretchr/testify/require"
)

func TestCreate(t *testing.T) {
func TestCreateInRootDirectory(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

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

configFileStats, err := p.FileSystem.Stat("porter.yaml")
// Verify that files are present in the root directory
configFileStats, err := p.FileSystem.Stat("config.yaml")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "porter.yaml", pkg.FileModeWritable, configFileStats.Mode())
tests.AssertFilePermissionsEqual(t, "config.yaml", pkg.FileModeWritable, configFileStats.Mode())

// Verify that helpers is present and executable
helperFileStats, err := p.FileSystem.Stat("helpers.sh")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "helpers.sh", pkg.FileModeExecutable, helperFileStats.Mode())
Expand All @@ -39,5 +39,37 @@ func TestCreate(t *testing.T) {
dockerignoreStats, err := p.FileSystem.Stat(".dockerignore")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, ".dockerignore", pkg.FileModeWritable, dockerignoreStats.Mode())
}

func TestCreateInDirectory(t *testing.T) {
p := NewTestPorter(t)
defer p.Close()

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

// Verify that files are present in the "mybundle" directory
configFileStats, err := p.FileSystem.Stat("mybundle/config.yaml")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "mybundle/config.yaml", pkg.FileModeWritable, configFileStats.Mode())

helperFileStats, err := p.FileSystem.Stat("mybundle/helpers.sh")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "mybundle/helpers.sh", pkg.FileModeExecutable, helperFileStats.Mode())

dockerfileStats, err := p.FileSystem.Stat("mybundle/template.Dockerfile")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "mybundle/template.Dockerfile", pkg.FileModeWritable, dockerfileStats.Mode())

readmeStats, err := p.FileSystem.Stat("mybundle/README.md")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "mybundle/README.md", pkg.FileModeWritable, readmeStats.Mode())

gitignoreStats, err := p.FileSystem.Stat("mybundle/.gitignore")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "mybundle/.gitignore", pkg.FileModeWritable, gitignoreStats.Mode())

dockerignoreStats, err := p.FileSystem.Stat("mybundle/.dockerignore")
require.NoError(t, err)
tests.AssertFilePermissionsEqual(t, "mybundle/.dockerignore", pkg.FileModeWritable, dockerignoreStats.Mode())
}

0 comments on commit ddceee7

Please sign in to comment.