Skip to content

Commit

Permalink
adding an exclude regex and changing flag names to include and exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
lhopki01 committed Jul 31, 2019
1 parent 9bdea4d commit b248647
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
18 changes: 10 additions & 8 deletions cmd/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ var githubCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(githubCmd)

githubCmd.Flags().String("regex", ".*", "Regex to match repo names against")
githubCmd.Flags().String("include", ".*", "Regex to match repo names against")
githubCmd.Flags().String("exclude", "^$", "Regex to exclude repo names against")
githubCmd.Flags().String("archive-dir", "", "Repo to put archived repos in\n(default is .archive in the download dir)")

err := viper.BindPFlags(githubCmd.Flags())
Expand All @@ -75,7 +76,7 @@ func init() {
viper.AutomaticEnv()
}

func processFlags(args []string) (string, string, string, *regexp.Regexp) {
func processFlags(args []string) (string, string, string, *regexp.Regexp, *regexp.Regexp) {
if len(args) != 2 {
log.Fatal("Wrong number of arguments")
}
Expand All @@ -93,21 +94,22 @@ func processFlags(args []string) (string, string, string, *regexp.Regexp) {
}
fmt.Printf("Archiving repos into %s\n", archiveDir)

r := regexp.MustCompile(viper.GetString("regex"))
inR := regexp.MustCompile(viper.GetString("include"))
exR := regexp.MustCompile(viper.GetString("exclude"))

fmt.Println("=============")

return dir, archiveDir, org, r
return dir, archiveDir, org, inR, exR
}

func runGithub(args []string) {
dir, archiveDir, org, r := processFlags(args)
dir, archiveDir, org, inR, exR := processFlags(args)

client := &http.Client{}
repoList := getRepoList(org, client)
dirList := actions.GetGitDirList(dir)

reposToSync, reposToClone, reposToArchive := repoActions(repoList, dirList, archiveDir, r)
reposToSync, reposToClone, reposToArchive := repoActions(repoList, dirList, archiveDir, inR, exR)

lenSync := len(reposToSync)
lenClone := len(reposToClone)
Expand Down Expand Up @@ -203,13 +205,13 @@ func repoAction(repo repo, dirList []string) (action, []string) {
return actionNone, dirList
}

func repoActions(repoList []repo, dirList []string, archiveDir string, r *regexp.Regexp) ([]string, []string, []string) {
func repoActions(repoList []repo, dirList []string, archiveDir string, inR *regexp.Regexp, exR *regexp.Regexp) ([]string, []string, []string) {
var reposToSync []string
var reposToClone []string
var reposToArchive []string

for _, repo := range repoList {
if r.MatchString(repo.Name) {
if inR.MatchString(repo.Name) && !exR.MatchString(repo.Name) {
var a action
a, dirList = repoAction(repo, dirList)
switch a {
Expand Down
13 changes: 8 additions & 5 deletions cmd/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ func TestRepoActions(t *testing.T) {
repos = append(repos, tc.repo)
}

r, _ := regexp.Compile(".*")
reposToSync, reposToClone, reposToArchive := repoActions(repos, []string{"archivedRepo", "syncRepo", "deletedRepo"}, "foobar", r)
inR, _ := regexp.Compile(".*")
exR, _ := regexp.Compile("^$")
reposToSync, reposToClone, reposToArchive := repoActions(repos, []string{"archivedRepo", "syncRepo", "deletedRepo"}, "foobar", inR, exR)
assert.Equal(t, []string{"syncRepo"}, reposToSync)
assert.Equal(t, []string{"git@giturl/cloneRepo", "git@giturl/cloneArchiveRepo"}, reposToClone)
assert.Equal(t, []string{"archivedRepo", "cloneArchiveRepo", "deletedRepo"}, reposToArchive)
Expand Down Expand Up @@ -165,10 +166,12 @@ func TestGetRepoList(t *testing.T) {
}

func TestProcessFlags(t *testing.T) {
dir, archiveDir, org, r := processFlags([]string{"foobar", "/tmp/foobar"})
dir, archiveDir, org, inR, exR := processFlags([]string{"foobar", "/tmp/foobar"})
assert.Equal(t, "/tmp/foobar", dir)
assert.Equal(t, "/tmp/foobar/.archive", archiveDir)
assert.Equal(t, "foobar", org)
expectedR, _ := regexp.Compile(".*")
assert.Equal(t, expectedR, r)
expectedInR, _ := regexp.Compile(".*")
assert.Equal(t, expectedInR, inR)
expectedExR, _ := regexp.Compile("^$")
assert.Equal(t, expectedExR, exR)
}

0 comments on commit b248647

Please sign in to comment.