Add initial distributor CLI skeleton

This commit is contained in:
2026-05-31 01:47:39 +00:00
parent 0818733b19
commit 22d0424232
19 changed files with 436 additions and 2 deletions

84
internal/cli/root.go Normal file
View File

@@ -0,0 +1,84 @@
package cli
import (
"context"
"errors"
"fmt"
"io"
"strings"
"gitea.maximumdirect.net/eric/distributor/internal/app"
)
const (
exitOK = 0
exitError = 1
exitUsage = 2
)
func Execute(ctx context.Context, args []string, stdout, stderr io.Writer) int {
if len(args) == 0 {
printRootHelp(stdout)
return exitOK
}
switch args[0] {
case "-h", "--help", "help":
printRootHelp(stdout)
return exitOK
case "version":
return versionCommand(ctx, args[1:], stdout, stderr)
case "run":
return runCommand(ctx, args[1:], stdout, stderr)
case "validate":
return validateCommand(ctx, args[1:], stdout, stderr)
case "inspect":
return inspectCommand(ctx, args[1:], stdout, stderr)
default:
fmt.Fprintf(stderr, "%s: unknown command %q\n\n", app.Name, args[0])
printRootHelp(stderr)
return exitUsage
}
}
func printRootHelp(w io.Writer) {
fmt.Fprintf(w, `%s validates and publishes manifested report bundles.
Usage:
%s <command> [options]
Commands:
version Print version information
run Run configured distribution pipelines
validate Validate a source bundle or bundle tree
inspect Inspect bundles or distributor state
Use "%s <command> --help" for command-specific help.
`, app.Name, app.Name, app.Name)
}
func hasHelp(args []string) bool {
for _, arg := range args {
if arg == "-h" || arg == "--help" {
return true
}
}
return false
}
func fail(stderr io.Writer, err error) int {
if errors.Is(err, app.ErrNotImplemented) {
fmt.Fprintf(stderr, "%s: %s\n", app.Name, err)
return exitError
}
fmt.Fprintf(stderr, "%s: %s\n", app.Name, err)
return exitError
}
func rejectExtraArgs(stderr io.Writer, command string, args []string) bool {
if len(args) == 0 {
return false
}
fmt.Fprintf(stderr, "%s: %s does not accept arguments: %s\n", app.Name, command, strings.Join(args, " "))
return true
}