Skip to content

Commit a18ec7c

Browse files
authored
Merge pull request #7 from bestseller-ecom/regexp
Adds new clone structure
2 parents c5c6c7c + fabcac7 commit a18ec7c

18 files changed

+302
-134
lines changed

.gitignore

+54-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
testing*/
1+
testing*/
2+
3+
# Created by https://www.toptal.com/developers/gitignore/api/macos,linux
4+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,linux
5+
6+
### Linux ###
7+
*~
8+
9+
# temporary files which can be created if a process still has a handle open of a deleted file
10+
.fuse_hidden*
11+
12+
# KDE directory preferences
13+
.directory
14+
15+
# Linux trash folder which might appear on any partition or disk
16+
.Trash-*
17+
18+
# .nfs files are created when an open file is removed but is still being accessed
19+
.nfs*
20+
21+
### macOS ###
22+
# General
23+
.DS_Store
24+
.AppleDouble
25+
.LSOverride
26+
27+
# Icon must end with two \r
28+
Icon
29+
30+
31+
# Thumbnails
32+
._*
33+
34+
# Files that might appear in the root of a volume
35+
.DocumentRevisions-V100
36+
.fseventsd
37+
.Spotlight-V100
38+
.TemporaryItems
39+
.Trashes
40+
.VolumeIcon.icns
41+
.com.apple.timemachine.donotpresent
42+
43+
# Directories potentially created on remote AFP share
44+
.AppleDB
45+
.AppleDesktop
46+
Network Trash Folder
47+
Temporary Items
48+
.apdisk
49+
50+
### macOS Patch ###
51+
# iCloud generated files
52+
*.icloud
53+
54+
# End of https://www.toptal.com/developers/gitignore/api/macos,linux

cmd/add.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ var addCmd = &cobra.Command{
2727
Short: "Add all content in each repository",
2828
Long: `Add all content to each repository, equivalent to make git add -A.`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
myRepos := githelper.MyRepos{}
32-
repoNames := myRepos.GithubGetRepoNames(json_file)
33-
githelper.GitAdd(repoNames)
30+
target, _ := cmd.Flags().GetString("target")
31+
repoNames := githelper.ListDirectories(target)
32+
githelper.GitAdd(target, repoNames)
3433
},
3534
}
3635

cmd/checkout.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ var checkoutCmd = &cobra.Command{
2828
Long: `Checkout to a specific branch, you need to pass branch name`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
branch, _ := cmd.Flags().GetString("branch")
31-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
32-
myRepos := githelper.MyRepos{}
33-
repoNames := myRepos.GithubGetRepoNames(json_file)
34-
githelper.GitCheckout(repoNames, branch)
31+
target, _ := cmd.Flags().GetString("target")
32+
repoNames := githelper.ListDirectories(target)
33+
githelper.GitCheckout(target, repoNames, branch)
3534
},
3635
}
3736

cmd/clone.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
package cmd
1818

1919
import (
20-
"errors"
21-
"os"
22-
2320
"github.com/fatih/color"
2421
"github.com/felipem1210/git-helper/githelper"
2522
"github.com/spf13/cobra"
@@ -36,27 +33,28 @@ var cloneCmd = &cobra.Command{
3633
provider, _ := cmd.Flags().GetString("provider")
3734
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
3835
org, _ := cmd.Flags().GetString("org")
36+
regex, _ := cmd.Flags().GetString("regexp")
37+
target, _ := cmd.Flags().GetString("target")
38+
auth, _ := cmd.Flags().GetString("auth")
3939
if provider == "github" && json_file != "" {
4040
myRepos := githelper.MyRepos{}
41-
repoNames = myRepos.GithubGetRepoNames(json_file)
42-
repoUrls = myRepos.GithubGetCloneUrls(json_file)
43-
if _, err := os.Stat(json_file); errors.Is(err, os.ErrNotExist) {
44-
myRepos = myRepos.GetGithubRepositoriesInfo(org)
45-
err := githelper.WriteReposToJson(myRepos, json_file)
46-
if err != nil {
47-
githelper.CheckIfError(err)
48-
} else {
49-
color.Green("The json file %s with repo info was written sucessfully", json_file)
50-
}
51-
}
41+
myRepos = myRepos.GetGithubRepositoriesInfo(org)
42+
err := githelper.WriteReposToJson(myRepos, json_file)
43+
githelper.CheckIfError(err)
44+
color.Green("The json file %s with repo info was written sucessfully", json_file)
45+
repoNames = myRepos.GithubGetRepoNames(json_file, regex)
46+
repoUrls = myRepos.GithubGetGitUrls(json_file, regex, auth)
5247
}
53-
githelper.GitClone(repoNames, repoUrls)
48+
githelper.GitClone(target, auth, repoNames, repoUrls)
5449
},
5550
}
5651

