89 lines
2.7 KiB
Go
89 lines
2.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"gitea.maximumdirect.net/eric/distributor/internal/config"
|
|
)
|
|
|
|
type ValidateOptions struct {
|
|
Path string
|
|
ConfigPath string
|
|
PipelineID string
|
|
BundlePath string
|
|
Stdout io.Writer
|
|
OutputFormat OutputFormat
|
|
}
|
|
|
|
func Validate(ctx context.Context, options ValidateOptions) error {
|
|
return validateWithBackendFactory(ctx, options, newBackendFactoryWithEnvironment)
|
|
}
|
|
|
|
func validateWithBackendFactory(ctx context.Context, options ValidateOptions, provider backendFactoryProvider) error {
|
|
if err := ValidateOutputFormat(options.OutputFormat); err != nil {
|
|
return err
|
|
}
|
|
selection, err := selectSourceBundles(ctx, sourceCommandOptions{
|
|
CommandName: "validate",
|
|
Path: options.Path,
|
|
ConfigPath: options.ConfigPath,
|
|
PipelineID: options.PipelineID,
|
|
BundlePath: options.BundlePath,
|
|
}, provider)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return writeValidateResult(options, selection)
|
|
}
|
|
|
|
func validateConfigWithBackendFactory(ctx context.Context, cfg config.Config, options ValidateOptions, provider backendFactoryProvider) error {
|
|
if err := ValidateOutputFormat(options.OutputFormat); err != nil {
|
|
return err
|
|
}
|
|
selection, err := selectSourceBundlesFromConfig(ctx, cfg, sourceCommandOptions{
|
|
CommandName: "validate",
|
|
PipelineID: options.PipelineID,
|
|
BundlePath: options.BundlePath,
|
|
}, provider)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return writeValidateResult(options, selection)
|
|
}
|
|
|
|
func writeValidateResult(options ValidateOptions, selection sourceSelection) error {
|
|
if IsJSONOutput(options.OutputFormat) {
|
|
return WriteJSONEnvelope(options.Stdout, "validate", true, selection.Warnings, validateResultFromSelection(selection), nil)
|
|
}
|
|
var err error
|
|
if options.Stdout != nil {
|
|
if err := writeWarnings(options.Stdout, selection.Warnings); err != nil {
|
|
return err
|
|
}
|
|
if selection.ConfigMode {
|
|
_, err = fmt.Fprintf(options.Stdout, "Validated %d bundle(s) for pipeline %s source %s\n", len(selection.Bundles), selection.PipelineID, selection.SourceBackend)
|
|
} else {
|
|
_, err = fmt.Fprintf(options.Stdout, "Validated %d bundle(s)\n", len(selection.Bundles))
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
type validateResult struct {
|
|
PipelineID string `json:"pipeline_id,omitempty"`
|
|
SourceBackend string `json:"source_backend,omitempty"`
|
|
BundleCount int `json:"bundle_count"`
|
|
Bundles []bundleSummaryResult `json:"bundles"`
|
|
}
|
|
|
|
func validateResultFromSelection(selection sourceSelection) validateResult {
|
|
return validateResult{
|
|
PipelineID: selection.PipelineID,
|
|
SourceBackend: selection.SourceBackend,
|
|
BundleCount: len(selection.Bundles),
|
|
Bundles: bundleSummariesFromBundles(selection.Bundles),
|
|
}
|
|
}
|