Skip to content

Commit

Permalink
chore: improve names and simplify goroutine
Browse files Browse the repository at this point in the history
+ MirrorRepos(wanted) -> Mirror(wanted) - parameter reads better with
  a simpler function name
+ updateRepo(repo, channel) -> update(repo, channel) — no stutter
+ go func() { update(repo, channel) } -> go update(repo, channel)
  - only one step inside, so just call the function directly
+ error message does not need the awkward phrase "problem with"
  • Loading branch information
telemachus committed Oct 15, 2024
1 parent aa52c6c commit a8f1e68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func (app *App) Unmarshal(configFile string, isDefault bool) *Wanted {
return &wanted
}

// MirrorRepos runs git push --mirror on a group of repositories and displays
// the result of the mirror operation.
func (app *App) MirrorRepos(wanted *Wanted) {
// Mirror runs git push --mirror on a group of repositories and displays the
// result of the mirror operation.
func (app *App) Mirror(wanted *Wanted) {
if app.NoOp() || len(wanted.Repos) == 0 {
return
}
Expand All @@ -108,24 +108,22 @@ func (app *App) MirrorRepos(wanted *Wanted) {
})
ch := make(chan Publisher)
for _, repo := range wanted.Repos {
go func() {
updateRepo(repo, ch)
}()
go mirror(repo, ch)
}
for range wanted.Repos {
result := <-ch
result.Publish()
}
}

func updateRepo(repo *Repo, ch chan Publisher) {
func mirror(repo *Repo, ch chan Publisher) {
args := []string{"push", "--mirror", repo.Remote}
cmd := exec.Command("git", args...)
cmd.Dir = repo.Dir
cmdString := fmt.Sprintf("`git %s` (in %s)", strings.Join(args, " "), cmd.Dir)
err := cmd.Run()
if err != nil {
ch <- Failure{msg: fmt.Sprintf("%s: problem with %s: %s", appName, cmdString, err)}
ch <- Failure{msg: fmt.Sprintf("%s: %s: %s", appName, cmdString, err)}
}
ch <- Success{msg: fmt.Sprintf("%s: %s", appName, cmdString)}
}
2 changes: 1 addition & 1 deletion cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func Run(args []string) int {
app := &App{ExitValue: exitSuccess}
configFile, isDefault := app.ParseFlags(args)
wanted := app.Unmarshal(configFile, isDefault)
app.MirrorRepos(wanted)
app.Mirror(wanted)
return app.ExitValue
}

0 comments on commit a8f1e68

Please sign in to comment.