5752
func init() {
5853
rootCmd.AddCommand(cloneCmd)
59-
cloneCmd.PersistentFlags().StringP("provider", "p", "", "A provider to choose, options: gitub, gitlab")
60-
cloneCmd.MarkPersistentFlagRequired("provider")
54+
cloneCmd.PersistentFlags().StringP("provider", "p", "github", "A provider to choose, options: gitub, gitlab")
55+
//cloneCmd.MarkPersistentFlagRequired("provider")
6156
cloneCmd.PersistentFlags().StringP("org", "o", "", "The github Organization to work with")
57+
cloneCmd.PersistentFlags().StringP("regexp", "r", "", "Regexp to apply to repositories clone process, to clone based on it")
58+
cloneCmd.PersistentFlags().StringP("auth", "a", "ssh", "Select if you want to clone using HTTPS or SSH.")
59+
cloneCmd.PersistentFlags().String("repo-info-json-file", "repos_info.json", "The name of the json file with info of the repos of the Github Org. It is read for each git local actions.")
6260
}

cmd/commit.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ var commitCmd = &cobra.Command{
2828
Long: `Create a commit to each repository.`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
message, _ := cmd.Flags().GetString("message")
31-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
32-
myRepos := githelper.MyRepos{}
33-
repoNames := myRepos.GithubGetRepoNames(json_file)
34-
githelper.GitCommit(repoNames, message)
31+
target, _ := cmd.Flags().GetString("target")
32+
repoNames := githelper.ListDirectories(target)
33+
githelper.GitCommit(target, repoNames, message)
3534
},
3635
}
3736

cmd/createBranch.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ var createBranchCmd = &cobra.Command{
2828
Long: `Create a branch locally and checkout into that branch for each repository.`,
2929
Run: func(cmd *cobra.Command, args []string) {
3030
branch, _ := cmd.Flags().GetString("branch")
31-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
32-
myRepos := githelper.MyRepos{}
33-
repoNames := myRepos.GithubGetRepoNames(json_file)
34-
githelper.GitCreateBranch(repoNames, branch)
31+
target, _ := cmd.Flags().GetString("target")
32+
repoNames := githelper.ListDirectories(target)
33+
githelper.GitCreateBranch(target, repoNames, branch)
3534
},
3635
}
3736

cmd/fetch.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ var fetchCmd = &cobra.Command{
2727
Short: "Do a git fetch of all branches on each repo",
2828
Long: `Do a git fetch of all branches on each repo. Equivalent to do: git fetch --all.`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
myRepos := githelper.MyRepos{}
32-
repoNames := myRepos.GithubGetRepoNames(json_file)
33-
githelper.GitFetch(repoNames)
30+
target, _ := cmd.Flags().GetString("target")
31+
repoNames := githelper.ListDirectories(target)
32+
githelper.GitFetch(target, repoNames)
3433
},
3534
}
3635

