Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate tile source from a tile #487

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions internal/acceptance/workflows/using_kiln.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ Feature: As a developer, I want the Kiln CLI to be usable
And stderr contains substring: flag provided but not defined

Examples:
| command |
| bake |
| re-bake |
| fetch |
| find-release-version |
| find-stemcell-version |
| glaze |
| publish |
| release-notes |
| sync-with-local |
| update-release |
| update-stemcell |
| upload-release |
| validate |
| command |
| bake |
| re-bake |
| fetch |
| find-release-version |
| find-stemcell-version |
| glaze |
| new |
| publish |
| release-notes |
| sync-with-local |
| update-release |
| update-stemcell |
| upload-release |
| validate |
45 changes: 45 additions & 0 deletions internal/commands/new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package commands

import (
"github.com/pivotal-cf/jhanda"

"github.com/pivotal-cf/kiln/pkg/bake"
"github.com/pivotal-cf/kiln/pkg/cargo"
)

type New struct {
Options struct {
Dir string `long:"directory" description:"path to directory where source should be written" default:"."`
Tile string `long:"tile" description:"tile path" required:"true"`
Slug string `long:"product-slug" description:"a TanzuNet product slug"`
KilnfileTemplate string `long:"kilnfile-template" description:"a Kilnfile template sets up some reasonable Kilnfile defaults one of [artifactory, bosh.io]" default:"bosh.io"`
}
}

func (n *New) Execute(args []string) error {
k, err := n.Setup(args)
if err != nil {
return err
}
k.Slug = n.Options.Slug
return bake.New(n.Options.Dir, n.Options.Tile, k)
}

func (n *New) Setup(args []string) (cargo.Kilnfile, error) {
if _, err := jhanda.Parse(&n.Options, args); err != nil {
return cargo.Kilnfile{}, err
}
k, err := cargo.KilnfileTemplate(n.Options.KilnfileTemplate)
if err != nil {
return cargo.Kilnfile{}, err
}
return k, nil
}

func (n *New) Usage() jhanda.Usage {
return jhanda.Usage{
Description: "generate tile source from a tile",
ShortDescription: "generate source",
Flags: n.Options,
}
}
83 changes: 83 additions & 0 deletions internal/commands/new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package commands

import (
"github.com/pivotal-cf/kiln/pkg/bake"
"log"
"os"
"path/filepath"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/pivotal-cf/kiln/pkg/cargo"
)

