Skip to content

Commit

Permalink
chore: let it breathe
Browse files Browse the repository at this point in the history
Add whitespace and some TODOs. Remove an unhelpful comment.
  • Loading branch information
telemachus committed Dec 4, 2024
1 parent 6d74347 commit 2805608
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ func newAppEnv(cfg *cfg) *appEnv {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", cfg.cmd, cfg.subCmd, err)
return &appEnv{exitValue: exitFailure}
}

if cfg.defaultCfgFile {
cfg.cfgFile = filepath.Join(homeDir, cfg.cfgFile)
}

return &appEnv{
cmd: cfg.cmd,
subCmd: cfg.subCmd,
Expand Down
8 changes: 8 additions & 0 deletions internal/cli/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ func (app *appEnv) clone(repos []Repo) {
if app.noOp() {
return
}

err := os.MkdirAll(filepath.Join(app.homeDir, defaultStorage), os.ModePerm)
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", app.cmd, app.subCmd, err)
app.exitValue = exitFailure
return
}

ch := make(chan result)

for _, repo := range repos {
go app.cloneOne(repo, ch)
}

for range repos {
res := <-ch
res.publish(app.quiet)
Expand All @@ -52,12 +56,15 @@ func (app *appEnv) cloneOne(repo Repo, ch chan<- result) {
}
return
}

args := []string{"clone", "--mirror", repo.URL, repo.Name}
cmd := exec.Command("git", args...)
// TODO: consider removing this?
noGitPrompt := "GIT_TERMINAL_PROMPT=0"
env := append(os.Environ(), noGitPrompt)
cmd.Env = env
cmd.Dir = filepath.Join(app.homeDir, defaultStorage)

err := cmd.Run()
if err != nil {
app.exitValue = exitFailure
Expand All @@ -67,6 +74,7 @@ func (app *appEnv) cloneOne(repo Repo, ch chan<- result) {
}
return
}

ch <- result{
isErr: false,
msg: fmt.Sprintf("%s: successfully cloned", repo.Name),
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/getrepos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ func makeNewAppEnv(cfgFile string) *appEnv {
cfgFile: cfgFile,
defaultCfgFile: false,
}

return newAppEnv(cfg)
}

func TestGetReposSuccess(t *testing.T) {
expected := makeRepos()
app := makeNewAppEnv("testdata/backups.json")
actual := app.getRepos()

if app.exitValue != exitSuccess {
t.Fatal("app.exitValue != exitSuccess")
}

if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("app.getRepos(\"testdata/backups.json\") failure (-want +got)\n%s", diff)
}
Expand All @@ -39,6 +42,7 @@ func TestGetReposSuccess(t *testing.T) {
func TestGetReposFailure(t *testing.T) {
app := makeNewAppEnv("testdata/nope.json")
app.getRepos()

if app.exitValue != exitFailure {
t.Errorf("app.getRepos(\"testdata/nope.json\") exit value: %d; expected %d", app.exitValue, exitFailure)
}
Expand All @@ -47,6 +51,7 @@ func TestGetReposFailure(t *testing.T) {
func TestRepoChecks(t *testing.T) {
app := makeNewAppEnv("testdata/repo-checks.json")
actual := app.getRepos()

if len(actual) != 0 {
t.Errorf("app.getRepos(\"testdata/repo-checks.json\") expected len(repos) = 0; actual: %d", len(actual))
}
Expand Down
5 changes: 4 additions & 1 deletion internal/cli/gitmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func Gitmirror(args []string) int {
return exitFailure
}

// Create a new app and switch on the subcommand, which we know is safe.
app := newAppEnv(cfg)
switch app.subCmd {
case "update", "up":
Expand All @@ -45,6 +44,7 @@ func Gitmirror(args []string) int {
fmt.Fprintf(os.Stderr, "%s: unrecognized subcommand %q\n", app.cmd, app.subCmd)
app.exitValue = exitFailure
}

return app.exitValue
}

Expand Down Expand Up @@ -72,6 +72,7 @@ func parse(args []string) (*cfg, error) {
cfg.defaultCfgFile = true
cfg.cfgFile = defaultCfgFile
}

return cfg, nil
}

Expand All @@ -88,6 +89,7 @@ func validate(subCommands []string) error {
if len(subCommands) > 1 {
return errors.New("too many subcommands")
}

validSubCommands := map[string]struct{}{
"clone": {},
"update": {},
Expand All @@ -97,5 +99,6 @@ func validate(subCommands []string) error {
if _, ok := validSubCommands[subCommands[0]]; !ok {
return fmt.Errorf("unrecognized subcommand: %q", subCommands[0])
}

return nil
}
3 changes: 3 additions & 0 deletions internal/cli/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ func (app *appEnv) getRepos() []Repo {
if app.noOp() {
return nil
}

blob, err := os.ReadFile(app.cfgFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", app.cmd, app.subCmd, err)
app.exitValue = exitFailure
return nil
}

repos := make([]Repo, 0, 20)
err = json.Unmarshal(blob, &repos)
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: %s\n", app.cmd, app.subCmd, err)
app.exitValue = exitFailure
return nil
}

// Every repository must have a URL and a directory name.
return slices.DeleteFunc(repos, func(repo Repo) bool {
return repo.URL == "" || repo.Name == ""
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func (r result) publish(quiet bool) {
fmt.Fprintln(os.Stderr, r.msg)
return
}

if quiet {
return
}

fmt.Fprintln(os.Stdout, r.msg)
}
10 changes: 10 additions & 0 deletions internal/cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ func (app *appEnv) update(repos []Repo) {
if app.noOp() {
return
}

ch := make(chan result)

for _, repo := range repos {
go app.updateOne(repo, ch)
}

for range repos {
res := <-ch
res.publish(app.quiet)
}
}

// TODO: use cmd and subCmd for error messages?
func (app *appEnv) updateOne(repo Repo, ch chan<- result) {
repoDir := filepath.Join(app.homeDir, defaultStorage, repo.Name)
fhBefore, err := git.NewFetchHead(filepath.Join(repoDir, "FETCH_HEAD"))
Expand All @@ -39,12 +43,15 @@ func (app *appEnv) updateOne(repo Repo, ch chan<- result) {
}
return
}

args := []string{"remote", "update"}
cmd := exec.Command("git", args...)
// TODO: consider removing this?
noGitPrompt := "GIT_TERMINAL_PROMPT=0"
env := append(os.Environ(), noGitPrompt)
cmd.Env = env
cmd.Dir = repoDir

err = cmd.Run()
if err != nil {
ch <- result{
Expand All @@ -53,6 +60,7 @@ func (app *appEnv) updateOne(repo Repo, ch chan<- result) {
}
return
}

fhAfter, err := git.NewFetchHead(filepath.Join(repoDir, "FETCH_HEAD"))
if err != nil {
ch <- result{
Expand All @@ -61,13 +69,15 @@ func (app *appEnv) updateOne(repo Repo, ch chan<- result) {
}
return
}

if fhBefore.Equals(fhAfter) {
ch <- result{
isErr: false,
msg: fmt.Sprintf("%s: already up-to-date", repo.Name),
}
return
}

ch <- result{
isErr: false,
msg: fmt.Sprintf("%s: updated", repo.Name),
Expand Down
1 change: 1 addition & 0 deletions internal/git/fetchhead.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewFetchHead(f string) (FetchHead, error) {
if err != nil {
return nil, err
}

return FetchHead(fh), nil
}

Expand Down
5 changes: 5 additions & 0 deletions internal/git/fetchhead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func TestFetchHeadEquality(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
fhBefore string
fhAfter string
Expand All @@ -34,17 +35,21 @@ func TestFetchHeadEquality(t *testing.T) {
expected: false,
},
}

for msg, tc := range testCases {
t.Run(msg, func(t *testing.T) {
t.Parallel()

fhBefore, err := git.NewFetchHead(tc.fhBefore)
if err != nil {
t.Fatalf("%s: %s", tc.fhBefore, err)
}

fhAfter, err := git.NewFetchHead(tc.fhAfter)
if err != nil {
t.Fatalf("%s: %s", tc.fhAfter, err)
}

got := fhBefore.Equals(fhAfter)
if got != tc.expected {
t.Errorf("fhBefore.Equals(fhAfter) = %v; want %v", got, tc.expected)
Expand Down

0 comments on commit 2805608

Please sign in to comment.