cmd/pr.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,18 @@ var prCmd = &cobra.Command{
3535
create, _ := cmd.Flags().GetBool("create")
3636
update, _ := cmd.Flags().GetBool("update")
3737
merge, _ := cmd.Flags().GetBool("merge")
38+
target, _ := cmd.Flags().GetString("target")
3839

3940
repos := githelper.MyRepos{}
40-
repoNames := repos.GithubGetRepoNames(repos_json_file)
41+
repoNames := githelper.ListDirectories(target)
4142
org := repos.GithubGetOrg(repos_json_file)
4243

4344
myPrs := githelper.MyPrs{}
4445
if create {
4546
myPrs = myPrs.GithubCreatePr(org, repoNames, new_pr_json_file, reviewers)
4647
err := githelper.WritePrsToJson(myPrs, pr_info_json_file)
47-
if err != nil {
48-
githelper.CheckIfError(err)
49-
} else {
50-
color.Green("The json file %s with pr info was written sucessfully", pr_info_json_file)
51-
}
48+
githelper.CheckIfError(err)
49+
color.Green("The json file %s with pr info was written sucessfully", pr_info_json_file)
5250
} else if update {
5351
myPrs.GithubEditPr(org, repoNames, pr_info_json_file)
5452
} else if merge {

cmd/pull.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ var pullCmd = &cobra.Command{
2727
Short: "Pull changes of a branch from remote",
2828
Long: `Pull changes of a branch from remote. It will make the pull of current branch`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
myRepos := githelper.MyRepos{}
32-
repoNames := myRepos.GithubGetRepoNames(json_file)
33-
githelper.GitPull(repoNames)
30+
target, _ := cmd.Flags().GetString("target")
31+
repoNames := githelper.ListDirectories(target)
32+
githelper.GitPull(target, repoNames)
3433
},
3534
}
3635

cmd/push.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ var pushCmd = &cobra.Command{
2727
Short: "Push changes of a branch to remote",
2828
Long: `Push changes of a branch from remote. It will make the Push of current branch.`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
myRepos := githelper.MyRepos{}
32-
repoNames := myRepos.GithubGetRepoNames(json_file)
33-
githelper.GitPush(repoNames)
30+
target, _ := cmd.Flags().GetString("target")
31+
repoNames := githelper.ListDirectories(target)
32+
githelper.GitPush(target, repoNames)
3433
},
3534
}
3635

cmd/rebase.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ If you have conflicts you can pass the argument with "continue" or "abort" optio
3232
Run: func(cmd *cobra.Command, args []string) {
3333
fmt.Println("rebase called")
3434
base_branch, _ := cmd.Flags().GetString("base-branch")
35-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
36-
myRepos := githelper.MyRepos{}
37-
repoNames := myRepos.GithubGetRepoNames(json_file)
38-
githelper.GitRebase(repoNames, base_branch)
35+
target, _ := cmd.Flags().GetString("target")
36+
repoNames := githelper.ListDirectories(target)
37+
githelper.GitRebase(target, repoNames, base_branch)
3938
},
4039
}
4140

cmd/repository.go

+1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ func init() {
4242
repoCmd.PersistentFlags().StringP("provider", "p", "", "A provider to choose, options: gitub, gitlab")
4343
repoCmd.MarkPersistentFlagRequired("provider")
4444
repoCmd.PersistentFlags().BoolP("create", "c", false, "Create the repositories from repo-info-json-file")
45+
repoCmd.PersistentFlags().String("repo-info-json-file", "repos_info.json", "The name of the json file with info of the repos of the Github Org. It is read for each git local actions.")
4546
}

cmd/reset.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ var resetCmd = &cobra.Command{
2727
Short: "Hard Reset to HEAD in all repositories",
2828
Long: `Hard Reset to HEAD in all repositories.`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
myRepos := githelper.MyRepos{}
32-
repoNames := myRepos.GithubGetRepoNames(json_file)
33-
githelper.GitReset(repoNames)
30+
target, _ := cmd.Flags().GetString("target")
31+
repoNames := githelper.ListDirectories(target)
32+
githelper.GitReset(target, repoNames)
3433
},
3534
}
3635

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ func init() {
5757
// Cobra also supports local flags, which will only run
5858
// when this action is called directly.
5959
cobra.OnInitialize(githelper.ValidateEnv)
60-
rootCmd.PersistentFlags().String("repo-info-json-file", "repos_info.json", "The name of the json file with info of the repos of the Github Org or Gitlab Group. It is read for each git local actions.")
60+
rootCmd.PersistentFlags().StringP("target", "t", "", "Target directory inside your working directory where git actions will be executed. If not set actions will be done on WORKING_DIR")
6161
}

cmd/status.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ var statusCmd = &cobra.Command{
2727
Short: "Print the working tree status of each repository",
2828
Long: `Print the working tree status of each repository. Equivalent to do git status`,
2929
Run: func(cmd *cobra.Command, args []string) {
30-
json_file, _ := cmd.Flags().GetString("repo-info-json-file")
31-
myRepos := githelper.MyRepos{}
32-
repoNames := myRepos.GithubGetRepoNames(json_file)
33-
githelper.GitStatus(repoNames)
30+
target, _ := cmd.Flags().GetString("target")
31+
repoNames := githelper.ListDirectories(target)
32+
githelper.GitStatus(target, repoNames)
3433
},
3534
}
3635

0 commit comments

Comments
 (0)