Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multiple templates in a repo -- Also clone template from specific branch #77

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
go test ./...
build:
go build
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ boilr template download <github-repo-path> <template-tag>
boilr template download tmrts/boilr-license license
```

An example for multi template, multi branch forced repo clone

```
boilr template download -p template2/templates/sample -b multi-branch -f https://github.com/jasimmk/boilr-test.git sample
```

The downloaded template will be saved to local `boilr` registry.

## Save a Local Template
Expand Down
9 changes: 9 additions & 0 deletions pkg/boilr/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
// TemplateDir is the directory that contains the template registry
TemplateDir = "templates"

// TemplateTempDir is the directory that is used for temporary storage
TemplateTempDir = "../tmp"

// ContextFileName is the name of the file that contains the context values for the template
ContextFileName = "project.json"

Expand Down Expand Up @@ -57,6 +60,12 @@ func TemplatePath(name string) (string, error) {
return filepath.Join(Configuration.TemplateDirPath, name), nil
}

// TemplateTempPath returns absolute path of template temporary directory given name of template.
func TemplateTempPath(name string) (string, error) {
return filepath.Join(Configuration.TemplateDirPath, TemplateTempDir, name), nil
}

// IsTemplateDirInitialized Checks if template directory is initialized
func IsTemplateDirInitialized() (bool, error) {
return osutil.DirExists(Configuration.TemplateDirPath)
}
Expand Down
68 changes: 64 additions & 4 deletions pkg/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"gopkg.in/src-d/go-git.v4/plumbing"

cli "github.com/spf13/cobra"

"github.com/tmrts/boilr/pkg/boilr"
Expand All @@ -27,9 +29,12 @@ var Download = &cli.Command{

MustValidateTemplateDir()

templateSubFolder := GetStringFlag(c, "sub-path")
templateRemoteBranch := GetStringFlag(c, "branch")
templateURL, templateName := args[0], args[1]

targetDir, err := boilr.TemplatePath(templateName)
targetTmpDir := targetDir

if err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
Expand All @@ -47,14 +52,69 @@ var Download = &cli.Command{
exit.Error(fmt.Errorf("download: %s", err))
}
}
// In case if we are copying template from repository sub-folder, clone repo to temp folder
if templateSubFolder != "" {
targetTmpDir, err = boilr.TemplateTempPath(templateName)
if err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
exists, err := osutil.DirExists(targetTmpDir)
if exists || (!exists && err != nil) {
if err := os.RemoveAll(targetTmpDir); err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
}
}

// TODO(tmrts): allow fetching other branches than 'master'
if err := git.Clone(targetDir, git.CloneOptions{
gitCloneOptions := git.CloneOptions{
URL: host.URL(templateURL),
}); err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
if templateRemoteBranch != "" {
gitCloneOptions.ReferenceName = plumbing.NewBranchReferenceName(templateRemoteBranch)
gitCloneOptions.SingleBranch = true
}
if err := git.Clone(targetTmpDir, gitCloneOptions); err != nil {
exit.Error(fmt.Errorf("download: Cloning repo - %s", err))
}

// Copy content from sub-folder to target folder
if templateSubFolder != "" {
// Ensure sub-folder exists
templateTmpDir := osutil.JoinPaths(targetTmpDir, templateSubFolder)
exists, err := osutil.DirExists(templateTmpDir)
if err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
if !exists {
exit.Error(fmt.Errorf("download: sub-folder doesn't exist"))
}
// Check target folder exists, and copy contents
if exists, err = osutil.DirExists(targetDir); err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}
if !exists {
if err = osutil.CreateDirs(targetDir); err != nil {
exit.Error(fmt.Errorf("download: %s", err))

}
}
if err = osutil.CopyRecursively(templateTmpDir, targetDir); err != nil {
exit.Error(fmt.Errorf("download: Error copying files from temp %s", err))
}
// Delete all temp files
if err := os.RemoveAll(targetTmpDir); err != nil {
exit.Error(fmt.Errorf("download: Error deleting temp files %s", err))
}
}
// Ensure that a 'template' folder exists inside the repo before registering template
exists, err := osutil.DirExists(osutil.JoinPaths(targetDir, boilr.TemplateDirName))
if err != nil {
exit.Error(fmt.Errorf("download: Template error - %s", err))
}
if !exists {
exit.Error(fmt.Errorf("download: Invalid template. Folder '%s' - doesn't exist at %s", boilr.TemplateDirName, targetDir))
}
// TODO(tmrts): use git-notes as metadata storage or boltdb
if err := serializeMetadata(templateName, templateURL, targetDir); err != nil {
exit.Error(fmt.Errorf("download: %s", err))
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func Run() {

Download.PersistentFlags().BoolP("force", "f", false, "Overwrite existing template with the same name")
Download.PersistentFlags().StringP("log-level", "l", "error", "log-level for output")
Download.PersistentFlags().StringP("branch", "b", "", "Branch, Commit Id, Tag or other reference to checkout")
Download.PersistentFlags().StringP("sub-path", "p", "", "Path inside the git repo where 'template' folder.")
Template.AddCommand(Download)

List.PersistentFlags().BoolP("dont-prettify", "", false, "Print only the template names without fancy formatting")
Expand Down
4 changes: 2 additions & 2 deletions pkg/prompt/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestNewBooleanPromptFunc(t *testing.T) {

expectedPromptMsg := "Please choose a value for \"fieldName\""
if msg != expectedPromptMsg {
t.Errorf("boolPrompt(%q).PromptMessage(%q) expected %q got %q", defval, name, expectedPromptMsg, msg)
t.Errorf("boolPrompt((%v).PromptMessage(%q) expected %q got %q", defval, name, expectedPromptMsg, msg)
}

choiceCases := []struct {
Expand All @@ -73,7 +73,7 @@ func TestNewBooleanPromptFunc(t *testing.T) {
for _, c := range choiceCases {
val, err := boolPrompt.EvaluateChoice(c.choice)
if err != nil {
t.Errorf("boolPrompt(%q).EvaluateChoice(%q) got error %q", defval, c.choice, err)
t.Errorf("boolPrompt(%v).EvaluateChoice(%q) got error %q", defval, c.choice, err)
continue
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/util/osutil/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"path/filepath"
)

// JoinPaths joins multiple paths together
func JoinPaths(paths ...string) string {
return filepath.Join(paths...)
}

// FileExists checks whether the given path exists and belongs to a file.
func FileExists(path string) (bool, error) {
info, err := os.Stat(path)
Expand Down