Skip to content

Commit

Permalink
refactor pull
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Feb 9, 2021
1 parent ade932c commit 277bb4f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/cmd/pull/pull.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package pull

import (
"github.com/Samasource/jen/src/internal/model"
"github.com/Samasource/jen/src/internal/home"
"github.com/Samasource/jen/src/internal/shell"
"github.com/spf13/cobra"
)

// New creates the "jen pull" cobra sub-command
func New(config *model.Config) *cobra.Command {
func New() *cobra.Command {
return &cobra.Command{
Use: "pull",
Short: "Pulls latest templates from git repo",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
return run(config)
jenHome, err := home.CloneJenRepo()
if err != nil {
return err
}

return shell.Execute(nil, jenHome, nil, "git pull")
},
}
}

func run(config *model.Config) error {
return shell.Execute(nil, config.JenDir, config.BinDirs, "git pull")
}
5 changes: 2 additions & 3 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ continues to support you throughout development in executing project-related com
c.PersistentFlags().StringVarP(&config.TemplateName, "template", "t", "", "Name of template to use (defaults to prompting user)")
c.PersistentFlags().BoolVarP(&config.SkipConfirm, "yes", "y", false, "skip all confirmation prompts")
c.PersistentFlags().StringSliceVarP(&config.RawVarOverrides, "set", "s", []string{}, "sets a project variable manually (can be used multiple times)")
c.AddCommand(pull.New(config))
c.AddCommand(pull.New())
c.AddCommand(do.New(config))
c.AddCommand(exec.New(config))
c.PersistentPreRunE = func(*cobra.Command, []string) error {
Expand All @@ -55,8 +55,7 @@ func initialize(config *model.Config) error {
return err
}

err = cloneJenRepo(config.JenDir, jenRepo)
if err != nil {
if err := cloneJenRepo(config.JenDir, jenRepo); err != nil {
return err
}

Expand Down
75 changes: 75 additions & 0 deletions src/internal/home/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package home

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/Samasource/jen/src/internal/helpers"
"github.com/Samasource/jen/src/internal/logging"
"github.com/Samasource/jen/src/internal/shell"
"github.com/mitchellh/go-homedir"
)

// getJenRepo reads the repoitory value from the environment and returns an error if it is not set
func getJenRepo() (string, error) {
jenRepo, ok := os.LookupEnv("JEN_REPO")
if !ok {
return "", fmt.Errorf("please specify a JEN_REPO env var pointing to your jen templates git repo")
}
return jenRepo, nil
}

// getJenHomeDir returns the path to the jen home folder defaulting to ~/.jen if not provided
func getJenHomeDir() (jenHomeDir string, err error) {
defer func() {
if err == nil {
logging.Log("Using jen home dir: %s", jenHomeDir)
}
}()

jenHomeDir, ok := os.LookupEnv("JEN_HOME")
if ok && jenHomeDir != "" {
return
}

home, err := homedir.Dir()
if err != nil {
err = fmt.Errorf("failed to detect home directory: %v", err)
return
}
jenHomeDir = filepath.Join(home, ".jen")
return
}

// CloneJenRepo will clone the jenRepo if it does not exist, and return the path to where it was cloned
func CloneJenRepo() (string, error) {
jenHome, err := getJenHomeDir()
if err != nil {
return "", fmt.Errorf("failed to locate jen home: %w", err)
}

if helpers.PathExists(jenHome) {
if helpers.PathExists(filepath.Join(jenHome, ".git")) {
return jenHome, nil
}

infos, err := ioutil.ReadDir(jenHome)
if err != nil {
return jenHome, fmt.Errorf("listing content of jen dir %q to ensure it's empty before cloning into it: %w", jenHome, err)
}

if len(infos) > 0 {
return jenHome, fmt.Errorf("jen dir %q already exists, is not a valid git working copy and already contains files so we cannot clone into it (please delete or empty it)", jenHome)
}
}

jenRepo, err := getJenRepo()
if err != nil {
return jenHome, fmt.Errorf("failed to detect jen repo: %w", err)
}

logging.Log("Cloning jen templates repo %q into jen dir %q", jenRepo, jenHome)
return jenHome, shell.Execute(nil, "", nil, fmt.Sprintf("git clone %s %s", jenRepo, jenHome))
}

0 comments on commit 277bb4f

Please sign in to comment.