diff --git a/internal/pkg/pct_config_processor/pct_config_processor.go b/internal/pkg/pct_config_processor/pct_config_processor.go index f2a1fd6a..25a99598 100644 --- a/internal/pkg/pct_config_processor/pct_config_processor.go +++ b/internal/pkg/pct_config_processor/pct_config_processor.go @@ -3,9 +3,9 @@ package pct_config_processor import ( "bytes" "fmt" - "path/filepath" "github.com/puppetlabs/pdkgo/internal/pkg/pct" + "github.com/puppetlabs/pdkgo/pkg/config_processor" "github.com/spf13/afero" "github.com/spf13/viper" ) @@ -14,23 +14,27 @@ type PctConfigProcessor struct { AFS *afero.Afero } -func (p *PctConfigProcessor) ProcessConfig(sourceDir, targetDir string, force bool) (string, error) { - // Read config to determine template properties - info, err := p.readConfig(filepath.Join(sourceDir, "pct-config.yml")) +func (p *PctConfigProcessor) GetConfigMetadata(configFile string) (metadata config_processor.ConfigMetadata, err error) { + configInfo, err := p.ReadConfig(configFile) if err != nil { - return "", fmt.Errorf("Invalid config: %v", err.Error()) + return metadata, err } - // Create namespaced directory and move contents of temp folder to it - namespacedPath, err := p.setupTemplateNamespace(targetDir, info, sourceDir, force) + err = p.CheckConfig(configFile) if err != nil { - return "", fmt.Errorf("Unable to install in namespace: %v", err.Error()) + return metadata, err } - return namespacedPath, nil + + metadata = config_processor.ConfigMetadata{ + Author: configInfo.Template.Author, + Id: configInfo.Template.Id, + Version: configInfo.Template.Version, + } + return metadata, nil } func (p *PctConfigProcessor) CheckConfig(configFile string) error { - info, err := p.readConfig(configFile) + info, err := p.ReadConfig(configFile) if err != nil { return err } @@ -55,7 +59,7 @@ func (p *PctConfigProcessor) CheckConfig(configFile string) error { return nil } -func (p *PctConfigProcessor) readConfig(configFile string) (info pct.PuppetContentTemplateInfo, err error) { +func (p *PctConfigProcessor) ReadConfig(configFile string) (info pct.PuppetContentTemplateInfo, err error) { fileBytes, err := p.AFS.ReadFile(configFile) if err != nil { return info, err @@ -75,38 +79,3 @@ func (p *PctConfigProcessor) readConfig(configFile string) (info pct.PuppetConte return info, err } - -func (p *PctConfigProcessor) setupTemplateNamespace(targetDir string, info pct.PuppetContentTemplateInfo, untarPath string, force bool) (string, error) { - // author/id/version - templatePath := filepath.Join(targetDir, info.Template.Author, info.Template.Id) - - err := p.AFS.MkdirAll(templatePath, 0750) - if err != nil { - return "", err - } - - namespacePath := filepath.Join(targetDir, info.Template.Author, info.Template.Id, info.Template.Version) - - // finally move to the full path - err = p.AFS.Rename(untarPath, namespacePath) - if err != nil { - // if a template already exists - if !force { - // error unless forced - return "", fmt.Errorf("Template already installed (%s)", namespacePath) - } else { - // remove the exiting template - err = p.AFS.RemoveAll(namespacePath) - if err != nil { - return "", fmt.Errorf("Unable to overwrite existing template: %v", err) - } - // perform the move again - err = p.AFS.Rename(untarPath, namespacePath) - if err != nil { - return "", fmt.Errorf("Unable to force install: %v", err) - } - } - } - - return namespacePath, err -} diff --git a/main.go b/main.go index 49f44c91..1ac65385 100644 --- a/main.go +++ b/main.go @@ -84,6 +84,7 @@ func main() { ConfigProcessor: &pct_config_processor.PctConfigProcessor{ AFS: &afs, }, + ConfigFileName: "pct-config.yml", }, AFS: &afs, } diff --git a/pkg/config_processor/config_processor.go b/pkg/config_processor/config_processor.go index 88b23d9f..4fc0dc93 100644 --- a/pkg/config_processor/config_processor.go +++ b/pkg/config_processor/config_processor.go @@ -1,6 +1,12 @@ package config_processor type ConfigProcessorI interface { - ProcessConfig(sourceDir, targetDir string, force bool) (string, error) + GetConfigMetadata(configFile string) (metadata ConfigMetadata, err error) CheckConfig(configFile string) error } + +type ConfigMetadata struct { + Id string + Author string + Version string +} diff --git a/pkg/install/install.go b/pkg/install/install.go index 25b56236..f9684d70 100644 --- a/pkg/install/install.go +++ b/pkg/install/install.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "os" + "path" "path/filepath" "strings" @@ -32,6 +33,7 @@ type Installer struct { HTTPClient httpclient.HTTPClientI Exec exec_runner.ExecI ConfigProcessor config_processor.ConfigProcessorI + ConfigFileName string } type InstallerI interface { @@ -76,7 +78,7 @@ func (p *Installer) Install(templatePkg, targetDir string, force bool) (string, } // Process the configuration file and set up namespacedPath and relocate config and content to it - namespacedPath, err := p.ConfigProcessor.ProcessConfig(untarPath, targetDir, force) + namespacedPath, err := p.InstallFromConfig(filepath.Join(untarPath, p.ConfigFileName), targetDir, force) if err != nil { return "", fmt.Errorf("Invalid config: %v", err.Error()) } @@ -125,7 +127,7 @@ func (p *Installer) InstallClone(gitUri, targetDir, tempDir string, force bool) return "", fmt.Errorf("Failed to remove '.git' directory") } - namespacedPath, err := p.ConfigProcessor.ProcessConfig(folderPath, targetDir, force) + namespacedPath, err := p.InstallFromConfig(filepath.Join(folderPath, p.ConfigFileName), targetDir, force) if err != nil { return "", err } @@ -177,3 +179,45 @@ func (p *Installer) downloadTemplate(targetURL *url.URL, downloadDir string) (st return downloadPath, nil } + +func (p *Installer) InstallFromConfig(configFile, targetDir string, force bool) (string, error) { + info, err := p.ConfigProcessor.GetConfigMetadata(configFile) + if err != nil { + return "", err + } + + // Create namespaced directory and move contents of temp folder to it + installedPkgPath := filepath.Join(targetDir, info.Author, info.Id) + + err = p.AFS.MkdirAll(installedPkgPath, 0750) + if err != nil { + return "", err + } + + installedPkgPath = filepath.Join(installedPkgPath, info.Version) + untarredPkgDir := path.Dir(configFile) + + // finally move to the full path + errMsgPrefix := "Unable to install in namespace:" + err = p.AFS.Rename(untarredPkgDir, installedPkgPath) + if err != nil { + // if a template already exists + if !force { + // error unless forced + return "", fmt.Errorf("%s Template already installed (%s)", errMsgPrefix, installedPkgPath) + } else { + // remove the exiting template + err = p.AFS.RemoveAll(installedPkgPath) + if err != nil { + return "", fmt.Errorf("%s Unable to overwrite existing template: %v", errMsgPrefix, err) + } + // perform the move again + err = p.AFS.Rename(untarredPkgDir, installedPkgPath) + if err != nil { + return "", fmt.Errorf("%s Unable to force install: %v", errMsgPrefix, err) + } + } + } + + return installedPkgPath, err +}