Skip to content

Commit

Permalink
chore: DRY up display of repo paths
Browse files Browse the repository at this point in the history
I want to turn /home/user/path/to/repo into ~/path/to/repo.  This
creates a method for different commands to print a pretty path.
  • Loading branch information
telemachus committed Oct 26, 2024
1 parent 27b03b3 commit 454b1c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"slices"
"strings"
)

const (
Expand Down Expand Up @@ -117,3 +118,8 @@ func (app *App) Unmarshal(configFile string, configIsDefault bool) []Repo {
return repo.URL == "" || repo.Name == ""
})
}

// PrettyPath replaces a user's home directory with ~ in a string.
func (app *App) PrettyPath(s string) string {
return strings.Replace(s, app.HomeDir, "~", 1)
}
10 changes: 8 additions & 2 deletions cli/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ func (app *App) initialize(repo Repo, ch chan<- Publisher) {
// to send a Success on the channel and return.
repoPath := filepath.Join(app.HomeDir, defaultStorage, repo.Name)
if _, err := os.Stat(repoPath); err == nil {
ch <- Success{msg: fmt.Sprintf("%s: %s already exists", app.CmdName, repoPath)}
ch <- Success{
msg: fmt.Sprintf(
"%s: %s already exists",
app.CmdName,
app.PrettyPath(repoPath),
),
}
return
}
args := []string{"clone", "--mirror", repo.URL, repo.Name}
Expand All @@ -59,7 +65,7 @@ func (app *App) initialize(repo Repo, ch chan<- Publisher) {
cmdString := fmt.Sprintf(
"git %s (in %s)",
strings.Join(args, " "),
strings.Replace(cmd.Dir, app.HomeDir, "~", 1),
app.PrettyPath(cmd.Dir),
)
err := cmd.Run()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (app *App) update(repo Repo, ch chan<- Publisher) {
cmdString := fmt.Sprintf(
"git %s (in %s)",
strings.Join(args, " "),
strings.Replace(cmd.Dir, app.HomeDir, "~", 1),
app.PrettyPath(cmd.Dir),
)
err := cmd.Run()
if err != nil {
Expand Down

0 comments on commit 454b1c2

Please sign in to comment.