Skip to content

Commit

Permalink
updates based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw7 committed Nov 1, 2023
1 parent bd3705c commit 34be403
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 31 deletions.
26 changes: 23 additions & 3 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
_ "embed"
"fmt"
"io"
"os"
"path/filepath"

Expand All @@ -23,8 +24,6 @@ type Builder struct {
eibBuildDir string
combustionDir string
combustionScripts []string
rpmFileNames []string
rpmSourceDir string
}

func New(imageConfig *config.ImageConfig, buildConfig *config.BuildConfig) *Builder {
Expand Down Expand Up @@ -95,7 +94,7 @@ func (b *Builder) prepareBuildDir() error {

err := os.MkdirAll(b.combustionDir, os.ModePerm)
if err != nil {
return fmt.Errorf("creating the build directory structure for combustion: %w", err)
return fmt.Errorf("creating the build directory structure: %w", err)
}

return nil
Expand Down Expand Up @@ -180,3 +179,24 @@ func (b *Builder) generateBaseImageFilename() string {
filename := filepath.Join(b.buildConfig.ImageConfigDir, "images", b.imageConfig.Image.BaseImage)
return filename
}

func (b *Builder) copyFile(sourcePath string, destPath string) error {
sourceFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("opening file from source path: %w", err)
}
defer sourceFile.Close()

destFile, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("creating file at dest path: %w", err)
}
defer destFile.Close()

_, err = io.Copy(destFile, sourceFile)
if err != nil {
return fmt.Errorf("copying file from source path to dest path: %w", err)
}

return nil
}
43 changes: 18 additions & 25 deletions pkg/build/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,46 @@ package build

import (
"fmt"
"io"
"os"
"path/filepath"
)

func (b *Builder) getRPMFileNames() error {
b.rpmSourceDir = filepath.Join(b.buildConfig.ImageConfigDir, "rpms")
func (b *Builder) getRPMFileNames() ([]string, error) {
var rpmFileNames []string
rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms")

rpms, err := os.ReadDir(b.rpmSourceDir)
rpms, err := os.ReadDir(rpmSourceDir)
if err != nil {
return fmt.Errorf("reading rpm source dir: %w", err)
return nil, fmt.Errorf("reading rpm source dir: %w", err)
}

for _, rpmFile := range rpms {
b.rpmFileNames = append(b.rpmFileNames, rpmFile.Name())
rpmFileNames = append(rpmFileNames, rpmFile.Name())
}

Check failure on line 21 in pkg/build/rpm.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` `-r 'interface{} -> any'` (gofmt)
if len(rpmFileNames) == 0 {
return nil, fmt.Errorf("no rpms found")
}

return nil
return rpmFileNames, nil
}

func (b *Builder) copyRPMs() error {
err := b.getRPMFileNames()
rpmFileNames, err := b.getRPMFileNames()
if err != nil {
return fmt.Errorf("getting rpm file names: %w", err)
}

for _, rpm := range b.rpmFileNames {
sourcePath := filepath.Join(b.rpmSourceDir, rpm)
destPath := filepath.Join(b.combustionDir, rpm)

sourceFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("opening rpm source path: %w", err)
}
rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms")

destFile, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("opening rpm dest path: %w", err)
}
for _, rpm := range rpmFileNames {
sourcePath := filepath.Join(rpmSourceDir, rpm)
destPath := filepath.Join(b.combustionDir, rpm)

_, err = io.Copy(destFile, sourceFile)
err = b.copyFile(sourcePath, destPath)
if err != nil {
return fmt.Errorf("copying rpm: %w", err)
return fmt.Errorf("looping through rpms to copy: %w", err)
}

sourceFile.Close()
destFile.Close()
}

return nil
Expand Down
69 changes: 66 additions & 3 deletions pkg/build/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,36 @@ func TestGetRPMFileNames(t *testing.T) {
require.NoError(t, err)
defer os.Remove(builder.eibBuildDir)

file1Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm1.rpm")
file2Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm2.rpm")

file1, err := os.Create(file1Path)
require.NoError(t, err)

file2, err := os.Create(file2Path)
require.NoError(t, err)

// Test
err = builder.getRPMFileNames()
rpmFileNames, err := builder.getRPMFileNames()

// Verify
require.NoError(t, err)

assert.Contains(t, builder.rpmFileNames, "rpm1.rpm")
assert.Contains(t, builder.rpmFileNames, "rpm2.rpm")
assert.Contains(t, rpmFileNames, "rpm1.rpm")
assert.Contains(t, rpmFileNames, "rpm2.rpm")

// Cleanup
err = file1.Close()
require.NoError(t, err)

err = file2.Close()
require.NoError(t, err)

err = os.Remove(file1Path)
require.NoError(t, err)

err = os.Remove(file2Path)
require.NoError(t, err)
}

func TestCopyRPMs(t *testing.T) {
Expand All @@ -40,6 +62,15 @@ func TestCopyRPMs(t *testing.T) {
require.NoError(t, err)
defer os.Remove(builder.eibBuildDir)

file1Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm1.rpm")
file2Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm2.rpm")

file1, err := os.Create(file1Path)
require.NoError(t, err)

file2, err := os.Create(file2Path)
require.NoError(t, err)

// Test
err = builder.copyRPMs()

Expand All @@ -51,4 +82,36 @@ func TestCopyRPMs(t *testing.T) {

_, err = os.Stat(filepath.Join(builder.combustionDir, "rpm2.rpm"))
require.NoError(t, err)

// Cleanup
err = file1.Close()
require.NoError(t, err)

err = file2.Close()
require.NoError(t, err)

err = os.Remove(file1Path)
require.NoError(t, err)

err = os.Remove(file2Path)
require.NoError(t, err)
}

func TestGetRPMFileNamesNoRPMs(t *testing.T) {
// Setup
bc := config.BuildConfig{
ImageConfigDir: "../config/testdata",
}
builder := New(nil, &bc)
err := builder.prepareBuildDir()
require.NoError(t, err)
defer os.Remove(builder.eibBuildDir)

// Test
rpmFileNames, err := builder.getRPMFileNames()

// Verify
require.ErrorContains(t, err, "no rpms found")

assert.Empty(t, rpmFileNames)
}
Empty file removed pkg/config/testdata/rpms/rpm1.rpm
Empty file.
Empty file removed pkg/config/testdata/rpms/rpm2.rpm
Empty file.

0 comments on commit 34be403

Please sign in to comment.