diff --git a/src/cmd/root.go b/src/cmd/root.go index 5680635..15956b8 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -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" @@ -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) { diff --git a/src/internal/model/config.go b/src/internal/model/config.go index 4e5006c..0026cea 100644 --- a/src/internal/model/config.go +++ b/src/internal/model/config.go @@ -5,7 +5,6 @@ type Config struct { TemplatesDir string TemplateName string TemplateDir string - ProjectDir string Spec *Spec Values Values BinDirs []string diff --git a/src/internal/persist/loadOrCreateJenFile.go b/src/internal/persist/loadOrCreateJenFile.go index 48ad30b..365d413 100644 --- a/src/internal/persist/loadOrCreateJenFile.go +++ b/src/internal/persist/loadOrCreateJenFile.go @@ -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 { @@ -28,7 +33,7 @@ func LoadOrCreateJenFile(config *model.Config) error { } } - err := LoadConfig(config) + err = LoadConfig(config) if err != nil { return err } diff --git a/src/internal/persist/loadSaveConfig.go b/src/internal/persist/loadSaveConfig.go index 4d5b0f7..d313732 100644 --- a/src/internal/persist/loadSaveConfig.go +++ b/src/internal/persist/loadSaveConfig.go @@ -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 } @@ -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 } diff --git a/src/internal/project/project.go b/src/internal/project/project.go new file mode 100644 index 0000000..266869c --- /dev/null +++ b/src/internal/project/project.go @@ -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) + } +} diff --git a/src/internal/steps/exec/exec.go b/src/internal/steps/exec/exec.go index 734bfbc..7b405f3 100644 --- a/src/internal/steps/exec/exec.go +++ b/src/internal/steps/exec/exec.go @@ -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" ) @@ -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...) } diff --git a/src/internal/steps/render/render.go b/src/internal/steps/render/render.go index b3dd96b..1855f62 100644 --- a/src/internal/steps/render/render.go +++ b/src/internal/steps/render/render.go @@ -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 { @@ -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) }