Skip to content

Commit

Permalink
This is ugly now, but it works.
Browse files Browse the repository at this point in the history
  • Loading branch information
telemachus committed Oct 27, 2024
1 parent 454b1c2 commit c1f3b98
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
gitmirror
gitmirror-init
gitmirror-help
gitmirror-version
gitmirror-initialize
gitmirror-update
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.DEFAULT_GOAL := test

PREFIX := $(HOME)/local/gitmirror

fmt:
golangci-lint run --disable-all --no-config -Egofmt --fix
golangci-lint run --disable-all --no-config -Egofumpt --fix
Expand All @@ -18,13 +20,23 @@ testr:

build: lint testr
go build ./cmd/gitmirror
go build ./cmd/gitmirror-init
go build ./cmd/gitmirror-help
go build ./cmd/gitmirror-version
go build ./cmd/gitmirror-initialize
go build ./cmd/gitmirror-update

install: lint testr
GOBIN=$(HOME)/bin go install ./...
install: build
install -d $(PREFIX)/bin
install -d $(PREFIX)/libexec
install gitmirror $(PREFIX)/bin
install gitmirror-help $(PREFIX)/libexec
install gitmirror-version $(PREFIX)/libexec
install gitmirror-update $(PREFIX)/libexec
install gitmirror-initialize $(PREFIX)/libexec

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

.PHONY: fmt lint build install test testv testr clean
18 changes: 9 additions & 9 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
type App struct {
HomeDir string
CmdName string
Version string
CmdVersion string
Usage string
ExitValue int
HelpWanted bool
Expand All @@ -44,11 +44,11 @@ func NewApp(cmdName, cmdVersion, cmdUsage string) *App {
return &App{ExitValue: exitFailure}
}
return &App{
CmdName: cmdName,
Version: cmdVersion,
Usage: cmdUsage,
ExitValue: exitSuccess,
HomeDir: homeDir,
CmdName: cmdName,
CmdVersion: cmdVersion,
Usage: cmdUsage,
ExitValue: exitSuccess,
HomeDir: homeDir,
}
}