var _ = Describe("new", func() {
var (
directory string
n *New

args []string
)

BeforeEach(func() {
var err error
directory, err = os.MkdirTemp("", "kiln-new-*")
if err != nil {
log.Fatal(err)
}
n = new(New)
})

When("the tile flag is not set", func() {
It("returns an error", func() {
_, err := n.Setup(nil)
Expect(err).To(MatchError(ContainSubstring("--tile")))
})
})

When("only required arguments are set", func() {
BeforeEach(func() {
args = []string{
"--tile", filepath.FromSlash("testdata/tile-0.1.2.pivotal"),
}
})
It("does not error out", func() {
_, err := n.Setup(args)
Expect(err).NotTo(HaveOccurred())
})
It("sets some release sources", func() {
k, _ := n.Setup(args)
Expect(k.ReleaseSources).NotTo(BeEmpty())
})
It("sets the current working directory", func() {
_, _ = n.Setup(args)
Expect(n.Options.Dir).To(Equal("."))
})
It("does not set the product slug", func() {
_, _ = n.Setup(args)
Expect(n.Options.Slug).To(BeEmpty())
})
It("sset the bosh.io release source kilnfile template", func() {
_, _ = n.Setup(args)
Expect(n.Options.KilnfileTemplate).To(Equal(cargo.BOSHReleaseTarballSourceTypeBOSHIO))
})
})

When("generating source", func() {
BeforeEach(func() {
args = []string{
"--tile", filepath.FromSlash("testdata/tile-0.1.3.pivotal"),
"--directory", directory,
}
})
It("generates tile source", func() {
Expect(n.Execute(args)).To(Succeed())

Expect(filepath.Join(directory, bake.DefaultFilepathKilnfile)).To(BeAnExistingFile())
Expect(filepath.Join(directory, bake.DefaultFilepathKilnfileLock)).To(BeAnExistingFile())
Expect(filepath.Join(directory, bake.DefaultFilepathBaseYML)).To(BeAnExistingFile())
Expect(filepath.Join(directory, bake.DefaultFilepathIconImage)).To(BeAnExistingFile())
})
})
})
Binary file added internal/commands/testdata/tile-0.1.3.pivotal
Binary file not shown.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func main() {
commandSet["bake"] = bakeCommand
commandSet["re-bake"] = commands.NewReBake(bakeCommand)

commandSet["new"] = new(commands.New)

commandSet["test"] = commands.NewTileTest()
commandSet["help"] = commands.NewHelp(os.Stdout, globalFlagsUsage, commandSet)
commandSet["version"] = commands.NewVersion(outLogger, version)
Expand Down
17 changes: 17 additions & 0 deletions pkg/bake/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bake

const (
DefaultFilepathIconImage = "icon.png"
DefaultFilepathKilnfile = "Kilnfile"
DefaultFilepathKilnfileLock = DefaultFilepathKilnfile + ".lock"
DefaultFilepathBaseYML = "base.yml"

DefaultDirectoryReleases = "releases"
DefaultDirectoryForms = "forms"
DefaultDirectoryInstanceGroups = "instance_groups"
DefaultDirectoryJobs = "jobs"
DefaultDirectoryMigrations = "migrations"
DefaultDirectoryProperties = "properties"
DefaultDirectoryRuntimeConfigs = "runtime_configs"
DefaultDirectoryBOSHVariables = "bosh_variables"
)
82 changes: 82 additions & 0 deletions pkg/bake/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package bake_test

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/pivotal-cf/kiln/internal/commands"
"github.com/pivotal-cf/kiln/pkg/bake"
)

func TestBakeOptions(t *testing.T) {
options := reflect.TypeOf(commands.BakeOptions{})

for _, tt := range []struct {
Constant string
LongFlag string
}{
{
Constant: bake.DefaultFilepathIconImage,
LongFlag: "icon",
},
{
Constant: bake.DefaultFilepathKilnfile,
LongFlag: "Kilnfile",
},
{
Constant: bake.DefaultFilepathBaseYML,
LongFlag: "metadata",
},
{
Constant: bake.DefaultDirectoryReleases,
LongFlag: "releases-directory",
},
{
Constant: bake.DefaultDirectoryForms,
LongFlag: "forms-directory",
},
{
Constant: bake.DefaultDirectoryInstanceGroups,
LongFlag: "instance-groups-directory",
},
{
Constant: bake.DefaultDirectoryJobs,
LongFlag: "jobs-directory",
},
{
Constant: bake.DefaultDirectoryMigrations,
LongFlag: "migrations-directory",
},
{
Constant: bake.DefaultDirectoryProperties,
LongFlag: "properties-directory",
},
{
Constant: bake.DefaultDirectoryRuntimeConfigs,
LongFlag: "runtime-configs-directory",
},
{
Constant: bake.DefaultDirectoryBOSHVariables,
LongFlag: "bosh-variables-directory",
},
} {
t.Run(tt.Constant, func(t *testing.T) {
field, found := fieldByTag(options, "long", "icon")
require.True(t, found)
require.Equal(t, field.Tag.Get("default"), bake.DefaultFilepathIconImage)
})
}
}

func fieldByTag(tp reflect.Type, tagName, tagValue string) (reflect.StructField, bool) {
for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
value, ok := field.Tag.Lookup(tagName)
if ok && value == tagValue {
return field, true
}
}
return reflect.StructField{}, false
}
Loading
Loading