netwatch-server refuses arguments it does not understand (closes #179)
check / check (push) Waiting to run

cli.Main starts the server only when given no arguments. check-cidr
CIDR and prepare-data-dir DATA_DIR run as before. Any other command
line, such as a subcommand with no value or two values, or a misspelt
subcommand, gets a one-line usage message naming both forms on stderr
and exit code 2. It used to start the server, which then held the
port.

Model: opus-5-5
This commit is contained in:
2026-10-19 05:59:51 +00:00
parent 7a3c706fe6
commit a083e57e2e
4 changed files with 91 additions and 12 deletions
+25 -11
View File
@@ -1,7 +1,7 @@
// Package cli runs netwatch-server from its command line: the
// check-cidr and prepare-data-dir subcommands, which bin/entrypoint.sh
// runs before it starts the server, and otherwise the server, with the
// fx wiring.
// runs before it starts the server, and, given no arguments, the
// server, with the fx wiring.
package cli
import (
@@ -22,16 +22,21 @@ import (
"go.uber.org/fx/fxevent"
)
// usageExitCode is what Main returns for a command line it does not
// understand, the usual exit code for a usage error.
const usageExitCode = 2
// Main runs netwatch-server with args, its command line after the
// program's name, and returns the exit code; the subcommands write
// their errors to stderr. Unless args are "check-cidr CIDR" or
// "prepare-data-dir DATA_DIR", Main runs the server by calling serve
// with appname and version, the name and version the server logs and
// reports. main passes os.Stderr and RunServer; the tests pass a
// buffer and a function that fails them, so that no test starts the
// server. When serve is RunServer and the server fails to start, to
// listen or to stop, fx's Run exits the process with status 1 and Main
// never returns.
// program's name, and returns the exit code. With no args, Main runs
// the server by calling serve with appname and version, the name and
// version the server logs and reports. With "check-cidr CIDR" or
// "prepare-data-dir DATA_DIR" it runs that subcommand, which writes its
// errors to stderr. Any other args get a one-line usage message on
// stderr and exit code 2. main passes os.Stderr and RunServer; the
// tests pass a buffer and a function of their own, so that no test
// starts the server. When serve is RunServer and the server fails to
// start, to listen or to stop, fx's Run exits the process with status 1
// and Main never returns.
func Main(
stderr io.Writer,
appname, version string,
@@ -53,6 +58,15 @@ func Main(
return prepareDataDir(stderr, "/data", args[1], "netwatch")
}
// Any other command line is refused, so that a mistyped one does
// not start the server.
if len(args) > 0 {
_, _ = fmt.Fprintln(stderr,
"usage: netwatch-server [check-cidr CIDR | prepare-data-dir DATA_DIR]")
return usageExitCode
}
serve(appname, version)
return 0