Skip to content

Commit

Permalink
Extract ProjectDir from config and determine it dynamically when needed
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 10561bb commit 52f29a3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 37 deletions.
26 changes: 0 additions & 26 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package cmd

import (
"fmt"
"os"
"path"
"regexp"

"github.com/Samasource/jen/src/cmd/do"
"github.com/Samasource/jen/src/cmd/exec"
"github.com/Samasource/jen/src/cmd/pull"
"github.com/Samasource/jen/src/internal/constant"
"github.com/Samasource/jen/src/internal/helpers"
"github.com/Samasource/jen/src/internal/logging"
"github.com/Samasource/jen/src/internal/model"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,34 +45,12 @@ continues to support you throughout development in executing project-related com

func initialize(config *model.Config, flags flags) error {
var err error
config.ProjectDir, err = findProjectDirUpFromWorkDir()
if err != nil {
return err
}
config.VarOverrides, err = parseOverrideVars(flags.varOverrides)
config.TemplateName = flags.templateName
config.SkipConfirm = flags.skipConfirm
return err
}

func findProjectDirUpFromWorkDir() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("finding project's root dir: %w", err)
}

for {
filePath := path.Join(dir, constant.JenFileName)
if helpers.PathExists(filePath) {
return dir, nil
}
if dir == "/" {
return "", nil
}
dir = path.Dir(dir)
}
}

var varOverrideRegexp = regexp.MustCompile(`^(\w+)=(.*)$`)

func parseOverrideVars(rawVarOverrides []string) (map[string]string, error) {
Expand Down
1 change: 0 additions & 1 deletion src/internal/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ type Config struct {
TemplatesDir string
TemplateName string
TemplateDir string
ProjectDir string
Spec *Spec
Values Values
BinDirs []string
Expand Down
9 changes: 7 additions & 2 deletions src/internal/persist/loadOrCreateJenFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ import (
"github.com/Samasource/jen/src/internal/constant"
"github.com/Samasource/jen/src/internal/home"
"github.com/Samasource/jen/src/internal/model"
"github.com/Samasource/jen/src/internal/project"
)

// LoadOrCreateJenFile loads the current project's jen file and, if it doesn't
// exists, it prompts users whether to create it.
func LoadOrCreateJenFile(config *model.Config) error {
if config.ProjectDir == "" {
projectDir, err := project.GetProjectDir()
if err != nil {
return err
}
if projectDir == "" {
if !config.SkipConfirm {
err := confirmCreateJenFile()
if err != nil {
Expand All @@ -28,7 +33,7 @@ func LoadOrCreateJenFile(config *model.Config) error {
}
}

err := LoadConfig(config)
err = LoadConfig(config)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions src/internal/persist/loadSaveConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (

"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) error {
jenfile, err := LoadJenFileFromDir(config.ProjectDir)
projectDir, err := project.GetProjectDir()
if err != nil {
return err
}
jenfile, err := LoadJenFileFromDir(projectDir)
if err != nil {
return err
}
Expand All @@ -31,7 +36,11 @@ func SaveConfig(config *model.Config) error {
Variables: config.Values.Variables,
}

err := SaveJenFileToDir(config.ProjectDir, jenfile)
projectDir, err := project.GetProjectDir()
if err != nil {
return err
}
err = SaveJenFileToDir(projectDir, jenfile)
if err != nil {
return err
}
Expand Down
31 changes: 31 additions & 0 deletions src/internal/project/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package project

import (
"fmt"
"os"
"path"

"github.com/Samasource/jen/src/internal/constant"
"github.com/Samasource/jen/src/internal/helpers"
)

// GetProjectDir returns the project's root dir. It finds it by looking for the jen.yaml file
// in current working dir and then walking up the directory structure until it reaches the
// volume's root dir. If it doesn't find it, it returns an empty string.
func GetProjectDir() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("finding project's root dir: %w", err)
}

for {
filePath := path.Join(dir, constant.JenFileName)
if helpers.PathExists(filePath) {
return dir, nil
}
if dir == "/" {
return "", nil
}
dir = path.Dir(dir)
}
}
9 changes: 4 additions & 5 deletions src/internal/steps/exec/exec.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package exec

import (
"path/filepath"

"github.com/Samasource/jen/src/internal/model"
"github.com/Samasource/jen/src/internal/project"
"github.com/Samasource/jen/src/internal/shell"
)

Expand All @@ -16,12 +15,12 @@ func (e Exec) String() string {
return "exec"
}

// Execute executes one or multiple shell commands with project's variables and bin dirs
// Execute runs one or multiple shell commands with project's variables and bin dirs
func (e Exec) Execute(config *model.Config) error {
dir, err := filepath.Abs(config.ProjectDir)
projectDir, err := project.GetProjectDir()
if err != nil {
return err
}

return shell.Execute(config.Values.Variables, dir, config.BinDirs, e.Commands...)
return shell.Execute(config.Values.Variables, projectDir, config.BinDirs, e.Commands...)
}
8 changes: 7 additions & 1 deletion src/internal/steps/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/Samasource/jen/src/internal/evaluation"
"github.com/Samasource/jen/src/internal/model"
"github.com/Samasource/jen/src/internal/project"
)

type Render struct {
Expand All @@ -16,6 +17,11 @@ func (r Render) String() string {
}

func (r Render) Execute(config *model.Config) error {
projectDir, err := project.GetProjectDir()
if err != nil {
return err
}

inputDir := path.Join(config.TemplateDir, r.Source)
return evaluation.Render(config.Values, inputDir, config.ProjectDir)
return evaluation.Render(config.Values, inputDir, projectDir)
}

0 comments on commit 52f29a3

Please sign in to comment.