From ad09b8f07d64d905a080511a67e9d19adc02a8c2 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Wed, 10 Feb 2021 10:38:31 -0500 Subject: [PATCH] Refactor jenfile into project pkg and use straight yaml marshalling Signed-off-by: Mathieu Frenette --- src/internal/home/home.go | 2 +- src/internal/model/jenfile.go | 7 -- src/internal/persist/loadJenFile.go | 70 -------------------- src/internal/persist/loadSaveConfig.go | 9 ++- src/internal/persist/loadSaveJenFile_test.go | 7 +- src/internal/persist/saveJenFile.go | 30 --------- src/internal/project/jenfile.go | 50 ++++++++++++++ src/internal/project/project.go | 2 +- src/main.go | 7 +- 9 files changed, 66 insertions(+), 118 deletions(-) delete mode 100644 src/internal/model/jenfile.go delete mode 100644 src/internal/persist/loadJenFile.go delete mode 100644 src/internal/persist/saveJenFile.go create mode 100644 src/internal/project/jenfile.go diff --git a/src/internal/home/home.go b/src/internal/home/home.go index 8db96b0..342692f 100644 --- a/src/internal/home/home.go +++ b/src/internal/home/home.go @@ -27,7 +27,7 @@ func GetOrCloneJenRepo() (string, error) { if helpers.PathExists(jenHome) { if helpers.PathExists(filepath.Join(jenHome, ".git")) { // Jen dir is a valid git repo - logging.Log("Using jen templates clones at %q", jenHome) + logging.Log("Using jen templates clone at %q", jenHome) return jenHome, nil } diff --git a/src/internal/model/jenfile.go b/src/internal/model/jenfile.go deleted file mode 100644 index 85fd7c9..0000000 --- a/src/internal/model/jenfile.go +++ /dev/null @@ -1,7 +0,0 @@ -package model - -type JenFile struct { - Version string - TemplateName string - Variables VarMap -} diff --git a/src/internal/persist/loadJenFile.go b/src/internal/persist/loadJenFile.go deleted file mode 100644 index e81ac8b..0000000 --- a/src/internal/persist/loadJenFile.go +++ /dev/null @@ -1,70 +0,0 @@ -package persist - -import ( - "fmt" - "path" - - "github.com/Samasource/jen/src/internal/constant" - "github.com/Samasource/jen/src/internal/model" - "github.com/kylelemons/go-gypsy/yaml" -) - -// LoadJenFileFromDir loads the jen file from given project directory -func LoadJenFileFromDir(projectDir string) (*model.JenFile, error) { - specFilePath := path.Join(projectDir, constant.JenFileName) - yamlFile, err := yaml.ReadFile(specFilePath) - if err != nil { - return nil, err - } - - _map, ok := yamlFile.Root.(yaml.Map) - if !ok { - return nil, fmt.Errorf("jen file root is expected to be an object") - } - return loadJenFileFromMap(_map) -} - -func loadJenFileFromMap(_map yaml.Map) (*model.JenFile, error) { - jenfile := new(model.JenFile) - - // Load metadata - metadata, err := getRequiredMap(_map, "metadata") - if err != nil { - return nil, err - } - jenfile.TemplateName, err = getRequiredStringFromMap(metadata, "template") - if err != nil { - return nil, err - } - jenfile.Version, err = getRequiredStringFromMap(metadata, "version") - if err != nil { - return nil, err - } - if jenfile.Version != constant.JenFileVersion { - return nil, fmt.Errorf("unsupported jenfile version %s (expected %s)", jenfile.Version, constant.JenFileVersion) - } - - // Load variables - variables, err := getRequiredMap(_map, "variables") - if err != nil { - return nil, err - } - jenfile.Variables, err = loadVariables(variables) - if err != nil { - return nil, err - } - - return jenfile, nil -} - -func loadVariables(_map yaml.Map) (model.VarMap, error) { - variables := make(model.VarMap, len(_map)) - for name, value := range _map { - str, ok := getString(value) - if !ok { - return nil, fmt.Errorf("value of variable %q must be a raw string", name) - } - variables[name] = str - } - return variables, nil -} diff --git a/src/internal/persist/loadSaveConfig.go b/src/internal/persist/loadSaveConfig.go index fa2d96a..adef1b9 100644 --- a/src/internal/persist/loadSaveConfig.go +++ b/src/internal/persist/loadSaveConfig.go @@ -3,13 +3,13 @@ package persist import ( "strings" - "github.com/Samasource/jen/src/internal/constant" "github.com/Samasource/jen/src/internal/model" + "github.com/Samasource/jen/src/internal/project" ) // LoadConfig loads config object from jen file func LoadConfig(config *model.Config, projectDir string) error { - jenfile, err := LoadJenFileFromDir(projectDir) + jenfile, err := project.Load(projectDir) if err != nil { return err } @@ -25,13 +25,12 @@ func LoadConfig(config *model.Config, projectDir string) error { // SaveConfig saves config object to jen file func SaveConfig(config *model.Config, projectDir string) error { - jenfile := model.JenFile{ - Version: constant.JenFileVersion, + jenfile := project.JenFile{ TemplateName: config.TemplateName, Variables: config.Values.Variables, } - err := SaveJenFileToDir(projectDir, jenfile) + err := jenfile.Save(projectDir) if err != nil { return err } diff --git a/src/internal/persist/loadSaveJenFile_test.go b/src/internal/persist/loadSaveJenFile_test.go index c214da0..202ac72 100644 --- a/src/internal/persist/loadSaveJenFile_test.go +++ b/src/internal/persist/loadSaveJenFile_test.go @@ -5,22 +5,23 @@ import ( "testing" "github.com/Samasource/jen/src/internal/model" + "github.com/Samasource/jen/src/internal/project" "github.com/go-test/deep" "github.com/stretchr/testify/assert" ) func TestSaveAndLoad(t *testing.T) { // Save - jenFile := model.JenFile{Variables: model.VarMap{ + jenFile := project.JenFile{Variables: model.VarMap{ "VAR1": "true", "VAR2": "abc", }} dir := getTempDir() - err := SaveJenFileToDir(dir, jenFile) + err := jenFile.Save(dir) assert.NoError(t, err) // Load - actualJenFile, err := LoadJenFileFromDir(dir) + actualJenFile, err := project.Load(dir) assert.NoError(t, err) // Compare diff --git a/src/internal/persist/saveJenFile.go b/src/internal/persist/saveJenFile.go deleted file mode 100644 index 694a890..0000000 --- a/src/internal/persist/saveJenFile.go +++ /dev/null @@ -1,30 +0,0 @@ -package persist - -import ( - "io/ioutil" - "os" - "path" - - "github.com/Samasource/jen/src/internal/constant" - "github.com/Samasource/jen/src/internal/model" - "gopkg.in/yaml.v2" -) - -// SaveJenFileToDir saves jen file into given project directory -func SaveJenFileToDir(projectDir string, jenfile model.JenFile) error { - _map := map[interface{}]interface{}{ - "metadata": map[interface{}]interface{}{ - "version": constant.JenFileVersion, - "template": jenfile.TemplateName, - }, - "variables": jenfile.Variables, - } - - doc, err := yaml.Marshal(&_map) - if err != nil { - return err - } - - filePath := path.Join(projectDir, constant.JenFileName) - return ioutil.WriteFile(filePath, doc, os.ModePerm) -} diff --git a/src/internal/project/jenfile.go b/src/internal/project/jenfile.go new file mode 100644 index 0000000..07b26b7 --- /dev/null +++ b/src/internal/project/jenfile.go @@ -0,0 +1,50 @@ +package project + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + + "github.com/Samasource/jen/src/internal/constant" + "gopkg.in/yaml.v2" +) + +// JenFile represents the configuration file in a project's root dir +type JenFile struct { + Version string + TemplateName string + Variables map[string]string +} + +// Save saves jen file into given project directory +func (jenFile JenFile) Save(dir string) error { + jenFile.Version = constant.JenFileVersion + doc, err := yaml.Marshal(jenFile) + if err != nil { + return err + } + + filePath := path.Join(dir, constant.JenFileName) + return ioutil.WriteFile(filePath, doc, os.ModePerm) +} + +// Load loads the jen file from given project directory +func Load(dir string) (*JenFile, error) { + specFilePath := filepath.Join(dir, constant.JenFileName) + buf, err := ioutil.ReadFile(specFilePath) + if err != nil { + return nil, fmt.Errorf("loading jen file: %w", err) + } + var jenFile JenFile + err = yaml.Unmarshal(buf, &jenFile) + if err != nil { + return nil, fmt.Errorf("unmarshalling jen file yaml: %w", err) + } + + if jenFile.Version != constant.JenFileVersion { + return nil, fmt.Errorf("unsupported jenfile version %s (expected %s)", jenFile.Version, constant.JenFileVersion) + } + return &jenFile, nil +} diff --git a/src/internal/project/project.go b/src/internal/project/project.go index fa45e53..36cfaf7 100644 --- a/src/internal/project/project.go +++ b/src/internal/project/project.go @@ -21,7 +21,7 @@ func GetProjectDir() (string, error) { for { path := filepath.Join(dir, constant.JenFileName) if helpers.PathExists(path) { - return path, nil + return dir, nil } if dir == "/" { return "", nil diff --git a/src/main.go b/src/main.go index cbe09e5..fa5d5dc 100644 --- a/src/main.go +++ b/src/main.go @@ -6,12 +6,17 @@ import ( "github.com/Samasource/jen/src/cmd" "github.com/Samasource/jen/src/internal/model" "github.com/Samasource/jen/src/internal/persist" + "github.com/Samasource/jen/src/internal/project" ) func main() { config := &model.Config{} config.OnValuesChanged = func() error { - return persist.SaveConfig(config) + projectDir, err := project.GetProjectDir() + if err != nil { + return err + } + return persist.SaveConfig(config, projectDir) } rootCmd := cmd.NewRoot(config)