Skip to content

Commit 5377d04

Browse files
committed
web-server: capture and use web server options
Allow 'git-bundle-server web-server' to specify command line arguments for the 'git-bundle-web-server' daemon. Like 'git-bundle-web-server' itself, use 'utils.WebServerFlags()' to configure the options (with a usage string that indicates that it is a web server flag) & validation. The results of the options are added to 'DaemonConfig.Arguments', with '--cert' and '--key' explicitly converted to absolute paths to ensure the daemon finds the files correctly. Signed-off-by: Victoria Dye <[email protected]>
1 parent 731f620 commit 5377d04

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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

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

33
import (
44
"errors"
5+
"flag"
56
"fmt"
67
"os"
78
"os/exec"
89
"path/filepath"
910

11+
"github.com/github/git-bundle-server/cmd/utils"
1012
"github.com/github/git-bundle-server/internal/argparse"
1113
"github.com/github/git-bundle-server/internal/common"
1214
"github.com/github/git-bundle-server/internal/daemon"
@@ -77,9 +79,19 @@ func (w *webServer) getDaemonConfig() (*daemon.DaemonConfig, error) {
7779
func (w *webServer) startServer(args []string) error {
7880
// Parse subcommand arguments
7981
parser := argparse.NewArgParser("git-bundle-server web-server start [-f|--force]")
82+
83+
// Args for 'git-bundle-server web-server start'
8084
force := parser.Bool("force", false, "Whether to force reconfiguration of the web server daemon")
8185
parser.BoolVar(force, "f", false, "Alias of --force")
86+
87+
// Arguments passed through to 'git-bundle-web-server'
88+
webServerFlags, validate := utils.WebServerFlags(parser)
89+
webServerFlags.VisitAll(func(f *flag.Flag) {
90+
parser.Var(f.Value, f.Name, fmt.Sprintf("[Web server] %s", f.Usage))
91+
})
92+
8293
parser.Parse(args)
94+
validate()
8395

8496
d, err := daemon.NewDaemonProvider(w.user, w.cmdExec, w.fileSystem)
8597
if err != nil {
@@ -91,6 +103,33 @@ func (w *webServer) startServer(args []string) error {
91103
return err
92104
}
93105

106+
// Configure flags
107+
loopErr := error(nil)
108+
parser.Visit(func(f *flag.Flag) {
109+
if webServerFlags.Lookup(f.Name) != nil {
110+
value := f.Value.String()
111+
if f.Name == "cert" || f.Name == "key" {
112+
// Need the absolute value of the path
113+
value, err = filepath.Abs(value)
114+
if err != nil {
115+
if loopErr == nil {
116+
// NEEDSWORK: Only report the first error because Go
117+
// doesn't like it when you manually chain errors :(
118+
// Luckily, this is slated to change in v1.20, per
119+
// https://tip.golang.org/doc/go1.20#errors
120+
loopErr = fmt.Errorf("could not get absolute path of '%s': %w", f.Name, err)
121+
}
122+
return
123+
}
124+
}
125+
config.Arguments = append(config.Arguments, fmt.Sprintf("--%s", f.Name), value)
126+
}
127+
})
128+
if loopErr != nil {
129+
// Error happened in 'Visit'
130+
return loopErr
131+
}
132+
94133
err = d.Create(config, *force)
95134
if err != nil {
96135
return err

0 commit comments

Comments
 (0)