Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-342) Refactor private build package
Browse files Browse the repository at this point in the history
Prior to this commit, the private build package implemented its own private
function to validate if a template config file prior to packaging it.

This commit:

1. Updates the private build package to append a new value to its struct,
   ConfigProcessor, which must be a valid ConfigProcessorI interface.
2. Updates the private build package to replace calls to its own private
   checkConfig() function with calls to ConfigProcessor.CheckConfig.
3. Removes the private checkConfig() function from the private build
   package as it is no longer needed or used.
4. Updates the unit tests for the private build package to reflect the
   modified implementation of ConfigProcessor.CheckConfig()
5. Updates the build command itself to load the private processor.
  • Loading branch information
michaeltlombardi committed Feb 11, 2022
1 parent 2e1b426 commit af5cd70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 52 deletions.
8 changes: 5 additions & 3 deletions cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

"github.com/puppetlabs/pdkgo/internal/pkg/pct"
"github.com/puppetlabs/pdkgo/internal/pkg/pct_config_processor"
"github.com/puppetlabs/pdkgo/pkg/gzip"
"github.com/puppetlabs/pdkgo/pkg/tar"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -33,9 +34,10 @@ func CreateCommand() *cobra.Command {

fs := afero.NewOsFs() // configure afero to use real filesystem
builder = &pct.Builder{
Tar: &tar.Tar{AFS: &afero.Afero{Fs: fs}},
Gzip: &gzip.Gzip{AFS: &afero.Afero{Fs: fs}},
AFS: &afero.Afero{Fs: fs},
Tar: &tar.Tar{AFS: &afero.Afero{Fs: fs}},
Gzip: &gzip.Gzip{AFS: &afero.Afero{Fs: fs}},
AFS: &afero.Afero{Fs: fs},
ConfigProcessor: &pct_config_processor.PctConfigProcessor{AFS: &afero.Afero{Fs: fs}},
}

return tmp
Expand Down
51 changes: 6 additions & 45 deletions internal/pkg/pct/build.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package pct

import (
"bytes"
"fmt"
"os"
"path/filepath"

"github.com/puppetlabs/pdkgo/pkg/config_processor"
"github.com/puppetlabs/pdkgo/pkg/gzip"
"github.com/puppetlabs/pdkgo/pkg/tar"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"github.com/spf13/viper"
)

type BuilderI interface {
Build(templatePath, targetDir string) (gzipArchiveFilePath string, err error)
}

type Builder struct {
Tar tar.TarI
Gzip gzip.GzipI
AFS *afero.Afero
Tar tar.TarI
Gzip gzip.GzipI
AFS *afero.Afero
ConfigProcessor config_processor.ConfigProcessorI
}

func (b *Builder) Build(templatePath, targetDir string) (gzipArchiveFilePath string, err error) {
Expand All @@ -34,7 +34,7 @@ func (b *Builder) Build(templatePath, targetDir string) (gzipArchiveFilePath str
return "", fmt.Errorf("No 'pct-config.yml' found in %v", templatePath)
}

err = b.checkConfig(filepath.Join(templatePath, "pct-config.yml"))
err = b.ConfigProcessor.CheckConfig(filepath.Join(templatePath, "pct-config.yml"))
if err != nil {
return "", fmt.Errorf("Invalid config: %v", err.Error())
}
Expand Down Expand Up @@ -68,42 +68,3 @@ func (b *Builder) Build(templatePath, targetDir string) (gzipArchiveFilePath str

return gzipArchiveFilePath, nil
}

func (b *Builder) checkConfig(configFile string) error {
fileBytes, err := b.AFS.ReadFile(configFile)
if err != nil {
return err
}

var info PuppetContentTemplateInfo
viper.SetConfigType("yaml")

err = viper.ReadConfig(bytes.NewBuffer(fileBytes))
if err != nil {
return err
}

err = viper.Unmarshal(&info)
if err != nil {
return err
}

msg := "The following attributes are missing in pct-config.yml:\n"
orig := msg
// These parts are essential for build and deployment.

if info.Template.Id == "" {
msg = msg + " * id\n"
}
if info.Template.Author == "" {
msg = msg + " * author\n"
}
if info.Template.Version == "" {
msg = msg + " * version\n"
}
if msg != orig {
return fmt.Errorf(msg)
}

return nil
}
12 changes: 8 additions & 4 deletions internal/pkg/pct/build_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package pct_test

import (
"fmt"
"path/filepath"
"testing"

"github.com/puppetlabs/pdkgo/internal/pkg/pct"
"github.com/puppetlabs/pdkgo/internal/pkg/pct_config_processor"
"github.com/puppetlabs/pdkgo/pkg/mock"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
Expand All @@ -18,6 +20,7 @@ func TestBuild(t *testing.T) {
}

var mockTemplateDir = "/path/to/my/cool-template"
var mockConfigFilePath = filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml"))

tests := []struct {
name string
Expand Down Expand Up @@ -160,7 +163,7 @@ template:
version: 1.0.0
`,
},
expectedErr: "Invalid config: The following attributes are missing in pct-config.yml:\n * id\n",
expectedErr: fmt.Sprintf("Invalid config: The following attributes are missing in %s:\n * id\n", mockConfigFilePath),
mockTarErr: false,
},
{
Expand All @@ -180,7 +183,7 @@ template:
version: 1.0.0
`,
},
expectedErr: "Invalid config: The following attributes are missing in pct-config.yml:\n * author\n",
expectedErr: fmt.Sprintf("Invalid config: The following attributes are missing in %s:\n * author\n", mockConfigFilePath),
mockTarErr: false,
},
{
Expand All @@ -200,7 +203,7 @@ template:
author: puppetlabs
`,
},
expectedErr: "Invalid config: The following attributes are missing in pct-config.yml:\n * version\n",
expectedErr: fmt.Sprintf("Invalid config: The following attributes are missing in %s:\n * version\n", mockConfigFilePath),
mockTarErr: false,
},
{
Expand All @@ -219,7 +222,7 @@ template:
foo: bar
`,
},
expectedErr: "Invalid config: The following attributes are missing in pct-config.yml:\n * id\n * author\n * version\n",
expectedErr: fmt.Sprintf("Invalid config: The following attributes are missing in %s:\n * id\n * author\n * version\n", mockConfigFilePath),
mockTarErr: false,
},
}
Expand All @@ -242,6 +245,7 @@ template:
&mock.Tar{ReturnedPath: tt.tarFile, ErrResponse: tt.mockTarErr},
&mock.Gzip{ReturnedPath: tt.gzipFile, ErrResponse: tt.mockGzipErr},
afs,
&pct_config_processor.PctConfigProcessor{AFS: afs},
}

gotGzipArchiveFilePath, err := p.Build(tt.args.templatePath, tt.args.targetDir)
Expand Down

0 comments on commit af5cd70

Please sign in to comment.