Skip to content

Commit df50237

Browse files
committed
start/stop: allow temporarily removing a route
1 parent 5259317 commit df50237

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

cmd/git-bundle-server/start.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
6+
"github.com/github/git-bundle-server/internal/core"
7+
)
8+
9+
type Start struct{}
10+
11+
func (Start) subcommand() string {
12+
return "start"
13+
}
14+
15+
func (Start) run(args []string) error {
16+
if len(args) < 1 {
17+
return errors.New("usage: git-bundle-server start <route>")
18+
}
19+
20+
route := args[0]
21+
22+
// CreateRepository re-reigsters the route.
23+
_, err := core.CreateRepository(route)
24+
if err != nil {
25+
return err
26+
}
27+
28+
// Make sure we have the global schedule running.
29+
SetCronSchedule()
30+
31+
return nil
32+
}

cmd/git-bundle-server/stop.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
6+
"github.com/github/git-bundle-server/internal/core"
7+
)
8+
9+
type Stop struct{}
10+
11+
func (Stop) subcommand() string {
12+
return "stop"
13+
}
14+
15+
func (Stop) run(args []string) error {
16+
if len(args) < 1 {
17+
return errors.New("usage: git-bundle-server stop <route>")
18+
}
19+
20+
route := args[0]
21+
22+
return core.RemoveRoute(route)
23+
}

cmd/git-bundle-server/subcommand.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type Subcommand interface {
88
func all() []Subcommand {
99
return []Subcommand{
1010
Init{},
11+
Start{},
12+
Stop{},
1113
Update{},
1214
UpdateAll{},
1315
}

internal/core/repo.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ func CreateRepository(route string) (*Repository, error) {
4949
return &repo, nil
5050
}
5151

52+
func RemoveRoute(route string) error {
53+
repos, err := GetRepositories()
54+
if err != nil {
55+
return fmt.Errorf("failed to parse routes file")
56+
}
57+
58+
_, contains := repos[route]
59+
if !contains {
60+
return fmt.Errorf("route '%s' is not registered", route)
61+
}
62+
63+
delete(repos, route)
64+
65+
return WriteRouteFile(repos)
66+
}
67+
5268
func WriteRouteFile(repos map[string]Repository) error {
5369
dir := bundleroot()
5470
routefile := dir + "/routes"
@@ -59,7 +75,7 @@ func WriteRouteFile(repos map[string]Repository) error {
5975
contents = contents + routes + "\n"
6076
}
6177

62-
return os.WriteFile(routefile, []byte(contents), 0600)
78+
return os.WriteFile(routefile, []byte(contents), 0o600)
6379
}
6480

6581
func GetRepositories() (map[string]Repository, error) {
@@ -68,7 +84,7 @@ func GetRepositories() (map[string]Repository, error) {
6884
dir := bundleroot()
6985
routefile := dir + "/routes"
7086

71-
file, err := os.OpenFile(routefile, os.O_RDONLY|os.O_CREATE, 0600)
87+
file, err := os.OpenFile(routefile, os.O_RDONLY|os.O_CREATE, 0o600)
7288
if err != nil {
7389
// Assume that the file doesn't exist?
7490
return repos, nil

0 commit comments

Comments
 (0)