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)