Expand Down Expand Up @@ -79,12 +79,12 @@ func (app *App) Flags(args []string) (string, bool) {
switch {
// This must precede all other checks.
case err != nil:
fmt.Fprintf(os.Stderr, "%s: %s\n%s\n", app.CmdName, err, app.Usage)
fmt.Fprintf(os.Stderr, "%s: %s\n%s", app.CmdName, err, app.Usage)
app.ExitValue = exitFailure
case app.HelpWanted:
fmt.Println(app.Usage)
fmt.Print(app.Usage)
case app.VersionWanted:
fmt.Printf("%s: v%s\n", app.CmdName, app.Version)
fmt.Printf("%s: v%s\n", app.CmdName, app.CmdVersion)
case configFile == "":
configFile = defaultConfig
configIsDefault = true
Expand Down
52 changes: 52 additions & 0 deletions cli/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Package cli organizes and implements a command line program.
package cli

import (
"fmt"
"os"
)

const (
helpCmd = "gitmirror-help"
helpVersion = "v0.1.0"
helpUsage = `usage: gitmirror help <command>
Display help information for the specified command
The commands are:
initialize|init Initialize 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) {
if app.NoOp() {
return
}
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "%s: no command given\n", helpCmd)
fmt.Fprint(os.Stderr, helpUsage)
app.ExitValue = exitFailure
return
}
switch args[0] {
case "init", "initialize":
fmt.Fprint(os.Stdout, initUsage)
case "up", "update":
fmt.Fprint(os.Stdout, updateUsage)
case "version":
fmt.Fprint(os.Stdout, versionUsage)
default:
fmt.Fprint(os.Stderr, helpUsage)
app.ExitValue = exitFailure
}
}

// InitRun clones requested repos locally for mirroring.
func CmdHelp(args []string) int {
app := NewApp(helpCmd, helpVersion, helpUsage)
app.Help(args)
return app.ExitValue
}
5 changes: 4 additions & 1 deletion cli/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ const (
initVersion = "v0.1.0"
initUsage = `usage: gitmirror-initialize [-config FILENAME]
Run 'git clone --mirror' on repos listed in a configuration file.
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`
-version/-v Show version
`
)

// Initialize runs git repote update on a group of repositories.
Expand Down
5 changes: 4 additions & 1 deletion cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ const (
updateVersion = "0.1.0"
updateUsage = `usage: gitmirror-update [-config FILENAME]
Run 'git remote update' on repos listed in a configuration file.
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`
-version/-v Show version
`
)

// Update runs git repote update on a group of repositories.
Expand Down
36 changes: 36 additions & 0 deletions cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"fmt"
"os"
)

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

// Version displays version information about gitmirror.
func (app *App) Version(args []string) {
if app.NoOp() {
return
}
if len(args) != 0 {
fmt.Fprintf(os.Stderr, "%s: no arguments accepted\n", versionCmd)
fmt.Fprint(os.Stderr, versionUsage)
app.ExitValue = exitFailure
return
}
fmt.Fprintf(os.Stdout, "%s: %s\n", versionCmd, versionVersion)
}

// InitRun clones requested repos locally for mirroring.
func CmdVersion(args []string) int {
app := NewApp(versionCmd, versionVersion, versionUsage)
app.Version(args)
return app.ExitValue
}
43 changes: 27 additions & 16 deletions cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)

const (
wrapperCmd = "gitmirror"
wrapperVersion = "0.1.0"
wrapperUsage = `usage: gitmirror <command> [options]
The commands are:
initialize|init
update|up
version
Use "gitmirror help <command>" for more information about a command.
`
)

func wrap(longCmd string, args []string) int {
currentCmd, err := os.Executable()
currentDir := filepath.Dir(currentCmd)
if err != nil {
fmt.Fprintf(os.Stdout, "%s: %s\n", wrapperCmd, err)
return exitFailure
}
finalDir := filepath.Base(currentDir)
pathExtra := strings.TrimSuffix(currentDir, finalDir) + "libexec:"
path := os.Getenv("PATH")
path = pathExtra + path
err = os.Setenv("PATH", path)
if err != nil {
fmt.Fprintf(os.Stdout, "%s: %s\n", wrapperCmd, err)
return exitFailure
}
binary, lookErr := exec.LookPath(longCmd)
if lookErr != nil {
fmt.Fprintf(os.Stdout, "%s: v%s\n", wrapperCmd, lookErr)
fmt.Fprintf(os.Stdout, "%s: %s\n", wrapperCmd, lookErr)
return exitFailure
}
args[0] = longCmd
env := os.Environ()
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
fmt.Fprintf(os.Stdout, "%s: v%s\n", wrapperCmd, execErr)
fmt.Fprintf(os.Stdout, "%s: %s\n", wrapperCmd, execErr)
return exitFailure
}
return exitSuccess
Expand All @@ -40,19 +48,22 @@ func wrap(longCmd string, args []string) int {
// CmdWrapper turns, e.g., `gitmirror update --q` into `gitmirror-update --q`.
func CmdWrapper(args []string) int {
if len(args) < 1 {
fmt.Fprint(os.Stderr, wrapperUsage)
fmt.Fprint(os.Stderr, helpUsage)
return exitFailure
}
exitValue := exitSuccess
var exitValue int
switch args[0] {
case "initialize", "init":
exitValue = wrap("gitmirror-init", args)
exitValue = wrap("gitmirror-initialize", args)
case "update", "up":
exitValue = wrap("gitmirror-update", args)
case "version":
fmt.Fprintf(os.Stdout, "%s: v%s\n", wrapperCmd, wrapperVersion)
exitValue = wrap("gitmirror-version", args)
case "help":
// TODO: write the help in Asciidoc?
exitValue = wrap("gitmirror-help", args)
default:
fmt.Fprint(os.Stderr, wrapperUsage)
fmt.Fprint(os.Stderr, helpUsage)
exitValue = exitFailure
}
return exitValue
Expand Down
11 changes: 0 additions & 11 deletions cmd/gitmirror-init/main.go

This file was deleted.

0 comments on commit c1f3b98

Please sign in to comment.