Skip to content

Commit

Permalink
chore: use heredoc and s/initialize/clone/
Browse files Browse the repository at this point in the history
  • Loading branch information
telemachus committed Nov 7, 2024
1 parent 41e257c commit 4bd4c88
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/gitmirror
/gitmirror-clone
/gitmirror-help
/gitmirror-version
/gitmirror-initialize
/gitmirror-update
/gitmirror-version
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ build: lint testr
go build ./cmd/gitmirror
go build ./cmd/gitmirror-help
go build ./cmd/gitmirror-version
go build ./cmd/gitmirror-initialize
go build ./cmd/gitmirror-clone
go build ./cmd/gitmirror-update

install: build
Expand All @@ -32,11 +32,11 @@ install: build
install gitmirror-help $(PREFIX)/libexec
install gitmirror-version $(PREFIX)/libexec
install gitmirror-update $(PREFIX)/libexec
install gitmirror-initialize $(PREFIX)/libexec
install gitmirror-clone $(PREFIX)/libexec

clean:
rm -f gitmirror gitmirror-help gitmirror-version
rm -f gitmirror-update gitmirror-initialize
rm -f gitmirror-update gitmirror-clone
go clean -i -r -cache

.PHONY: fmt lint build install test testv testr clean
38 changes: 21 additions & 17 deletions cli/initialize.go → cli/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ import (
"os"
"os/exec"
"path/filepath"

"github.com/MakeNowJust/heredoc"
)

const (
initCmd = "gitmirror-initialize"
initCmd = "gitmirror-clone"
initVersion = "v0.1.0"
initUsage = `usage: gitmirror-initialize [-config FILENAME]
)

Run 'git clone --mirror' on repos listed in a configuration file.
var initUsage = heredoc.Docf(
`usage: gitmirror clone [-config FILENAME]
options:
-config/-c FILENAME Specify a configuration file (default = ~/.gitmirror.json)
-quiet/-q Suppress output unless an error occurs
-help/-h Show this message
-version/-v Show version
`
)
Run %[1]sgit clone --mirror%[1]s on repos listed in a configuration file.
options:
-config/-c FILENAME Specify configuration file (default ~/.gitmirror.json)
-quiet/-q Suppress output unless an error occurs
-help/-h Show this message
-version/-v Show version
`, "`")

// Initialize runs git repote update on a group of repositories.
func (app *App) Initialize(repos []Repo) {
// Clone runs git repote update on a group of repositories.
func (app *App) Clone(repos []Repo) {
if app.NoOp() {
return
}
Expand All @@ -35,15 +39,15 @@ func (app *App) Initialize(repos []Repo) {
}
ch := make(chan result)
for _, repo := range repos {
go app.initialize(repo, ch)
go app.clone(repo, ch)
}
for range repos {
res := <-ch
res.publish(app.QuietWanted)
}
}

func (app *App) initialize(repo Repo, ch chan<- result) {
func (app *App) clone(repo Repo, ch chan<- result) {
// Normally, it is a bad idea to check whether a directory exists
// before trying an operation. However, this case is an exception.
// git clone --mirror /path/to/existing/repo.git will fail with an
Expand Down Expand Up @@ -77,11 +81,11 @@ func (app *App) initialize(repo Repo, ch chan<- result) {
}
}

// InitRun clones requested repos locally for mirroring.
func CmdInitialize(args []string) int {
// CmdClone clones requested repos locally for mirroring.
func CmdClone(args []string) int {
app := NewApp(initCmd, initVersion, initUsage)
configFile, configIsDefault := app.Flags(args)
repos := app.Unmarshal(configFile, configIsDefault)
app.Initialize(repos)
app.Clone(repos)
return app.ExitValue
}
20 changes: 12 additions & 8 deletions cli/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ package cli
import (
"fmt"
"os"

"github.com/MakeNowJust/heredoc"
)

const (
helpCmd = "gitmirror-help"
helpVersion = "v0.1.0"
helpUsage = `usage: gitmirror help <command>
)

Display help information for the specified command
var helpUsage = heredoc.Doc(`
usage: gitmirror help <command>
The commands are:
initialize|init Initialize repositories to mirror
update|up Update mirrored repositories
version Print version and exit
`
)
Display help information for the specified command
The commands are:
clone Clone repositories to mirror
update|up Update mirrored repositories
version Print version and exit
`)

// Help displays the help message for a command.
func (app *App) Help(args []string) {
Expand Down
22 changes: 13 additions & 9 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import (
"fmt"
"os/exec"
"path/filepath"

"github.com/MakeNowJust/heredoc"
)

const (
updateCmd = "gitmirror-update"
updateVersion = "0.1.0"
updateUsage = `usage: gitmirror-update [-config FILENAME]
)

Run 'git remote update' on repos listed in a configuration file.
var updateUsage = heredoc.Docf(
`usage: gitmirror-update [-config FILENAME]
options:
-config/-c FILENAME Specify a configuration file (default = ~/.gitmirror.json)
-quiet/-q Suppress output unless an error occurs
-help/-h Show this message
-version/-v Show version
`
)
Run %[1]sgit remote update%[1]s on repos listed in a configuration file.
options:
-config/-c FILENAME Specify configuration file (default ~/.gitmirror.json)
-quiet/-q Suppress output unless an error occurs
-help/-h Show this message
-version/-v Show version
`, "`")

// Update runs git repote update on a group of repositories.
func (app *App) Update(repos []Repo) {
Expand Down
12 changes: 8 additions & 4 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package cli
import (
"fmt"
"os"

"github.com/MakeNowJust/heredoc"
)

const (
versionCmd = "gitmirror-version"
versionVersion = "v0.1.0"
versionUsage = `usage: gitmirror version
Display version information about gitmirror
`
)

var versionUsage = heredoc.Doc(`
usage: gitmirror version
Display version information about gitmirror
`)

// Version displays version information about gitmirror.
func (app *App) Version(args []string) {
if app.NoOp() {
Expand Down
6 changes: 3 additions & 3 deletions cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ func wrap(longCmd string, args []string) int {
return exitSuccess
}

// CmdWrapper turns, e.g., `gitmirror update --q` into `gitmirror-update --q`.
// CmdWrapper turns, e.g., `gitmirror update -q` into `gitmirror-update -q`.
func CmdWrapper(args []string) int {
if len(args) < 1 {
fmt.Fprint(os.Stderr, helpUsage)
return exitFailure
}
var exitValue int
switch args[0] {
case "initialize", "init":
exitValue = wrap("gitmirror-initialize", args)
case "clone":
exitValue = wrap("gitmirror-clone", args)
case "update", "up":
exitValue = wrap("gitmirror-update", args)
case "version":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func main() {
os.Exit(cli.CmdInitialize(os.Args[1:]))
os.Exit(cli.CmdClone(os.Args[1:]))
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/telemachus/gitmirror

go 1.23

require github.com/google/go-cmp v0.6.0
require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/google/go-cmp v0.6.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=

0 comments on commit 4bd4c88

Please sign in to comment.