From b2486476e8fd6747606692db6809b9c41df27abb Mon Sep 17 00:00:00 2001 From: Luke Hopkins Date: Wed, 31 Jul 2019 16:37:19 +0100 Subject: [PATCH] adding an exclude regex and changing flag names to include and exclude --- cmd/github.go | 18 ++++++++++-------- cmd/github_test.go | 13 ++++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cmd/github.go b/cmd/github.go index bfdafcc..2c4a235 100644 --- a/cmd/github.go +++ b/cmd/github.go @@ -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()) @@ -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") } @@ -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) @@ -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 { diff --git a/cmd/github_test.go b/cmd/github_test.go index 08ebf9b..026e861 100644 --- a/cmd/github_test.go +++ b/cmd/github_test.go @@ -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) @@ -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) }