From 6e4514129015fce4432258c61f95ac15df9d187f Mon Sep 17 00:00:00 2001 From: Christopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Tue, 26 Mar 2024 20:00:53 -0700 Subject: [PATCH 1/2] add "bake.New" function to split a into defaults based tile source --- pkg/bake/defaults.go | 17 +++ pkg/bake/defaults_test.go | 82 +++++++++++ pkg/bake/new.go | 200 +++++++++++++++++++++++++++ pkg/bake/new_internal_test.go | 79 +++++++++++ pkg/bake/new_test.go | 45 ++++++ pkg/bake/testdata/tile-0.1.3.pivotal | Bin 0 -> 9439 bytes 6 files changed, 423 insertions(+) create mode 100644 pkg/bake/defaults.go create mode 100644 pkg/bake/defaults_test.go create mode 100644 pkg/bake/new.go create mode 100644 pkg/bake/new_internal_test.go create mode 100644 pkg/bake/new_test.go create mode 100644 pkg/bake/testdata/tile-0.1.3.pivotal diff --git a/pkg/bake/defaults.go b/pkg/bake/defaults.go new file mode 100644 index 00000000..1617d428 --- /dev/null +++ b/pkg/bake/defaults.go @@ -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" +) diff --git a/pkg/bake/defaults_test.go b/pkg/bake/defaults_test.go new file mode 100644 index 00000000..d6c05581 --- /dev/null +++ b/pkg/bake/defaults_test.go @@ -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 +} diff --git a/pkg/bake/new.go b/pkg/bake/new.go new file mode 100644 index 00000000..2c6f20f5 --- /dev/null +++ b/pkg/bake/new.go @@ -0,0 +1,200 @@ +package bake + +import ( + "archive/zip" + "encoding/base64" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/crhntr/yamlutil/yamlnode" + "gopkg.in/yaml.v3" + + "github.com/pivotal-cf/kiln/pkg/cargo" + "github.com/pivotal-cf/kiln/pkg/tile" +) + +const ( + fileMode = 0644 + dirMode = 0744 + + gitKeepFilename = ".gitkeep" + defaultGitIgnore = + /* language=gitignore */ ` +*.pivotal +releases/*.tgz +releases/*.tar.gz +*.out +` +) + +func New(outputDirectory, tilePath string, spec cargo.Kilnfile) error { + f, openErr := os.Open(tilePath) + info, statErr := os.Stat(tilePath) + if err := errors.Join(statErr, openErr); err != nil { + return err + } + defer closeAndIgnoreError(f) + return newFromReader(outputDirectory, f, info.Size(), spec) +} + +func newFromReader(outputDirectory string, r io.ReaderAt, size int64, spec cargo.Kilnfile) error { + zr, err := zip.NewReader(r, size) + if err != nil { + return err + } + return newFromFS(outputDirectory, zr, spec) +} + +func newFromFS(outputDirectory string, dir fs.FS, spec cargo.Kilnfile) error { + productTemplateBuffer, err := tile.ReadMetadataFromFS(dir) + if err != nil { + return err + } + productTemplate, err := newFromProductTemplate(outputDirectory, productTemplateBuffer) + if err != nil { + return err + } + if err := extractMigrations(outputDirectory, dir); err != nil { + return err + } + releaseLocks, err := extractReleases(outputDirectory, productTemplate, dir) + if err != nil { + return err + } + if err := newKilnfiles(outputDirectory, spec, releaseLocks); err != nil { + return err + } + baseYML, err := yaml.Marshal(productTemplate) + if err != nil { + return err + } + if err := os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathBaseYML), baseYML, fileMode); err != nil { + return err + } + if err := os.WriteFile(filepath.Join(outputDirectory, ".gitignore"), []byte(defaultGitIgnore), fileMode); err != nil { + return err + } + return nil +} + +func newKilnfiles(outputDirectory string, spec cargo.Kilnfile, releaseTarballs []cargo.BOSHReleaseTarball) error { + var lock cargo.KilnfileLock + for _, tarball := range releaseTarballs { + lock.Releases = append(lock.Releases, cargo.BOSHReleaseTarballLock{ + Name: tarball.Manifest.Name, + Version: tarball.Manifest.Version, + SHA1: tarball.SHA1, + }) + } + slices.SortFunc(lock.Releases, func(a, b cargo.BOSHReleaseTarballLock) int { + return strings.Compare(a.Name, b.Name) + }) + spec.Releases = spec.Releases[:0] + for _, lock := range spec.Releases { + spec.Releases = append(spec.Releases, cargo.BOSHReleaseTarballSpecification{ + Name: lock.Name, + Version: lock.Version, + DeGlazeBehavior: cargo.LockPatch, + FloatAlways: false, + }) + } + kilnfileLock, err := yaml.Marshal(lock) + if err != nil { + return err + } + return os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathKilnfileLock), kilnfileLock, fileMode) +} + +func newFromProductTemplate(outputDirectory string, productTemplate []byte) (*yaml.Node, error) { + var productTemplateNode yaml.Node + if err := yaml.Unmarshal(productTemplate, &productTemplateNode); err != nil { + return &productTemplateNode, fmt.Errorf("failed to parse product template: %w", err) + } + return &productTemplateNode, errors.Join(writeIconPNG(outputDirectory, &productTemplateNode)) +} + +func extractMigrations(outputDirectory string, dir fs.FS) error { + migrations, err := fs.Glob(dir, "migrations/*.js") + if err != nil { + return err + } + for _, migration := range migrations { + outPath := filepath.Join(outputDirectory, filepath.FromSlash(migration)) + if err := copyFile(outPath, dir, migration); err != nil { + return err + + } + } + return nil +} + +func extractReleases(outputDirectory string, productTemplate *yaml.Node, dir fs.FS) ([]cargo.BOSHReleaseTarball, error) { + releases, err := fs.Glob(dir, "releases/*.tgz") + if err != nil { + return nil, err + } + var tarballs []cargo.BOSHReleaseTarball + + if err := os.MkdirAll(filepath.Join(outputDirectory, DefaultDirectoryReleases), dirMode); err != nil { + return nil, err + } + if err := os.WriteFile(filepath.Join(outputDirectory, DefaultDirectoryReleases, gitKeepFilename), nil, fileMode); err != nil { + return nil, err + } + for _, release := range releases { + outPath := filepath.Join(outputDirectory, filepath.FromSlash(release)) + if err := copyFile(outPath, dir, release); err != nil { + return nil, err + } + releaseTarball, err := cargo.OpenBOSHReleaseTarball(outPath) + if err != nil { + return nil, err + } + tarballs = append(tarballs, releaseTarball) + } + releasesNode, found := yamlnode.LookupKey(productTemplate, "releases") + if !found { + return nil, err + } + var releasesList []string + for _, tarball := range tarballs { + releasesList = append(releasesList, fmt.Sprintf("{{ release %q }}", tarball.Manifest.Name)) + } + return tarballs, releasesNode.Encode(&releasesList) +} + +func copyFile(out string, dir fs.FS, p string) error { + srcFile, err := dir.Open(p) + if err != nil { + return err + } + defer closeAndIgnoreError(srcFile) + + dstFile, err := os.Create(out) + if err != nil { + return err + } + defer closeAndIgnoreError(dstFile) + + _, err = io.Copy(dstFile, srcFile) + return err +} + +func writeIconPNG(outputDirectory string, productTemplate *yaml.Node) error { + iconImageNode, found := yamlnode.LookupKey(productTemplate, "icon_image") + if !found { + return fmt.Errorf("icon_image not found in product template") + } + iconImage, err := base64.StdEncoding.DecodeString(strings.TrimSpace(iconImageNode.Value)) + if err != nil { + return fmt.Errorf("failed to decode icon_image: %w", err) + } + iconImageNode.Value = `{{ icon }}` + return os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathIconImage), iconImage, fileMode) +} diff --git a/pkg/bake/new_internal_test.go b/pkg/bake/new_internal_test.go new file mode 100644 index 00000000..efd9a7c1 --- /dev/null +++ b/pkg/bake/new_internal_test.go @@ -0,0 +1,79 @@ +package bake + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" + + "github.com/pivotal-cf/kiln/pkg/cargo" +) + +func Test_shatterFromFS(t *testing.T) { + t.Run("missing product template", func(t *testing.T) { + zip := os.DirFS(t.TempDir()) + output := t.TempDir() + err := newFromFS(output, zip, cargo.Kilnfile{}) + require.ErrorContains(t, err, "metadata file not found") + }) +} + +func Test_shatterProductTemplate(t *testing.T) { + t.Run("when the product template is valid", func(t *testing.T) { + const productTemplateYAML = `}:` + output := t.TempDir() + _, err := newFromProductTemplate(output, []byte(productTemplateYAML)) + require.ErrorContains(t, err, "failed to parse product template") + }) +} + +func Test_writeIconPNG(t *testing.T) { + t.Run("when the field is valid base64", func(t *testing.T) { + const productTemplateYAML = + /* language=yaml */ `--- +icon_image: cmVsYXRpbmcgdG8gb3Igb2YgdGhlIG5hdHVyZSBvZiBhbiBpY29u +` + output := t.TempDir() + productTemplate := parseProductTemplateNode(t, productTemplateYAML) + err := writeIconPNG(output, productTemplate) + require.NoError(t, err) + + expOutput := filepath.Join(output, DefaultFilepathIconImage) + require.FileExists(t, expOutput) + buf, err := os.ReadFile(expOutput) + require.NoError(t, err) + require.Equal(t, "relating to or of the nature of an icon", string(buf), + "it gets written to the file") + }) + + t.Run("missing icon", func(t *testing.T) { + const productTemplateYAML = + /* language=yaml */ `--- +ping: pong +` + output := t.TempDir() + productTemplate := parseProductTemplateNode(t, productTemplateYAML) + err := writeIconPNG(output, productTemplate) + require.ErrorContains(t, err, "icon_image not found in product template") + }) + + t.Run("not base64", func(t *testing.T) { + const productTemplateYAML = + /* language=yaml */ `--- +icon_image: $ +` + output := t.TempDir() + productTemplate := parseProductTemplateNode(t, productTemplateYAML) + err := writeIconPNG(output, productTemplate) + require.ErrorContains(t, err, "failed to decode icon_image") + }) +} + +func parseProductTemplateNode(t *testing.T, productTemplateYAML string) *yaml.Node { + t.Helper() + var productTemplate yaml.Node + require.NoError(t, yaml.Unmarshal([]byte(productTemplateYAML), &productTemplate)) + return &productTemplate +} diff --git a/pkg/bake/new_test.go b/pkg/bake/new_test.go new file mode 100644 index 00000000..bcce80cf --- /dev/null +++ b/pkg/bake/new_test.go @@ -0,0 +1,45 @@ +package bake_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" + + "github.com/pivotal-cf/kiln/pkg/bake" + "github.com/pivotal-cf/kiln/pkg/cargo" +) + +func TestNew(t *testing.T) { + outputDirectory := t.TempDir() + + err := bake.New(outputDirectory, filepath.FromSlash("testdata/tile-0.1.3.pivotal"), cargo.Kilnfile{}) + require.NoError(t, err) + + iconFileContents, _ := os.ReadFile(filepath.Join(outputDirectory, bake.DefaultFilepathIconImage)) + require.Equal(t, "some icon\n", string(iconFileContents), "it writes the icon image") + + baseYML, _ := os.ReadFile(filepath.Join(outputDirectory, bake.DefaultFilepathBaseYML)) + var productTemplate yaml.Node + require.NoError(t, yaml.Unmarshal(baseYML, &productTemplate)) + + // do kiln bake in outputDirectory and see if it outputs the same tile +} + +func TestNew_bad_inputs(t *testing.T) { + t.Run("not a zip file", func(t *testing.T) { + outputDirectory := t.TempDir() + const notAZipFile = "new_test.go" + err := bake.New(outputDirectory, notAZipFile, cargo.Kilnfile{}) + require.ErrorContains(t, err, "zip") + }) + + t.Run("file does not exist", func(t *testing.T) { + outputDirectory := t.TempDir() + + err := bake.New(outputDirectory, filepath.FromSlash("testdata/banana.pivotal"), cargo.Kilnfile{}) + require.ErrorContains(t, err, "no such file") + }) +} diff --git a/pkg/bake/testdata/tile-0.1.3.pivotal b/pkg/bake/testdata/tile-0.1.3.pivotal new file mode 100644 index 0000000000000000000000000000000000000000..5d75019e3aac1fc59475c6947e46ab5ea2122a48 GIT binary patch literal 9439 zcmb7q1z40_)Bgg3l!B!6f*>f}OG<-;bmy`xuypLwDJb0`-69|$ji7{dNC^lCQVSxX zbV_`S==1nI*Z2P4|97+Z#hy8HW_~kgX3o9m+-gb~*GK@^007`imX{vt-(MFg00&?X zg@Y}?a4_fZFC3oscG_B)0JOMp3`+~BE5rrn1cx~~2m>{tE^bg)AQ;G?0JXDo1nN4v z*jX?DT`$Ql9JrQ_F7~EyPba9WFwoE#7Y1>3FooHJt)Rj{2#*llKvT*|pGOEGZ6zj# zYvXACixrn0_zxutT3TvXnp|*!KnqU?ussZ73Ud+$a-rHV2Uj@Q0RlC(fLg*FU_Wu8 z$Uq=UnI+5$;Q}_dL+OLNAfUg=u5cGH%mMBy{F49#+JilWf!x0vFqB=Ff>ElVmSBV( z{CA%n*c@sn41D-Yq3cf`6d08B-yp7Nmlj{r;7}{*CFEZNe>&^3jnZcdb1=2EgIQVs zl71z|`qGK)SKdSMxjh1j~dj)73K7%8$my@1|9?dP!3Gyj^FpUq#ckyg|G!ygbQ#Lw2ho$yzX z{5#5AVGdSyP*bp%siT9P=U*9s$`<&q^tlXqIMm+B4h%imEZLKg$pghad+Ru01OK?Gg6B$)>34V~TLHasgYQ z^mB1=qgehxiHkDI0s(>lX1`+Ly(C}S>S=0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<{SBSpvi{SN=-;viP{*hL@qqN7fc|`7xI7~L`$9*-|8EDTzajlT oaQ)Nt%ZH4=ubb3=Nc*pk9BN9KC=&pHYp6d#Gyov}&SmR=04ef$O8@`> literal 0 HcmV?d00001 From 8fea70086d1b0fef76653a8e66cbf5c9e19da8e5 Mon Sep 17 00:00:00 2001 From: Christopher Hunter Date: Wed, 27 Mar 2024 11:50:00 -0700 Subject: [PATCH 2/2] add new command --- .../acceptance/workflows/using_kiln.feature | 29 +++--- internal/commands/new.go | 45 ++++++++++ internal/commands/new_test.go | 83 ++++++++++++++++++ internal/commands/testdata/tile-0.1.3.pivotal | Bin 0 -> 9439 bytes main.go | 2 + pkg/bake/new.go | 15 +++- pkg/cargo/kilnfile.go | 44 ++++++++++ pkg/cargo/kilnfile_test.go | 17 ++++ 8 files changed, 217 insertions(+), 18 deletions(-) create mode 100644 internal/commands/new.go create mode 100644 internal/commands/new_test.go create mode 100644 internal/commands/testdata/tile-0.1.3.pivotal diff --git a/internal/acceptance/workflows/using_kiln.feature b/internal/acceptance/workflows/using_kiln.feature index 77de2328..1df2fbb3 100644 --- a/internal/acceptance/workflows/using_kiln.feature +++ b/internal/acceptance/workflows/using_kiln.feature @@ -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 | \ No newline at end of file + | 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 | \ No newline at end of file diff --git a/internal/commands/new.go b/internal/commands/new.go new file mode 100644 index 00000000..7c681437 --- /dev/null +++ b/internal/commands/new.go @@ -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, + } +} diff --git a/internal/commands/new_test.go b/internal/commands/new_test.go new file mode 100644 index 00000000..0dd05497 --- /dev/null +++ b/internal/commands/new_test.go @@ -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()) + }) + }) +}) diff --git a/internal/commands/testdata/tile-0.1.3.pivotal b/internal/commands/testdata/tile-0.1.3.pivotal new file mode 100644 index 0000000000000000000000000000000000000000..5d75019e3aac1fc59475c6947e46ab5ea2122a48 GIT binary patch literal 9439 zcmb7q1z40_)Bgg3l!B!6f*>f}OG<-;bmy`xuypLwDJb0`-69|$ji7{dNC^lCQVSxX zbV_`S==1nI*Z2P4|97+Z#hy8HW_~kgX3o9m+-gb~*GK@^007`imX{vt-(MFg00&?X zg@Y}?a4_fZFC3oscG_B)0JOMp3`+~BE5rrn1cx~~2m>{tE^bg)AQ;G?0JXDo1nN4v z*jX?DT`$Ql9JrQ_F7~EyPba9WFwoE#7Y1>3FooHJt)Rj{2#*llKvT*|pGOEGZ6zj# zYvXACixrn0_zxutT3TvXnp|*!KnqU?ussZ73Ud+$a-rHV2Uj@Q0RlC(fLg*FU_Wu8 z$Uq=UnI+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lK(gWO-6Z#7L;(XtZqYdz<|x`aYX$Vhm>igZFWp zd;%<(uH9!c$1}(bL$gtoi6F3MDxt*htI5@mBee=;!1~TNY)2-c`K-!Ql@+azdZdv< zUoMbfJHG5$p0#!UX)hZ&|4Fev_8tR_-Q@JGa~tMBZK*`bW))u3P?~ImgBsIXVPDGP z?VM7x@fVAH~2(NvXiO!;9je3+K@IsfAM&!Vnh4VP{cqny9v0Ubh&X z&ddX5Wp=kWETSdu+$`MHn?u{p}7SNfHW<2WUHS^cqQp=_X(U_;&5F()D!c?>?>=*PMXT{c*3m4#z1C+DBn?-TI<{SBSpvi{SN=-;viP{*hL@qqN7fc|`7xI7~L`$9*-|8EDTzajlT oaQ)Nt%ZH4=ubb3=Nc*pk9BN9KC=&pHYp6d#Gyov}&SmR=04ef$O8@`> literal 0 HcmV?d00001 diff --git a/main.go b/main.go index 52e9d730..4e063c6f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/pkg/bake/new.go b/pkg/bake/new.go index 2c6f20f5..73496099 100644 --- a/pkg/bake/new.go +++ b/pkg/bake/new.go @@ -104,11 +104,18 @@ func newKilnfiles(outputDirectory string, spec cargo.Kilnfile, releaseTarballs [ FloatAlways: false, }) } - kilnfileLock, err := yaml.Marshal(lock) + lockBuf, err := yaml.Marshal(lock) if err != nil { return err } - return os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathKilnfileLock), kilnfileLock, fileMode) + specBuf, err := yaml.Marshal(spec) + if err != nil { + return err + } + return errors.Join( + os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathKilnfileLock), lockBuf, fileMode), + os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathKilnfile), specBuf, fileMode), + ) } func newFromProductTemplate(outputDirectory string, productTemplate []byte) (*yaml.Node, error) { @@ -164,7 +171,7 @@ func extractReleases(outputDirectory string, productTemplate *yaml.Node, dir fs. } var releasesList []string for _, tarball := range tarballs { - releasesList = append(releasesList, fmt.Sprintf("{{ release %q }}", tarball.Manifest.Name)) + releasesList = append(releasesList, fmt.Sprintf("$( release %q )", tarball.Manifest.Name)) } return tarballs, releasesNode.Encode(&releasesList) } @@ -195,6 +202,6 @@ func writeIconPNG(outputDirectory string, productTemplate *yaml.Node) error { if err != nil { return fmt.Errorf("failed to decode icon_image: %w", err) } - iconImageNode.Value = `{{ icon }}` + iconImageNode.Value = `$( icon )` return os.WriteFile(filepath.Join(outputDirectory, DefaultFilepathIconImage), iconImage, fileMode) } diff --git a/pkg/cargo/kilnfile.go b/pkg/cargo/kilnfile.go index 4622543e..484f1b78 100644 --- a/pkg/cargo/kilnfile.go +++ b/pkg/cargo/kilnfile.go @@ -371,3 +371,47 @@ type BakeConfiguration struct { EmbedPaths []string `yaml:"embed_paths,omitempty" json:"embed_paths,omitempty"` VariableFiles []string `yaml:"variable_files,omitempty" json:"variable_files,omitempty"` } + +func KilnfileTemplate(name string) (Kilnfile, error) { + const ( + artifactory = BOSHReleaseTarballSourceTypeArtifactory + boshIO = BOSHReleaseTarballSourceTypeBOSHIO + ) + switch name { + default: + return Kilnfile{}, fmt.Errorf("unknown Kilnfile template please use one of %s", strings.Join([]string{ + artifactory, boshIO, + }, ", ")) + case artifactory: + return Kilnfile{ + ReleaseSources: []ReleaseSourceConfig{ + { + Type: BOSHReleaseTarballSourceTypeArtifactory, + ID: "compiled-releases", + Publishable: true, + ArtifactoryHost: `{{ variable "artifactory_host" }}`, + Repo: `{{ variable "artifactory_repository" }}`, + Username: `{{ variable "artifactory_username" }}`, + Password: `{{ variable "artifactory_password" }}`, + PathTemplate: `compiled-releases/{{.Name}}/{{.Name}}-{{.Version}}-{{.StemcellOS}}-{{.StemcellVersion}}.tgz`, + }, + { + Type: BOSHReleaseTarballSourceTypeArtifactory, + ID: "ingested-releases", + Publishable: true, + ArtifactoryHost: `{{ variable "artifactory_host" }}`, + Repo: `{{ variable "artifactory_repository" }}`, + Username: `{{ variable "artifactory_username" }}`, + Password: `{{ variable "artifactory_password" }}`, + PathTemplate: `bosh-releases/{{.Name}}/{{.Name}}-{{.Version}}.tgz`, + }, + }, + }, nil + case boshIO: + return Kilnfile{ + ReleaseSources: []ReleaseSourceConfig{ + {Type: BOSHReleaseTarballSourceTypeBOSHIO}, + }, + }, nil + } +} diff --git a/pkg/cargo/kilnfile_test.go b/pkg/cargo/kilnfile_test.go index 94d12940..b8dfe450 100644 --- a/pkg/cargo/kilnfile_test.go +++ b/pkg/cargo/kilnfile_test.go @@ -361,3 +361,20 @@ func Test_deGlazeBOSHReleaseTarballSpecification(t *testing.T) { }) } } + +func TestKilnfileTemplate(t *testing.T) { + for _, name := range []string{ + "artifactory", + "bosh.io", + } { + t.Run(name, func(t *testing.T) { + k, err := KilnfileTemplate(name) + assert.NoError(t, err) + assert.NotEmpty(t, k.ReleaseSources) + }) + } + t.Run("banana", func(t *testing.T) { + _, err := KilnfileTemplate("banana") + assert.Error(t, err) + }) +}