109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"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 "reconcile-state":
|
|
return reconcileStateCommand(ctx, args[1:], stdout, stderr)
|
|
case "serve":
|
|
return serveCommand(ctx, args[1:], stdout, stderr)
|
|
case "validate":
|
|
return validateCommand(ctx, args[1:], stdout, stderr)
|
|
case "inspect":
|
|
return inspectCommand(ctx, args[1:], stdout, stderr)
|
|
case "manifest":
|
|
return manifestCommand(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
|
|
reconcile-state
|
|
Repair missing managed-output records in destination state
|
|
serve Run the HTTP upload server
|
|
validate Validate a source bundle or bundle tree
|
|
inspect Inspect bundles or distributor state
|
|
manifest Create source bundle manifests
|
|
|
|
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 {
|
|
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
|
|
}
|
|
|
|
func parseOptionalPathArg(stderr io.Writer, command string, args []string) (string, bool) {
|
|
if len(args) > 1 {
|
|
fmt.Fprintf(stderr, "%s: %s accepts at most one path\n", app.Name, command)
|
|
return "", false
|
|
}
|
|
if len(args) == 0 {
|
|
return "", true
|
|
}
|
|
return args[0], true
|
|
}
|
|
|
|
func rejectPositionalArgs(stderr io.Writer, command string, args []string) bool {
|
|
if len(args) == 0 {
|
|
return false
|
|
}
|
|
fmt.Fprintf(stderr, "%s: %s does not accept positional arguments: %v\n", app.Name, command, args)
|
|
return true
|
|
}
|