Skip to content

Commit 788ca58

Browse files
authored
embed resources and skills directory in binary (#31)
1 parent e29e289 commit 788ca58

17 files changed

Lines changed: 98 additions & 160 deletions

File tree

Containerfile

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,5 @@ FROM registry.access.redhat.com/ubi10/ubi-minimal:10.0
1717

1818
COPY --from=builder /build/patternizer /usr/local/bin/patternizer
1919

20-
ARG PATTERNIZER_RESOURCES_DIR=/tmp/resources
21-
WORKDIR ${PATTERNIZER_RESOURCES_DIR}
22-
23-
COPY resources/* .
24-
25-
ARG PATTERNIZER_SKILLS_DIR=/tmp/skills
26-
WORKDIR ${PATTERNIZER_SKILLS_DIR}
27-
28-
COPY skills/ .
29-
30-
ENV PATTERNIZER_RESOURCES_DIR=${PATTERNIZER_RESOURCES_DIR}
31-
ENV PATTERNIZER_SKILLS_DIR=${PATTERNIZER_SKILLS_DIR}
32-
3320
ENTRYPOINT ["patternizer"]
3421
CMD ["help"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Patternizer
22

3-
![Version: 1.3.1](https://img.shields.io/badge/Version-1.3.1-informational?style=flat-square)
3+
![Version: 2.0.0](https://img.shields.io/badge/Version-2.0.0-informational?style=flat-square)
44
[![Quay Repository](https://img.shields.io/badge/Quay.io-patternizer-blue?logo=quay)](https://quay.io/repository/validatedpatterns/patternizer)
55
[![CI Pipeline](https://github.com/validatedpatterns/patternizer/actions/workflows/build-push.yaml/badge.svg?branch=main)](https://github.com/validatedpatterns/patternizer/actions/workflows/build-push.yaml)
66

src/cmd/cmd_suite_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var (
2020
binaryPath string
2121
resourcesPath string
2222
skillsPath string
23-
projectRoot string
2423
)
2524

2625
func TestCmd(t *testing.T) {
@@ -33,18 +32,16 @@ var _ = BeforeSuite(func() {
3332

3433
wd, err := os.Getwd()
3534
Expect(err).NotTo(HaveOccurred())
36-
projectRoot, err = filepath.Abs(filepath.Join(wd, "..", ".."))
35+
srcRoot, err := filepath.Abs(filepath.Join(wd, ".."))
3736
Expect(err).NotTo(HaveOccurred())
3837

39-
resourcesPath = filepath.Join(projectRoot, "resources")
38+
resourcesPath = filepath.Join(srcRoot, "internal", "embedded", "resources")
4039
Expect(resourcesPath).To(BeADirectory(), "Could not find resources directory")
41-
os.Setenv("PATTERNIZER_RESOURCES_DIR", resourcesPath)
4240

43-
skillsPath = filepath.Join(projectRoot, "skills")
41+
skillsPath = filepath.Join(srcRoot, "internal", "embedded", "skills")
4442
Expect(skillsPath).To(BeADirectory(), "Could not find skills directory")
45-
os.Setenv("PATTERNIZER_SKILLS_DIR", skillsPath)
4643

47-
binaryPath, err = gexec.Build(filepath.Join(projectRoot, "src"))
44+
binaryPath, err = gexec.Build(srcRoot)
4845
Expect(err).NotTo(HaveOccurred())
4946
})
5047

@@ -135,10 +132,6 @@ func createTestDir() string {
135132
func runCLI(dir string, args ...string) *gexec.Session {
136133
cmd := exec.Command(binaryPath, args...)
137134
cmd.Dir = dir
138-
cmd.Env = append(os.Environ(),
139-
"PATTERNIZER_RESOURCES_DIR="+resourcesPath,
140-
"PATTERNIZER_SKILLS_DIR="+skillsPath,
141-
)
142135

143136
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
144137
Expect(err).NotTo(HaveOccurred())

src/cmd/init.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77

8+
"github.com/validatedpatterns/patternizer/internal/embedded"
89
"github.com/validatedpatterns/patternizer/internal/fileutils"
910
"github.com/validatedpatterns/patternizer/internal/helm"
1011
"github.com/validatedpatterns/patternizer/internal/pattern"
@@ -35,44 +36,27 @@ func runInit(withSecrets bool) error {
3536
return fmt.Errorf("error processing cluster group values: %w", err)
3637
}
3738

38-
// Copy pattern.sh and Makefile from resources
39-
resourcesDir, err := fileutils.GetResourcesPath()
40-
if err != nil {
41-
return fmt.Errorf("error getting resource path: %w", err)
42-
}
43-
44-
patternShSrc := filepath.Join(resourcesDir, "pattern.sh")
45-
patternShDst := filepath.Join(repoRoot, "pattern.sh")
46-
if err := fileutils.CopyFile(patternShSrc, patternShDst); err != nil {
39+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/pattern.sh", filepath.Join(repoRoot, "pattern.sh"), 0o755); err != nil {
4740
return fmt.Errorf("error copying pattern.sh: %w", err)
4841
}
4942

50-
// Always copy ansible.cfg to the pattern repo
51-
ansibleCfgSrc := filepath.Join(resourcesDir, "ansible.cfg")
52-
ansibleCfgDst := filepath.Join(repoRoot, "ansible.cfg")
53-
if err := fileutils.CopyFile(ansibleCfgSrc, ansibleCfgDst); err != nil {
43+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/ansible.cfg", filepath.Join(repoRoot, "ansible.cfg"), 0o644); err != nil {
5444
return fmt.Errorf("error copying ansible.cfg: %w", err)
5545
}
5646

57-
// Always copy Makefile-common to the pattern repo
58-
makefilePatternSrc := filepath.Join(resourcesDir, "Makefile-common")
59-
makefilePatternDst := filepath.Join(repoRoot, "Makefile-common")
60-
if err := fileutils.CopyFile(makefilePatternSrc, makefilePatternDst); err != nil {
47+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/Makefile-common", filepath.Join(repoRoot, "Makefile-common"), 0o644); err != nil {
6148
return fmt.Errorf("error copying Makefile-common: %w", err)
6249
}
6350

64-
// Create a simple Makefile that includes Makefile-common (only if it doesn't exist)
65-
makefileSrc := filepath.Join(resourcesDir, "Makefile")
6651
makefileDst := filepath.Join(repoRoot, "Makefile")
6752
if _, err := os.Stat(makefileDst); os.IsNotExist(err) {
68-
if err := fileutils.CopyFile(makefileSrc, makefileDst); err != nil {
53+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/Makefile", makefileDst, 0o644); err != nil {
6954
return fmt.Errorf("error copying Makefile: %w", err)
7055
}
7156
}
7257

73-
// Handle secrets setup if requested
7458
if withSecrets {
75-
if err := fileutils.HandleSecretsSetup(resourcesDir, repoRoot); err != nil {
59+
if err := fileutils.HandleSecretsSetup(embedded.Resources, repoRoot); err != nil {
7660
return fmt.Errorf("error setting up secrets: %w", err)
7761
}
7862
}

src/cmd/upgrade.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77

8+
"github.com/validatedpatterns/patternizer/internal/embedded"
89
"github.com/validatedpatterns/patternizer/internal/fileutils"
910
"github.com/validatedpatterns/patternizer/internal/pattern"
1011
)
@@ -31,43 +32,30 @@ func runUpgrade(replaceMakefile bool) error {
3132
return fmt.Errorf("error removing pattern.sh: %w", err)
3233
}
3334

34-
// Copy resources into repo root
35-
resourcesDir, err := fileutils.GetResourcesPath()
36-
if err != nil {
37-
return fmt.Errorf("error getting resource path: %w", err)
38-
}
39-
40-
// Copy pattern.sh
41-
if err := fileutils.CopyFile(filepath.Join(resourcesDir, "pattern.sh"), patternShPath); err != nil {
35+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/pattern.sh", patternShPath, 0o755); err != nil {
4236
return fmt.Errorf("error copying pattern.sh: %w", err)
4337
}
4438

45-
// Copy Makefile-common
46-
if err := fileutils.CopyFile(filepath.Join(resourcesDir, "Makefile-common"), filepath.Join(repoRoot, "Makefile-common")); err != nil {
39+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/Makefile-common", filepath.Join(repoRoot, "Makefile-common"), 0o644); err != nil {
4740
return fmt.Errorf("error copying Makefile-common: %w", err)
4841
}
4942

50-
// Copy ansible.cfg
51-
if err := fileutils.CopyFile(filepath.Join(resourcesDir, "ansible.cfg"), filepath.Join(repoRoot, "ansible.cfg")); err != nil {
43+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/ansible.cfg", filepath.Join(repoRoot, "ansible.cfg"), 0o644); err != nil {
5244
return fmt.Errorf("error copying ansible.cfg: %w", err)
5345
}
5446

55-
// Makefile handling
56-
makefileSrc := filepath.Join(resourcesDir, "Makefile")
5747
makefileDst := filepath.Join(repoRoot, "Makefile")
5848

5949
if replaceMakefile {
60-
if err := fileutils.CopyFile(makefileSrc, makefileDst); err != nil {
50+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/Makefile", makefileDst, 0o644); err != nil {
6151
return fmt.Errorf("error replacing Makefile: %w", err)
6252
}
6353
} else {
64-
// If Makefile doesn't exist, copy it
6554
if _, err := os.Stat(makefileDst); os.IsNotExist(err) {
66-
if err := fileutils.CopyFile(makefileSrc, makefileDst); err != nil {
55+
if err := fileutils.WriteEmbeddedFile(embedded.Resources, "resources/Makefile", makefileDst, 0o644); err != nil {
6756
return fmt.Errorf("error copying Makefile: %w", err)
6857
}
6958
} else if err == nil {
70-
// If Makefile exists, check for include and prepend if missing
7159
hasInclude, err := fileutils.FileContainsIncludeMakefileCommon(makefileDst)
7260
if err != nil {
7361
return fmt.Errorf("error checking Makefile for include: %w", err)

src/internal/embedded/embedded.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package embedded
2+
3+
import "embed"
4+
5+
//go:embed resources/*
6+
var Resources embed.FS
7+
8+
//go:embed all:skills
9+
var Skills embed.FS

resources/Makefile-common renamed to src/internal/embedded/resources/Makefile-common

File renamed without changes.

resources/ansible.cfg renamed to src/internal/embedded/resources/ansible.cfg

File renamed without changes.

resources/pattern.sh renamed to src/internal/embedded/resources/pattern.sh

File renamed without changes.

0 commit comments

Comments
 (0)