Skip to content

Commit

Permalink
Create --debug option for all commands and refactor (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmahsax authored Sep 10, 2023
1 parent dbbc793 commit 95bc946
Show file tree
Hide file tree
Showing 27 changed files with 752 additions and 364 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Release

on:
workflow_dispatch:
inputs:
tag_name:
description: The tag name to make a GitHub Release for
required: true
type: string

jobs:
go-release:
if: github.ref == 'refs/heads/main'
uses: emmahsax/github-actions/.github/workflows/go-release.yml@main
with:
build_command: task build
files: |
git-helper_darwin_arm64
tag_name: ${{ inputs.tag_name }}
upload: true
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
name: Test & Release
name: Test

on:
pull_request:
push:
branches: [ main ]
workflow_dispatch:
inputs:
release:
default: false
description: Whether to create a GitHub Release (ignored if not on 'main' branch)
required: true
type: boolean
tag_name:
description: The tag name to make a GitHub Release for
required: false
type: string

jobs:
setup:
Expand All @@ -30,19 +20,9 @@ jobs:
gitleaks:
uses: emmahsax/github-actions/.github/workflows/gitleaks.yml@main

go-release:
if: github.ref == 'refs/heads/main' && inputs.release
uses: emmahsax/github-actions/.github/workflows/go-release.yml@main
with:
build_command: task build
files: |
git-helper_darwin_arm64
tag_name: ${{ inputs.tag_name }}
upload: true

go-test:
if: github.event_name == 'pull_request' || !(github.event_name == 'push' && github.ref == 'refs/heads/main')
needs: [setup]
needs: [ setup ]
uses: emmahsax/github-actions/.github/workflows/go-test.yml@main
with:
branch: ${{ needs.setup.outputs.branch }}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Alternatively, you can download pre-built binaries straight from [GitHub Release

```bash
sudo mv ~/Downloads/git-helper_darwin_arm64 /usr/local/bin/git-helper
sudo chown root:wheel /usr/local/bin/git-helper
sudo chown $(whoami):staff /usr/local/bin/git-helper
sudo chmod +x /usr/local/bin/git-helper
```

Expand Down Expand Up @@ -278,4 +278,4 @@ To make a new release:
1. Merge the pull request via the big green button
2. Run `git tag vX.X.X` and `git push --tag`
3. Trigger a new workflow from [GitHub Actions](https://github.com/emmahsax/go-git-helper/actions/workflows/test-and-release.yml) and check to create a new release and pass in the tag name that you just pushed
3. Trigger a new workflow from [GitHub Actions](https://github.com/emmahsax/go-git-helper/actions/workflows/release.yml) and pass in the tag name just created
6 changes: 3 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tasks:
- cmd: GOOS=darwin GOARCH=arm64 go build -o git-helper_darwin_arm64

build:test:
desc: Build go-git-helper for testing purposes
desc: Build test-git-helper for testing purposes
cmds:
- cmd: GOOS=darwin GOARCH=arm64 go build -o go-git-helper
- cmd: sudo mv go-git-helper /usr/local/bin/go-git-helper
- cmd: GOOS=darwin GOARCH=arm64 go build -o test-git-helper
- cmd: sudo mv test-git-helper /usr/local/bin/test-git-helper
64 changes: 44 additions & 20 deletions cmd/changeRemote/changeRemote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,35 @@ import (
)

type ChangeRemote struct {
Debug bool
OldOwner string
NewOwner string
}

func NewCommand() *cobra.Command {
var (
debug bool
)

cmd := &cobra.Command{
Use: "change-remote [oldOwner] [newOwner]",
Short: "Change the git remote owners for multiple cloned git repositories",
Args: cobra.ExactArgs(2),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
newChangeRemote(args[0], args[1]).execute()
newChangeRemoteClient(args[0], args[1], debug).execute()
return nil
},
}

cmd.Flags().BoolVar(&debug, "debug", false, "enables debug mode")

return cmd
}

func newChangeRemote(oldOwner, newOwner string) *ChangeRemote {
func newChangeRemoteClient(oldOwner, newOwner string, debug bool) *ChangeRemote {
return &ChangeRemote{
Debug: debug,
OldOwner: oldOwner,
NewOwner: newOwner,
}
Expand All @@ -46,36 +54,48 @@ func (cr *ChangeRemote) execute() {

for _, entry := range nestedDirs {
if entry.IsDir() && entry.Name() != "." && entry.Name() != ".." {
processDir(entry.Name(), originalDir, cr)
cr.processDir(entry.Name(), originalDir)
}
}
}

func processDir(currentDir, originalDir string, cr *ChangeRemote) {
func (cr *ChangeRemote) processDir(currentDir, originalDir string) {
_ = os.Chdir(currentDir)
defer os.Chdir(originalDir)

current, _ := os.Getwd()
gitDir := filepath.Join(current, ".git")

if _, err := os.Stat(gitDir); err == nil {
answer := commandline.AskYesNoQuestion(
"Found git directory: " + currentDir + ". Do you wish to proceed in updating " + currentDir + "'s remote URLs?",
)
fullRemoteInfo := cr.processGitRepository()

if len(fullRemoteInfo) > 0 {
fmt.Println("Found git directory: " + currentDir + ".")
}

for repo, inner := range fullRemoteInfo {
answer := commandline.AskYesNoQuestion(
"Do you wish to proceed in updating the " + inner["plainRemote"] + " remote URL?",
)

if answer {
processGitRepository(cr)
if answer {
cr.processRemote(inner["plainRemote"], inner["host"], repo, inner["remoteName"])
}
}
}
}

func processGitRepository(cr *ChangeRemote) {
func (cr *ChangeRemote) processGitRepository() map[string]map[string]string {
fullRemoteInfo := make(map[string]map[string]string)

cmd := exec.Command("git", "remote", "-v")
output, err := cmd.Output()
if err != nil {
debug.PrintStack()
if cr.Debug {
debug.PrintStack()
}
log.Fatal(err)
return
return fullRemoteInfo
}

remotes := strings.Split(string(output), "\n")
Expand All @@ -87,18 +107,20 @@ func processGitRepository(cr *ChangeRemote) {

plainRemote := strings.Fields(remote)[1]
remoteName := strings.Fields(remote)[0]
host, owner, repo := remoteInfo(plainRemote)
host, owner, repo := cr.remoteInfo(plainRemote)

if owner == cr.OldOwner {
processRemote(plainRemote, host, repo, remoteName, cr)
} else {
fmt.Printf(" Found remote (%s) is not pointing to %s.\n", plainRemote, cr.OldOwner)
inner := make(map[string]string)
inner["plainRemote"] = plainRemote
inner["remoteName"] = remoteName
inner["host"] = host
fullRemoteInfo[repo] = inner
}
}
fmt.Println()
return fullRemoteInfo
}

func processRemote(remote, host, repo, remoteName string, cr *ChangeRemote) {
func (cr *ChangeRemote) processRemote(remote, host, repo, remoteName string) {
var newRemote string

if strings.Contains(remote, "git@") {
Expand All @@ -111,13 +133,15 @@ func processRemote(remote, host, repo, remoteName string, cr *ChangeRemote) {
cmd := exec.Command("git", "remote", "set-url", remoteName, newRemote)
_, err := cmd.Output()
if err != nil {
debug.PrintStack()
if cr.Debug {
debug.PrintStack()
}
log.Fatal(err)
return
}
}

func remoteInfo(remote string) (string, string, string) {
func (cr *ChangeRemote) remoteInfo(remote string) (string, string, string) {
if strings.Contains(remote, "git@") {
remoteSplit := strings.SplitN(remote, ":", 2)
if len(remoteSplit) != 2 {
Expand Down
10 changes: 5 additions & 5 deletions cmd/changeRemote/changeRemote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ func TestExecute(t *testing.T) {
err := os.Chdir(tmpDir)
assert.NoError(t, err)

cr := newChangeRemote("oldOwner", "newOwner")
cr := newChangeRemoteClient("oldOwner", "newOwner", true)
cr.execute()
}

func TestRemoteInfo(t *testing.T) {
remote := "[email protected]:oldOwner/repo.git"
cr := newChangeRemote("oldOwner", "newOwner")
cr := newChangeRemoteClient("oldOwner", "newOwner", false)
cr.execute()
host, owner, repo := remoteInfo(remote)
host, owner, repo := cr.remoteInfo(remote)

if host != "[email protected]" {
t.Fatalf(`Host should match %s, was %s`, "[email protected]", host)
Expand All @@ -35,9 +35,9 @@ func TestRemoteInfo(t *testing.T) {
}

remote = "https://gitlab.com/oldOwner/repo"
cr = newChangeRemote("oldOwner", "newOwner")
cr = newChangeRemoteClient("oldOwner", "newOwner", false)
cr.execute()
host, owner, repo = remoteInfo(remote)
host, owner, repo = cr.remoteInfo(remote)

if host != "https://gitlab.com" {
t.Fatalf(`Host should match %s, was %s`, "https://gitlab.com", host)
Expand Down
23 changes: 17 additions & 6 deletions cmd/checkoutDefault/checkoutDefault.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,39 @@ import (
"github.com/spf13/cobra"
)

type CheckoutDefault struct{}
type CheckoutDefault struct {
Debug bool
}

func NewCommand() *cobra.Command {
var (
debug bool
)

cmd := &cobra.Command{
Use: "checkout-default",
Short: "Switches to the default branch",
Args: cobra.ExactArgs(0),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
checkoutDefault().execute()
newCheckoutDefaultClient(debug).execute()
return nil
},
}

cmd.Flags().BoolVar(&debug, "debug", false, "enables debug mode")

return cmd
}

func checkoutDefault() *CheckoutDefault {
return &CheckoutDefault{}
func newCheckoutDefaultClient(debug bool) *CheckoutDefault {
return &CheckoutDefault{
Debug: debug,
}
}

func (cd *CheckoutDefault) execute() {
branch := git.DefaultBranch()
git.Checkout(branch)
g := git.NewGitClient(cd.Debug)
branch := g.DefaultBranch()
g.Checkout(branch)
}
29 changes: 20 additions & 9 deletions cmd/cleanBranches/cleanBranches.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,42 @@ import (
"github.com/spf13/cobra"
)

type CleanBranches struct{}
type CleanBranches struct {
Debug bool
}

func NewCommand() *cobra.Command {
var (
debug bool
)

cmd := &cobra.Command{
Use: "clean-branches",
Short: "Switches to the default branch, git pulls, git fetches, and removes remote-deleted branches from your machine",
Args: cobra.ExactArgs(0),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
cleanBranches().execute()
newCleanBranchesClient(debug).execute()
return nil
},
}

cmd.Flags().BoolVar(&debug, "debug", false, "enables debug mode")

return cmd
}

func cleanBranches() *CleanBranches {
return &CleanBranches{}
func newCleanBranchesClient(debug bool) *CleanBranches {
return &CleanBranches{
Debug: debug,
}
}

func (cb *CleanBranches) execute() {
branch := git.DefaultBranch()
git.Checkout(branch)
git.Pull()
git.Fetch()
git.CleanDeletedBranches()
g := git.NewGitClient(cb.Debug)
branch := g.DefaultBranch()
g.Checkout(branch)
g.Pull()
g.Fetch()
g.CleanDeletedBranches()
}
Loading

0 comments on commit 95bc946

Please sign in to comment.