Skip to content

Commit

Permalink
Refactor jenfile into project pkg and use straight yaml marshalling
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Frenette <[email protected]>
  • Loading branch information
silphid committed Feb 10, 2021
1 parent b5f125f commit ad09b8f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 0 additions & 7 deletions src/internal/model/jenfile.go

This file was deleted.

70 changes: 0 additions & 70 deletions src/internal/persist/loadJenFile.go

This file was deleted.

9 changes: 4 additions & 5 deletions src/internal/persist/loadSaveConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions src/internal/persist/loadSaveJenFile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 0 additions & 30 deletions src/internal/persist/saveJenFile.go

This file was deleted.

50 changes: 50 additions & 0 deletions src/internal/project/jenfile.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ad09b8f

Please sign in to comment.