Skip to content

Commit

Permalink
fixed path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Apr 12, 2018
1 parent 7f152bf commit 4143c1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
10 changes: 5 additions & 5 deletions commands/githook/githook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type CommandData struct {
// Githook command to create git hooks
type Githook struct {
Path string
GitDir string
Cwd string
CommandData *CommandData
}

Expand All @@ -54,10 +54,10 @@ func New(options ...Option) *Githook {
return v
}

// WithGitDir option.
func WithGitDir(dir string) Option {
// WithCwd option.
func WithCwd(dir string) Option {
return func(g *Githook) {
g.GitDir = dir
g.Cwd = dir
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func (g *Githook) getQuestions() []*survey.Question {
Validate: survey.Required,
Prompt: &survey.Input{
Message: "What's the root directory of your git repository?",
Default: g.GitDir,
Default: g.Cwd,
},
},
{
Expand Down
56 changes: 27 additions & 29 deletions commands/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ type (
templateConfig *Survey
dirRenamings map[string]string
dirRemovings []string
gitDir string
cwd string
butlerVersion semver.Version
}
Expand Down Expand Up @@ -129,20 +128,13 @@ func New(options ...Option) *Templating {
"uuid": uuid.NewV4,
"randomInt": func(min, max int) int { return rand.Intn(max-min) + min },
//environment
"cwd": func() (string, error) { return filepath.Abs(t.CommandData.Path) },
"cwd": func() string { return t.CommandData.Path },
"env": func(name string) string { return os.Getenv(name) },
}

return t
}

// WithGitDir option.
func WithGitDir(dir string) Option {
return func(t *Templating) {
t.gitDir = dir
}
}

// WithCwd option.
func WithCwd(dir string) Option {
return func(t *Templating) {
Expand Down Expand Up @@ -194,6 +186,8 @@ func WithTemplateSurveyResults(sr map[string]interface{}) Option {

// unpackGitRepository clone a repo to the dst
func (t *Templating) unpackGitRepository(templatePath string, dest string) error {
logy.Debugf("unpack template from %s to %s", templatePath, dest)

_, err := git.PlainClone(dest, false, &git.CloneOptions{
URL: templatePath,
})
Expand All @@ -213,6 +207,8 @@ func (t *Templating) unpackGitRepository(templatePath string, dest string) error

// unpackLocalGitRepository copy a local repository to the dst
func (t *Templating) unpackLocalGitRepository(templatePath string, dest string) error {
logy.Debugf("unpack template from %s to %s", templatePath, dest)

err := utils.MoveDir(templatePath, dest)
if err != nil {
return errors.Wrap(err, "local repository could not be copied")
Expand All @@ -237,26 +233,20 @@ func (t *Templating) packTemplate(tempDir, dest string) error {
}
}

destAbs, err := filepath.Abs(dest)
logy.Debugf("pack template from %s to %s", tempDir, dest)

err := utils.CreateDirIfNotExist(dest)
if err != nil {
return errors.Wrap(err, "dest abs failed")
return errors.Wrap(err, "create dest dir failed")
}

// move files from temp to cd
if destAbs == t.gitDir {
err := utils.MoveDir(tempDir, t.gitDir)
if err != nil {
return errors.Wrap(err, "move failed")
}
err = os.RemoveAll(tempDir)
if err != nil {
return errors.Wrap(err, "remove all failed")
}
} else {
err := os.Rename(tempDir, dest)
if err != nil {
return errors.Wrap(err, "rename failed")
}
err = utils.MoveDir(tempDir, dest)
if err != nil {
return errors.Wrap(err, "move failed")
}
err = os.RemoveAll(tempDir)
if err != nil {
return errors.Wrap(err, "remove all failed")
}

return nil
Expand Down Expand Up @@ -386,7 +376,6 @@ func (t *Templating) StartCommandSurvey() error {
return errors.Wrap(err, "template command survey")
}
t.CommandData = cd
t.CommandData.Path = path.Clean(t.CommandData.Path)
return nil
}

Expand All @@ -396,6 +385,11 @@ func (t *Templating) startProjectSurvey() error {
if err != nil {
return errors.Wrap(err, "command survey")
}
dest, err := filepath.Abs(t.CommandData.Path)
if err != nil {
return errors.Wrap(err, "dest path failed")
}
t.CommandData.Path = dest
return nil
}

Expand Down Expand Up @@ -676,11 +670,15 @@ func (t *Templating) templater(path, filename string, ctx *logy.Entry) error {

// Run the command
func (t *Templating) Run() (err error) {
tempDir, err := ioutil.TempDir(t.gitDir, "butler")
tempDir, err := ioutil.TempDir(t.CommandData.Path, "butler")
if err != nil {
err = errors.Wrap(err, "create temp folder failed")
return err
}
tempDir, err = filepath.Abs(tempDir)
if err != nil {
return errors.Wrap(err, "temp abs failed")
}

// remove template artifacts when a panic or error occur
// when the user abort the process at the last step we will
Expand Down Expand Up @@ -925,7 +923,7 @@ func (t *Templating) Run() (err error) {
* Git hook task
*/
commandGitHook := githook.New(
githook.WithGitDir(t.gitDir),
githook.WithCwd(t.cwd),
githook.WithCommandData(
&githook.CommandData{
Path: t.CommandData.Path,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func interactiveCliMode() {
}
}
case "Create Git Hooks":
command := githook.New(githook.WithGitDir(cd))
command := githook.New(githook.WithCwd(cd))

err := command.StartCommandSurvey()
if err != nil {
Expand Down

0 comments on commit 4143c1b

Please sign in to comment.