Skip to content

Commit 2d4657d

Browse files
committed
bundle-server: update commands to use arg parser
Update the 'git-bundle-server' commands to use argument parsers to parse their flags and positional arguments (none currently use subcommands). Signed-off-by: Victoria Dye <[email protected]>
1 parent a4da0f6 commit 2d4657d

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

cmd/git-bundle-server/delete.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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

@@ -20,18 +20,16 @@ data.`
2020
}
2121

2222
func (Delete) Run(args []string) error {
23-
if len(args) < 1 {
24-
return errors.New("usage: git-bundle-server delete <route>")
25-
}
26-
27-
route := args[0]
23+
parser := argparse.NewArgParser("git-bundle-server delete <route>")
24+
route := parser.PositionalString("route", "the route to delete")
25+
parser.Parse(args)
2826

29-
repo, err := core.CreateRepository(route)
27+
repo, err := core.CreateRepository(*route)
3028
if err != nil {
3129
return err
3230
}
3331

34-
err = core.RemoveRoute(route)
32+
err = core.RemoveRoute(*route)
3533
if err != nil {
3634
return err
3735
}

cmd/git-bundle-server/init.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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"
@@ -22,21 +22,19 @@ should be hosted at '<route>'.`
2222
}
2323

2424
func (Init) Run(args []string) error {
25-
if len(args) < 2 {
26-
// TODO: allow parsing <route> out of <url>
27-
return errors.New("usage: git-bundle-server init <url> <route>")
28-
}
29-
30-
url := args[0]
31-
route := args[1]
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)
3230

33-
repo, err := core.CreateRepository(route)
31+
repo, err := core.CreateRepository(*route)
3432
if err != nil {
3533
return err
3634
}
3735

38-
fmt.Printf("Cloning repository from %s\n", url)
39-
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)
4038

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

cmd/git-bundle-server/start.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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

@@ -21,21 +21,19 @@ specified '<route>'.`
2121
}
2222

2323
func (Start) Run(args []string) error {
24-
if len(args) < 1 {
25-
return errors.New("usage: git-bundle-server start <route>")
26-
}
27-
28-
route := args[0]
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)
2927

3028
// CreateRepository registers the route.
31-
repo, err := core.CreateRepository(route)
29+
repo, err := core.CreateRepository(*route)
3230
if err != nil {
3331
return err
3432
}
3533

3634
_, err = os.ReadDir(repo.RepoDir)
3735
if err != nil {
38-
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)
3937
}
4038

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

cmd/git-bundle-server/stop.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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

@@ -19,11 +18,9 @@ specified '<route>'.`
1918
}
2019

2120
func (Stop) Run(args []string) error {
22-
if len(args) < 1 {
23-
return errors.New("usage: git-bundle-server stop <route>")
24-
}
25-
26-
route := args[0]
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)
2724

28-
return core.RemoveRoute(route)
25+
return core.RemoveRoute(*route)
2926
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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

@@ -20,6 +21,9 @@ For every configured route, run 'git-bundle-server update <options> <route>'.`
2021
}
2122

2223
func (UpdateAll) Run(args []string) error {
24+
parser := argparse.NewArgParser("git-bundle-server update-all")
25+
parser.Parse(args)
26+
2327
exe, err := os.Executable()
2428
if err != nil {
2529
return fmt.Errorf("failed to get path to execuable: %w", err)

cmd/git-bundle-server/update.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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
)
@@ -22,13 +22,11 @@ bundles, and update the bundle list.`
2222
}
2323

2424
func (Update) Run(args []string) error {
25-
if len(args) != 1 {
26-
// TODO: allow parsing <route> out of <url>
27-
return errors.New("usage: git-bundle-server update <route>")
28-
}
25+
parser := argparse.NewArgParser("git-bundle-server update <route>")
26+
route := parser.PositionalString("route", "the route to update")
27+
parser.Parse(args)
2928

30-
route := args[0]
31-
repo, err := core.CreateRepository(route)
29+
repo, err := core.CreateRepository(*route)
3230
if err != nil {
3331
return err
3432
}

0 commit comments

Comments
 (0)