Skip to content

Commit f64602c

Browse files
authored
Merge pull request #9 from github/vdye/web-server-daemon
Improve argument parsing & implement the `web-server` command
2 parents 9429f14 + ca23190 commit f64602c

File tree

23 files changed

+1676
-81
lines changed

23 files changed

+1676
-81
lines changed

cmd/git-bundle-server/delete.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
package main
22

33
import (
4-
"errors"
54
"os"
65

6+
"github.com/github/git-bundle-server/internal/argparse"
77
"github.com/github/git-bundle-server/internal/core"
88
)
99

1010
type Delete struct{}
1111

12-
func (Delete) subcommand() string {
12+
func (Delete) Name() string {
1313
return "delete"
1414
}
1515

16-
func (Delete) run(args []string) error {
17-
if len(args) < 1 {
18-
return errors.New("usage: git-bundle-server delete <route>")
19-
}
16+
func (Delete) Description() string {
17+
return `
18+
Remove the configuration for the given '<route>' and delete its repository
19+
data.`
20+
}
2021

21-
route := args[0]
22+
func (Delete) Run(args []string) error {
23+
parser := argparse.NewArgParser("git-bundle-server delete <route>")
24+
route := parser.PositionalString("route", "the route to delete")
25+
parser.Parse(args)
2226

23-
repo, err := core.CreateRepository(route)
27+
repo, err := core.CreateRepository(*route)
2428
if err != nil {
2529
return err
2630
}
2731

28-
err = core.RemoveRoute(route)
32+
err = core.RemoveRoute(*route)
2933
if err != nil {
3034
return err
3135
}

cmd/git-bundle-server/init.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65

6+
"github.com/github/git-bundle-server/internal/argparse"
77
"github.com/github/git-bundle-server/internal/bundles"
88
"github.com/github/git-bundle-server/internal/core"
99
"github.com/github/git-bundle-server/internal/git"
1010
)
1111

1212
type Init struct{}
1313

14-
func (Init) subcommand() string {
14+
func (Init) Name() string {
1515
return "init"
1616
}
1717

18-
func (Init) run(args []string) error {
19-
if len(args) < 2 {
20-
// TODO: allow parsing <route> out of <url>
21-
return errors.New("usage: git-bundle-server init <url> <route>")
22-
}
18+
func (Init) Description() string {
19+
return `
20+
Initialize a repository by cloning a bare repo from '<url>', whose bundles
21+
should be hosted at '<route>'.`
22+
}
2323

24-
url := args[0]
25-
route := args[1]
24+
func (Init) Run(args []string) error {
25+
parser := argparse.NewArgParser("git-bundle-server init <url> <route>")
26+
url := parser.PositionalString("url", "the URL of a repository to clone")
27+
// TODO: allow parsing <route> out of <url>
28+
route := parser.PositionalString("route", "the route to host the specified repo")
29+
parser.Parse(args)
2630

27-
repo, err := core.CreateRepository(route)
31+
repo, err := core.CreateRepository(*route)
2832
if err != nil {
2933
return err
3034
}
3135

32-
fmt.Printf("Cloning repository from %s\n", url)
33-
gitErr := git.GitCommand("clone", "--bare", url, repo.RepoDir)
36+
fmt.Printf("Cloning repository from %s\n", *url)
37+
gitErr := git.GitCommand("clone", "--bare", *url, repo.RepoDir)
3438

3539
if gitErr != nil {
3640
return fmt.Errorf("failed to clone repository: %w", gitErr)

cmd/git-bundle-server/main.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,34 @@ package main
33
import (
44
"log"
55
"os"
6+
7+
"github.com/github/git-bundle-server/internal/argparse"
68
)
79

10+
func all() []argparse.Subcommand {
11+
return []argparse.Subcommand{
12+
Delete{},
13+
Init{},
14+
Start{},
15+
Stop{},
16+
Update{},
17+
UpdateAll{},
18+
NewWebServerCommand(),
19+
}
20+
}
21+
822
func main() {
923
cmds := all()
1024

11-
if len(os.Args) < 2 {
12-
log.Fatal("usage: git-bundle-server <command> [<options>]\n")
13-
return
25+
parser := argparse.NewArgParser("git-bundle-server <command> [<options>]")
26+
parser.SetIsTopLevel(true)
27+
for _, cmd := range cmds {
28+
parser.Subcommand(cmd)
1429
}
30+
parser.Parse(os.Args[1:])
1531

16-
for i := 0; i < len(cmds); i++ {
17-
if cmds[i].subcommand() == os.Args[1] {
18-
err := cmds[i].run(os.Args[2:])
19-
if err != nil {
20-
log.Fatal("Failed with error: ", err)
21-
}
22-
}
32+
err := parser.InvokeSubcommand()
33+
if err != nil {
34+
log.Fatal("Failed with error: ", err)
2335
}
2436
}

cmd/git-bundle-server/start.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65
"os"
76

7+
"github.com/github/git-bundle-server/internal/argparse"
88
"github.com/github/git-bundle-server/internal/core"
99
)
1010

1111
type Start struct{}
1212

13-
func (Start) subcommand() string {
13+
func (Start) Name() string {
1414
return "start"
1515
}
1616

17-
func (Start) run(args []string) error {
18-
if len(args) < 1 {
19-
return errors.New("usage: git-bundle-server start <route>")
20-
}
17+
func (Start) Description() string {
18+
return `
19+
Start computing bundles and serving content for the repository at the
20+
specified '<route>'.`
21+
}
2122

22-
route := args[0]
23+
func (Start) Run(args []string) error {
24+
parser := argparse.NewArgParser("git-bundle-server start <route>")
25+
route := parser.PositionalString("route", "the route for which bundles should be generated")
26+
parser.Parse(args)
2327

2428
// CreateRepository registers the route.
25-
repo, err := core.CreateRepository(route)
29+
repo, err := core.CreateRepository(*route)
2630
if err != nil {
2731
return err
2832
}
2933

3034
_, err = os.ReadDir(repo.RepoDir)
3135
if err != nil {
32-
return fmt.Errorf("route '%s' appears to have been deleted; use 'init' instead", route)
36+
return fmt.Errorf("route '%s' appears to have been deleted; use 'init' instead", *route)
3337
}
3438

3539
// Make sure we have the global schedule running.

cmd/git-bundle-server/stop.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package main
22

33
import (
4-
"errors"
5-
4+
"github.com/github/git-bundle-server/internal/argparse"
65
"github.com/github/git-bundle-server/internal/core"
76
)
87

98
type Stop struct{}
109

11-
func (Stop) subcommand() string {
10+
func (Stop) Name() string {
1211
return "stop"
1312
}
1413

15-
func (Stop) run(args []string) error {
16-
if len(args) < 1 {
17-
return errors.New("usage: git-bundle-server stop <route>")
18-
}
14+
func (Stop) Description() string {
15+
return `
16+
Stop computing bundles or serving content for the repository at the
17+
specified '<route>'.`
18+
}
1919

20-
route := args[0]
20+
func (Stop) Run(args []string) error {
21+
parser := argparse.NewArgParser("git-bundle-server stop <route>")
22+
route := parser.PositionalString("route", "the route for which bundles should stop being generated")
23+
parser.Parse(args)
2124

22-
return core.RemoveRoute(route)
25+
return core.RemoveRoute(*route)
2326
}

cmd/git-bundle-server/subcommand.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

cmd/git-bundle-server/update-all.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@ import (
55
"os"
66
"os/exec"
77

8+
"github.com/github/git-bundle-server/internal/argparse"
89
"github.com/github/git-bundle-server/internal/core"
910
)
1011

1112
type UpdateAll struct{}
1213

13-
func (UpdateAll) subcommand() string {
14+
func (UpdateAll) Name() string {
1415
return "update-all"
1516
}
1617

17-
func (UpdateAll) run(args []string) error {
18+
func (UpdateAll) Description() string {
19+
return `
20+
For every configured route, run 'git-bundle-server update <options> <route>'.`
21+
}
22+
23+
func (UpdateAll) Run(args []string) error {
24+
parser := argparse.NewArgParser("git-bundle-server update-all")
25+
parser.Parse(args)
26+
1827
exe, err := os.Executable()
1928
if err != nil {
2029
return fmt.Errorf("failed to get path to execuable: %w", err)

cmd/git-bundle-server/update.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65

6+
"github.com/github/git-bundle-server/internal/argparse"
77
"github.com/github/git-bundle-server/internal/bundles"
88
"github.com/github/git-bundle-server/internal/core"
99
)
1010

1111
type Update struct{}
1212

13-
func (Update) subcommand() string {
13+
func (Update) Name() string {
1414
return "update"
1515
}
1616

17-
func (Update) run(args []string) error {
18-
if len(args) != 1 {
19-
// TODO: allow parsing <route> out of <url>
20-
return errors.New("usage: git-bundle-server update <route>")
21-
}
17+
func (Update) Description() string {
18+
return `
19+
For the repository in the current directory (or the one specified by
20+
'<route>'), fetch the latest content from the remote, create a new set of
21+
bundles, and update the bundle list.`
22+
}
23+
24+
func (Update) Run(args []string) error {
25+
parser := argparse.NewArgParser("git-bundle-server update <route>")
26+
route := parser.PositionalString("route", "the route to update")
27+
parser.Parse(args)
2228

23-
route := args[0]
24-
repo, err := core.CreateRepository(route)
29+
repo, err := core.CreateRepository(*route)
2530
if err != nil {
2631
return err
2732
}

0 commit comments

Comments
 (0)