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 1 commit
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
Next Next commit
Sub folder template; initial commit
  • Loading branch information
jasimmk committed Sep 22, 2019
commit 2f39582916632a876890bb0c88786be462a9f28b
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
48 changes: 46 additions & 2 deletions pkg/cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ var Download = &cli.Command{

MustValidateTemplateDir()

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

targetDir, err := boilr.TemplatePath(templateName)
if err != nil {
exit.Error(fmt.Errorf("download: %s", err))
}

targetTmpDir := targetDir

switch exists, err := osutil.DirExists(targetDir); {
case err != nil:
exit.Error(fmt.Errorf("download: %s", err))
Expand All @@ -47,14 +49,56 @@ 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{
if err := git.Clone(targetTmpDir, git.CloneOptions{
URL: host.URL(templateURL),
}); err != nil {
exit.Error(fmt.Errorf("download: %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))
}
}
// 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
1 change: 1 addition & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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("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
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