Skip to content

Commit 7f309c0

Browse files
committed
git-bundle-server: add constructors for all commands
In later patches, a logger struct will need to be a member of each command struct to facilitate structured logging & tracing. Like the 'web-server' command in ca23190 (bundle-server: add 'web-server' subcommand, 2023-01-05), add explicit constructors (i.e., 'New<Command>()') to the remaining commands. Signed-off-by: Victoria Dye <[email protected]>
1 parent 13e2319 commit 7f309c0

File tree

8 files changed

+64
-40
lines changed

8 files changed

+64
-40
lines changed

cmd/git-bundle-server/delete.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ import (
88
"github.com/github/git-bundle-server/internal/core"
99
)
1010

11-
type Delete struct{}
11+
type deleteCmd struct{}
1212

13-
func (Delete) Name() string {
13+
func NewDeleteCommand() argparse.Subcommand {
14+
return &deleteCmd{}
15+
}
16+
17+
func (deleteCmd) Name() string {
1418
return "delete"
1519
}
1620

17-
func (Delete) Description() string {
21+
func (deleteCmd) Description() string {
1822
return `
1923
Remove the configuration for the given '<route>' and delete its repository
2024
data.`
2125
}
2226

23-
func (Delete) Run(ctx context.Context, args []string) error {
27+
func (deleteCmd) Run(ctx context.Context, args []string) error {
2428
parser := argparse.NewArgParser("git-bundle-server delete <route>")
2529
route := parser.PositionalString("route", "the route to delete")
2630
parser.Parse(ctx, args)

cmd/git-bundle-server/init.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@ import (
1010
"github.com/github/git-bundle-server/internal/git"
1111
)
1212

13-
type Init struct{}
13+
type initCmd struct{}
1414

15-
func (Init) Name() string {
15+
func NewInitCommand() argparse.Subcommand {
16+
return &initCmd{}
17+
}
18+
19+
func (initCmd) Name() string {
1620
return "init"
1721
}
1822

19-
func (Init) Description() string {
23+
func (initCmd) Description() string {
2024
return `
2125
Initialize a repository by cloning a bare repo from '<url>', whose bundles
2226
should be hosted at '<route>'.`
2327
}
2428

25-
func (Init) Run(ctx context.Context, args []string) error {
29+
func (initCmd) Run(ctx context.Context, args []string) error {
2630
parser := argparse.NewArgParser("git-bundle-server init <url> <route>")
2731
url := parser.PositionalString("url", "the URL of a repository to clone")
2832
// TODO: allow parsing <route> out of <url>

cmd/git-bundle-server/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010

1111
func all() []argparse.Subcommand {
1212
return []argparse.Subcommand{
13-
Delete{},
14-
Init{},
15-
Start{},
16-
Stop{},
17-
Update{},
18-
UpdateAll{},
13+
NewDeleteCommand(),
14+
NewInitCommand(),
15+
NewStartCommand(),
16+
NewStopCommand(),
17+
NewUpdateCommand(),
18+
NewUpdateAllCommand(),
1919
NewWebServerCommand(),
2020
}
2121
}

cmd/git-bundle-server/start.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,23 @@ import (
99
"github.com/github/git-bundle-server/internal/core"
1010
)
1111

12-
type Start struct{}
12+
type startCmd struct{}
1313

14-
func (Start) Name() string {
14+
func NewStartCommand() argparse.Subcommand {
15+
return &startCmd{}
16+
}
17+
18+
func (startCmd) Name() string {
1519
return "start"
1620
}
1721

18-
func (Start) Description() string {
22+
func (startCmd) Description() string {
1923
return `
2024
Start computing bundles and serving content for the repository at the
2125
specified '<route>'.`
2226
}
2327

24-
func (Start) Run(ctx context.Context, args []string) error {
28+
func (startCmd) Run(ctx context.Context, args []string) error {
2529
parser := argparse.NewArgParser("git-bundle-server start <route>")
2630
route := parser.PositionalString("route", "the route for which bundles should be generated")
2731
parser.Parse(ctx, args)

cmd/git-bundle-server/stop.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ import (
77
"github.com/github/git-bundle-server/internal/core"
88
)
99

10-
type Stop struct{}
10+
type stopCmd struct{}
1111

12-
func (Stop) Name() string {
12+
func NewStopCommand() argparse.Subcommand {
13+
return &stopCmd{}
14+
}
15+
16+
func (stopCmd) Name() string {
1317
return "stop"
1418
}
1519

16-
func (Stop) Description() string {
20+
func (stopCmd) Description() string {
1721
return `
1822
Stop computing bundles or serving content for the repository at the
1923
specified '<route>'.`
2024
}
2125

22-
func (Stop) Run(ctx context.Context, args []string) error {
26+
func (stopCmd) Run(ctx context.Context, args []string) error {
2327
parser := argparse.NewArgParser("git-bundle-server stop <route>")
2428
route := parser.PositionalString("route", "the route for which bundles should stop being generated")
2529
parser.Parse(ctx, args)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ import (
1111
"github.com/github/git-bundle-server/internal/core"
1212
)
1313

14-
type UpdateAll struct{}
14+
type updateAllCmd struct{}
1515

16-
func (UpdateAll) Name() string {
16+
func NewUpdateAllCommand() argparse.Subcommand {
17+
return &updateAllCmd{}
18+
}
19+
20+
func (updateAllCmd) Name() string {
1721
return "update-all"
1822
}
1923

20-
func (UpdateAll) Description() string {
24+
func (updateAllCmd) Description() string {
2125
return `
2226
For every configured route, run 'git-bundle-server update <options> <route>'.`
2327
}
2428

25-
func (UpdateAll) Run(ctx context.Context, args []string) error {
29+
func (updateAllCmd) Run(ctx context.Context, args []string) error {
2630
user, err := common.NewUserProvider().CurrentUser()
2731
if err != nil {
2832
return err

cmd/git-bundle-server/update.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ import (
99
"github.com/github/git-bundle-server/internal/core"
1010
)
1111

12-
type Update struct{}
12+
type updateCmd struct{}
1313

14-
func (Update) Name() string {
14+
func NewUpdateCommand() argparse.Subcommand {
15+
return &updateAllCmd{}
16+
}
17+
18+
func (updateCmd) Name() string {
1519
return "update"
1620
}
1721

18-
func (Update) Description() string {
22+
func (updateCmd) Description() string {
1923
return `
2024
For the repository in the current directory (or the one specified by
2125
'<route>'), fetch the latest content from the remote, create a new set of
2226
bundles, and update the bundle list.`
2327
}
2428

25-
func (Update) Run(ctx context.Context, args []string) error {
29+
func (updateCmd) Run(ctx context.Context, args []string) error {
2630
parser := argparse.NewArgParser("git-bundle-server update <route>")
2731
route := parser.PositionalString("route", "the route to update")
2832
parser.Parse(ctx, args)

cmd/git-bundle-server/web-server.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,30 @@ import (
1515
"github.com/github/git-bundle-server/internal/daemon"
1616
)
1717

18-
type webServer struct {
18+
type webServerCmd struct {
1919
user common.UserProvider
2020
cmdExec common.CommandExecutor
2121
fileSystem common.FileSystem
2222
}
2323

24-
func NewWebServerCommand() *webServer {
25-
// Create dependencies
26-
return &webServer{
24+
func NewWebServerCommand() argparse.Subcommand {
25+
// Create subcommand-specific dependencies
26+
return &webServerCmd{
2727
user: common.NewUserProvider(),
2828
cmdExec: common.NewCommandExecutor(),
2929
fileSystem: common.NewFileSystem(),
3030
}
3131
}
3232

33-
func (webServer) Name() string {
33+
func (webServerCmd) Name() string {
3434
return "web-server"
3535
}
3636

37-
func (webServer) Description() string {
37+
func (webServerCmd) Description() string {
3838
return `Manage the web server hosting bundle content`
3939
}
4040

41-
func (w *webServer) getDaemonConfig() (*daemon.DaemonConfig, error) {
41+
func (w *webServerCmd) getDaemonConfig() (*daemon.DaemonConfig, error) {
4242
// Find git-bundle-web-server
4343
// First, search for it on the path
4444
programPath, err := exec.LookPath("git-bundle-web-server")
@@ -77,7 +77,7 @@ func (w *webServer) getDaemonConfig() (*daemon.DaemonConfig, error) {
7777
}, nil
7878
}
7979

80-
func (w *webServer) startServer(ctx context.Context, args []string) error {
80+
func (w *webServerCmd) startServer(ctx context.Context, args []string) error {
8181
// Parse subcommand arguments
8282
parser := argparse.NewArgParser("git-bundle-server web-server start [-f|--force]")
8383

@@ -144,7 +144,7 @@ func (w *webServer) startServer(ctx context.Context, args []string) error {
144144
return nil
145145
}
146146

147-
func (w *webServer) stopServer(ctx context.Context, args []string) error {
147+
func (w *webServerCmd) stopServer(ctx context.Context, args []string) error {
148148
// Parse subcommand arguments
149149
parser := argparse.NewArgParser("git-bundle-server web-server stop [--remove]")
150150
remove := parser.Bool("remove", false, "Remove the web server daemon configuration from the system after stopping")
@@ -175,7 +175,7 @@ func (w *webServer) stopServer(ctx context.Context, args []string) error {
175175
return nil
176176
}
177177

178-
func (w *webServer) Run(ctx context.Context, args []string) error {
178+
func (w *webServerCmd) Run(ctx context.Context, args []string) error {
179179
// Parse command arguments
180180
parser := argparse.NewArgParser("git-bundle-server web-server (start|stop) <options>")
181181
parser.Subcommand(argparse.NewSubcommand("start", "Start the web server", w.startServer))

0 commit comments

Comments
 (0)