Files
distributor/internal/cli/source_mode.go

68 lines
1.9 KiB
Go

package cli
import (
"flag"
"fmt"
"io"
"gitea.maximumdirect.net/eric/distributor/internal/app"
)
type sourceDiagnosticArgs struct {
Path string
ConfigPath string
PipelineID string
BundlePath string
OutputFormat app.OutputFormat
}
func parseSourceDiagnosticArgs(stderr io.Writer, command string, args []string) (sourceDiagnosticArgs, bool) {
flags := flag.NewFlagSet(command, flag.ContinueOnError)
flags.SetOutput(stderr)
configPath := flags.String("config", "", "path to config file")
pipelineID := flags.String("pipeline", "", "pipeline id")
bundlePath := flags.String("bundle", "", "source-root-relative bundle path")
formatFlag := addFormatFlag(flags)
if err := flags.Parse(args); err != nil {
return sourceDiagnosticArgs{}, false
}
format, ok := parseOutputFormat(stderr, command, *formatFlag)
if !ok {
return sourceDiagnosticArgs{}, false
}
path, ok := parseOptionalPathArg(stderr, command, flags.Args())
if !ok {
return sourceDiagnosticArgs{}, false
}
if !validateInspectModeOK(stderr, command, path, *configPath, *pipelineID, *bundlePath) {
return sourceDiagnosticArgs{}, false
}
return sourceDiagnosticArgs{
Path: path,
ConfigPath: *configPath,
PipelineID: *pipelineID,
BundlePath: *bundlePath,
OutputFormat: format,
}, true
}
func validateInspectModeOK(stderr io.Writer, command, path, configPath, pipelineID, bundlePath string) bool {
configMode := configPath != "" || pipelineID != "" || bundlePath != ""
if !configMode {
return true
}
if path != "" {
fmt.Fprintf(stderr, "distributor: %s does not accept a local path with --config, --pipeline, or --bundle\n", command)
return false
}
if configPath == "" {
fmt.Fprintf(stderr, "distributor: %s requires --config when --pipeline or --bundle is set\n", command)
return false
}
if pipelineID == "" {
fmt.Fprintf(stderr, "distributor: %s requires --pipeline in config mode\n", command)
return false
}
return true
}