From 2b41e020bfb0fddfc04826f5c717ff78bc4736c9 Mon Sep 17 00:00:00 2001 From: Drew Wyatt Date: Thu, 25 Apr 2019 23:08:48 -0400 Subject: [PATCH] Moved commands to their own files --- commands/default.go | 60 +++++++++++++++++++++++++++++++++++++++++++++ commands/version.go | 11 +++++++++ 2 files changed, 71 insertions(+) create mode 100644 commands/default.go create mode 100644 commands/version.go diff --git a/commands/default.go b/commands/default.go new file mode 100644 index 0000000..45dadef --- /dev/null +++ b/commands/default.go @@ -0,0 +1,60 @@ +package commands + +import ( + "fmt" + "gopkg.in/AlecAivazis/survey.v1" + "regexp" + + gUtils "github.com/drewwyatt/gitclean/git" +) + +var goneBranch = regexp.MustCompile(`(?m)^(?:\*| ) ([^\s]+)\s+[a-z0-9]+ \[[^:\n]+: gone\].*$`) + +func Default(directory string, interactive bool, force bool) { + if directory == "" { + directory = "." + } + + git := gUtils.NewExecutor(directory) + + goneBranches := []string{} + branchesToDelete := []string{} + + git.Fetch().Prune().ListRemoteBranches() + submatches := goneBranch.FindAllStringSubmatch(git.Output, -1) + for _, matches := range submatches { + if len(matches) == 2 && matches[1] != "" { + if interactive { + goneBranches = append(goneBranches, matches[1]) + } else { + branchesToDelete = append(branchesToDelete, matches[1]) + } + } + } + + prompt := &survey.MultiSelect{ + Message: "Branches to delete:", + Options: goneBranches, + } + survey.AskOne(prompt, &branchesToDelete, nil) + + for _, branch := range branchesToDelete { + git.Delete(branch, force) + } + + if len(git.DeletedBranches) > 0 { + fmt.Println("Deleted branches:") + for _, branch := range git.DeletedBranches { + fmt.Println(branch) + } + } + + if len(git.BranchDeletionErrors) > 0 { + fmt.Println("Errors:") + for _, err := range git.BranchDeletionErrors { + fmt.Printf("[%s]: %s", err.Branch, err.Msg) + } + } + + fmt.Println("Done.") +} diff --git a/commands/version.go b/commands/version.go new file mode 100644 index 0000000..143411e --- /dev/null +++ b/commands/version.go @@ -0,0 +1,11 @@ +package commands + +import ( + "fmt" +) + +// Version print the current version +func Version(version string) { + fmt.Println(version) + return +}