From 994f028d494ca133b63af8a1de9409f29b99aee8 Mon Sep 17 00:00:00 2001 From: Michael T Lombardi Date: Fri, 11 Feb 2022 15:50:44 -0600 Subject: [PATCH] (GH-342) Refactor private build package 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. --- cmd/build/build.go | 8 ++++-- internal/pkg/pct/build.go | 51 ++++------------------------------ internal/pkg/pct/build_test.go | 12 +++++--- 3 files changed, 19 insertions(+), 52 deletions(-) diff --git a/cmd/build/build.go b/cmd/build/build.go index 4d7ede37..46d1d282 100644 --- a/cmd/build/build.go +++ b/cmd/build/build.go @@ -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" @@ -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 diff --git a/internal/pkg/pct/build.go b/internal/pkg/pct/build.go index f971f9d2..d63fdad4 100644 --- a/internal/pkg/pct/build.go +++ b/internal/pkg/pct/build.go @@ -1,16 +1,15 @@ 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 { @@ -18,9 +17,10 @@ type BuilderI interface { } 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) { @@ -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()) } @@ -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 -} diff --git a/internal/pkg/pct/build_test.go b/internal/pkg/pct/build_test.go index 9ab14fce..0dda5955 100644 --- a/internal/pkg/pct/build_test.go +++ b/internal/pkg/pct/build_test.go @@ -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" @@ -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 @@ -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, }, { @@ -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, }, { @@ -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, }, { @@ -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, }, } @@ -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)