From 3a228a4b06512caebfb78e65cdd8eef55cbacbac Mon Sep 17 00:00:00 2001 From: Jay Gabriels Date: Sat, 18 Jun 2022 10:26:26 -0700 Subject: [PATCH] Fix prune command to work with backups (#209) --- CHANGELOG.md | 9 ++++++++ README.md | 3 ++- cmd/clone.go | 52 +++++++++++++++++++++++------------------------ cmd/clone_test.go | 24 +++++++++++----------- cmd/root.go | 3 ++- cmd/version.go | 2 +- 6 files changed, 52 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de4781b..c28d01b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +## [1.7.17] - unreleased +### Added +### Changed +### Deprecated +### Removed +### Fixed +- Backup flag not working with prune; thanks @i3v +### Security + ## [1.7.16] - 6/1/22 ### Added - GHORG_PRUNE setting which allows a user to have Ghorg automatically remove items from their local diff --git a/README.md b/README.md index e086ce5..41f7b2b 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/gabrie30/ghorg)](https://goreportcard.com/report/github.com/gabrie30/ghorg) GoDoc [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![WakeMeOps](https://docs.wakemeops.com/badges/ghorg.svg)](https://docs.wakemeops.com//packages/ghorg) +Pronounced [gore-guh]; similar to [gorge (verb)](https://www.dictionary.com/browse/gorge). You can use ghorg to gorge on repos...Time to gorge! -ghorg allows you to quickly clone all of an orgs, or users repos into a single directory. This can be useful in many situations including +Use ghorg to quickly clone all of an orgs, or users repos into a single directory. This can be useful in many situations including 1. Searching an orgs/users codebase with ack, silver searcher, grep etc.. 1. Bash scripting diff --git a/cmd/clone.go b/cmd/clone.go index 9a83d7f..61a3a1a 100644 --- a/cmd/clone.go +++ b/cmd/clone.go @@ -194,7 +194,8 @@ func cloneFunc(cmd *cobra.Command, argz []string) { os.Exit(1) } - parseParentFolder(argz) + setOutputDirName(argz) + setOuputDirAbsolutePath() args = argz targetCloneSource = argz[0] setupRepoClone() @@ -256,7 +257,7 @@ func getCloneUrls(isOrg bool) ([]scm.Repo, error) { } func createDirIfNotExist() { - if _, err := os.Stat(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO") + parentFolder); os.IsNotExist(err) { + if _, err := os.Stat(outputDirAbsolutePath); os.IsNotExist(err) { err = os.MkdirAll(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), 0o700) if err != nil { panic(err) @@ -397,16 +398,16 @@ func printDryRun(repos []scm.Repo) { colorlog.PrintSubtleInfo(repo.URL + "\n") } count := len(repos) - colorlog.PrintSuccess(fmt.Sprintf("%v repos to be cloned into: %s%s", count, os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder)) + colorlog.PrintSuccess(fmt.Sprintf("%v repos to be cloned into: %s", count, outputDirAbsolutePath)) if os.Getenv("GHORG_PRUNE") == "true" { - cloneLocation := filepath.Join(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder) - if stat, err := os.Stat(cloneLocation); err == nil && stat.IsDir() { + + if stat, err := os.Stat(outputDirAbsolutePath); err == nil && stat.IsDir() { // We check that the clone path exists, otherwise there would definitely be no pruning // to do. colorlog.PrintInfo("\nScanning for local clones that have been removed on remote...") - files, err := ioutil.ReadDir(cloneLocation) + files, err := ioutil.ReadDir(outputDirAbsolutePath) if err != nil { log.Fatal(err) } @@ -510,7 +511,7 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { repoSlug = repo.Path } - repo.HostPath = filepath.Join(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder, repoSlug) + repo.HostPath = filepath.Join(outputDirAbsolutePath, repoSlug) if repo.IsWiki { if !strings.HasSuffix(repo.HostPath, ".wiki") { @@ -518,10 +519,6 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { } } - if os.Getenv("GHORG_BACKUP") == "true" { - repo.HostPath = filepath.Join(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder+"_backup", repoSlug) - } - action := "cloning" if repoExistsLocally(repo) { @@ -667,9 +664,8 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { // Now, clean up local repos that don't exist in remote, if prune flag is set if os.Getenv("GHORG_PRUNE") == "true" { colorlog.PrintInfo("\nScanning for local clones that have been removed on remote...") - cloneLocation := filepath.Join(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder) - files, err := ioutil.ReadDir(cloneLocation) + files, err := ioutil.ReadDir(outputDirAbsolutePath) if err != nil { log.Fatal(err) } @@ -688,8 +684,8 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { fmt.Sprintf("%s was not found in remote. Do you want to prune it?", f.Name())) if userAgreesToDelete { colorlog.PrintSubtleInfo( - fmt.Sprintf("Deleting %s", filepath.Join(cloneLocation, f.Name()))) - err = os.RemoveAll(filepath.Join(cloneLocation, f.Name())) + fmt.Sprintf("Deleting %s", filepath.Join(outputDirAbsolutePath, f.Name()))) + err = os.RemoveAll(filepath.Join(outputDirAbsolutePath, f.Name())) if err != nil { log.Fatal(err) } @@ -700,11 +696,8 @@ func CloneAllRepos(git git.Gitter, cloneTargets []scm.Repo) { } } - // TODO: fix all these if else checks with ghorg_backups - if os.Getenv("GHORG_BACKUP") == "true" { - colorlog.PrintSuccess(fmt.Sprintf("\nFinished! %s%s_backup", os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder)) - } else if os.Getenv("GHORG_QUIET") != "true" { - colorlog.PrintSuccess(fmt.Sprintf("\nFinished! %s%s", os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), parentFolder)) + if os.Getenv("GHORG_QUIET") != "true" { + colorlog.PrintSuccess(fmt.Sprintf("\nFinished! %s", outputDirAbsolutePath)) } } @@ -792,7 +785,7 @@ func PrintConfigs() { colorlog.PrintInfo("* Exclude Prefix: " + os.Getenv("GHORG_EXCLUDE_MATCH_PREFIX")) } if os.Getenv("GHORG_OUTPUT_DIR") != "" { - colorlog.PrintInfo("* Output Dir : " + parentFolder) + colorlog.PrintInfo("* Output Dir : " + outputDirName) } if os.Getenv("GHORG_NO_CLEAN") == "true" { colorlog.PrintInfo("* No Clean : " + "true") @@ -825,13 +818,17 @@ func getGhorgBranch() string { return os.Getenv("GHORG_BRANCH") } -func parseParentFolder(argz []string) { +func setOuputDirAbsolutePath() { + outputDirAbsolutePath = filepath.Join(os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO"), outputDirName) +} + +func setOutputDirName(argz []string) { if os.Getenv("GHORG_OUTPUT_DIR") != "" { - parentFolder = os.Getenv("GHORG_OUTPUT_DIR") + outputDirName = os.Getenv("GHORG_OUTPUT_DIR") return } - parentFolder = strings.ToLower(argz[0]) + outputDirName = strings.ToLower(argz[0]) // If all-group is used set the parent folder to the name of the baseurl if argz[0] == "all-groups" && os.Getenv("GHORG_SCM_BASE_URL") != "" { @@ -839,7 +836,10 @@ func parseParentFolder(argz []string) { if err != nil { return } - parentFolder = strings.TrimSuffix(strings.TrimPrefix(u.Host, "www."), ".com") - fmt.Println(parentFolder) + outputDirName = strings.TrimSuffix(strings.TrimPrefix(u.Host, "www."), ".com") + } + + if os.Getenv("GHORG_BACKUP") == "true" { + outputDirName = outputDirName + "_backup" } } diff --git a/cmd/clone_test.go b/cmd/clone_test.go index 222fe7e..6cbbb25 100644 --- a/cmd/clone_test.go +++ b/cmd/clone_test.go @@ -13,20 +13,20 @@ import ( func TestShouldLowerRegularString(t *testing.T) { upperName := "RepoName" - parseParentFolder([]string{upperName}) + setOutputDirName([]string{upperName}) - if parentFolder != "reponame" { - t.Errorf("Wrong folder name, expected: %s, got: %s", upperName, parentFolder) + if outputDirName != "reponame" { + t.Errorf("Wrong folder name, expected: %s, got: %s", upperName, outputDirName) } } func TestShouldNotChangeLowerCasedRegularString(t *testing.T) { lowerName := "repo_name" - parseParentFolder([]string{lowerName}) + setOutputDirName([]string{lowerName}) - if parentFolder != "repo_name" { - t.Errorf("Wrong folder name, expected: %s, got: %s", lowerName, parentFolder) + if outputDirName != "repo_name" { + t.Errorf("Wrong folder name, expected: %s, got: %s", lowerName, outputDirName) } } @@ -34,20 +34,20 @@ func TestReplaceDashWithUnderscore(t *testing.T) { want := "repo-name" lowerName := "repo-name" - parseParentFolder([]string{lowerName}) + setOutputDirName([]string{lowerName}) - if parentFolder != want { - t.Errorf("Wrong folder name, expected: %s, got: %s", want, parentFolder) + if outputDirName != want { + t.Errorf("Wrong folder name, expected: %s, got: %s", want, outputDirName) } } func TestShouldNotChangeNonLettersString(t *testing.T) { numberName := "1234567_8" - parseParentFolder([]string{numberName}) + setOutputDirName([]string{numberName}) - if parentFolder != "1234567_8" { - t.Errorf("Wrong folder name, expected: %s, got: %s", numberName, parentFolder) + if outputDirName != "1234567_8" { + t.Errorf("Wrong folder name, expected: %s, got: %s", numberName, outputDirName) } } diff --git a/cmd/root.go b/cmd/root.go index c2f4995..f67b7ad 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,7 +15,8 @@ import ( var ( protocol string path string - parentFolder string + outputDirName string + outputDirAbsolutePath string branch string token string cloneType string diff --git a/cmd/version.go b/cmd/version.go index 14ccdef..b323b8a 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -const ghorgVersion = "v1.7.16" +const ghorgVersion = "v1.7.17" var versionCmd = &cobra.Command{ Use: "version",