151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"gitea.maximumdirect.net/eric/distributor/internal/config"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
|
)
|
|
|
|
type InspectOptions struct {
|
|
Path string
|
|
ConfigPath string
|
|
PipelineID string
|
|
BundlePath string
|
|
Stdout io.Writer
|
|
OutputFormat OutputFormat
|
|
}
|
|
|
|
func Inspect(ctx context.Context, options InspectOptions) error {
|
|
return inspectWithBackendFactory(ctx, options, newBackendFactoryWithEnvironment)
|
|
}
|
|
|
|
func inspectWithBackendFactory(ctx context.Context, options InspectOptions, provider backendFactoryProvider) error {
|
|
if err := ValidateOutputFormat(options.OutputFormat); err != nil {
|
|
return err
|
|
}
|
|
selection, err := selectSourceBundles(ctx, sourceCommandOptions{
|
|
CommandName: "inspect",
|
|
Path: options.Path,
|
|
ConfigPath: options.ConfigPath,
|
|
PipelineID: options.PipelineID,
|
|
BundlePath: options.BundlePath,
|
|
}, provider)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return writeInspectResult(options, selection)
|
|
}
|
|
|
|
func inspectConfigWithBackendFactory(ctx context.Context, cfg config.Config, options InspectOptions, provider backendFactoryProvider) error {
|
|
if err := ValidateOutputFormat(options.OutputFormat); err != nil {
|
|
return err
|
|
}
|
|
selection, err := selectSourceBundlesFromConfig(ctx, cfg, sourceCommandOptions{
|
|
CommandName: "inspect",
|
|
PipelineID: options.PipelineID,
|
|
BundlePath: options.BundlePath,
|
|
}, provider)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return writeInspectResult(options, selection)
|
|
}
|
|
|
|
func writeInspectResult(options InspectOptions, selection sourceSelection) error {
|
|
if IsJSONOutput(options.OutputFormat) {
|
|
return WriteJSONEnvelope(options.Stdout, "inspect", true, selection.Warnings, inspectResultFromSelection(selection), nil)
|
|
}
|
|
if err := writeWarnings(options.Stdout, selection.Warnings); err != nil {
|
|
return err
|
|
}
|
|
return writeInspection(options.Stdout, selection)
|
|
}
|
|
|
|
type inspectResult struct {
|
|
PipelineID string `json:"pipeline_id,omitempty"`
|
|
SourceBackend string `json:"source_backend,omitempty"`
|
|
BundleCount int `json:"bundle_count"`
|
|
Bundles []inspectBundleResult `json:"bundles"`
|
|
}
|
|
|
|
type inspectBundleResult struct {
|
|
Path string `json:"path"`
|
|
ID string `json:"id"`
|
|
Created string `json:"created"`
|
|
Digest string `json:"digest"`
|
|
FileCount int `json:"file_count"`
|
|
TotalSize int64 `json:"total_size"`
|
|
Files []inspectFileResult `json:"files"`
|
|
}
|
|
|
|
type inspectFileResult struct {
|
|
Path string `json:"path"`
|
|
SHA256 string `json:"sha256"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
func inspectResultFromSelection(selection sourceSelection) inspectResult {
|
|
result := inspectResult{
|
|
PipelineID: selection.PipelineID,
|
|
SourceBackend: selection.SourceBackend,
|
|
BundleCount: len(selection.Bundles),
|
|
Bundles: make([]inspectBundleResult, 0, len(selection.Bundles)),
|
|
}
|
|
for _, sourceBundle := range selection.Bundles {
|
|
bundleResult := inspectBundleResult{
|
|
Path: storage.DisplayPath(sourceBundle.RootRelativePath),
|
|
ID: sourceBundle.Manifest.ID,
|
|
Created: sourceBundle.Manifest.Created.Format("2006-01-02T15:04:05Z07:00"),
|
|
Digest: sourceBundle.Manifest.Digest,
|
|
FileCount: len(sourceBundle.Manifest.Files),
|
|
Files: make([]inspectFileResult, 0, len(sourceBundle.Manifest.Files)),
|
|
}
|
|
for _, file := range sourceBundle.Manifest.Files {
|
|
bundleResult.TotalSize += file.Size
|
|
bundleResult.Files = append(bundleResult.Files, inspectFileResult{
|
|
Path: file.Path,
|
|
SHA256: file.SHA256,
|
|
Size: file.Size,
|
|
})
|
|
}
|
|
result.Bundles = append(result.Bundles, bundleResult)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func writeInspection(w io.Writer, selection sourceSelection) error {
|
|
if w == nil {
|
|
return nil
|
|
}
|
|
if selection.ConfigMode {
|
|
if _, err := fmt.Fprintf(w, "Pipeline: %s\nSource: %s\n", selection.PipelineID, selection.SourceBackend); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if _, err := fmt.Fprintf(w, "Bundles: %d\n", len(selection.Bundles)); err != nil {
|
|
return err
|
|
}
|
|
for _, sourceBundle := range selection.Bundles {
|
|
if _, err := fmt.Fprintf(
|
|
w,
|
|
"- path=%s id=%s created=%s digest=%s files=%d\n",
|
|
storage.DisplayPath(sourceBundle.RootRelativePath),
|
|
sourceBundle.Manifest.ID,
|
|
sourceBundle.Manifest.Created.Format("2006-01-02T15:04:05Z07:00"),
|
|
sourceBundle.Manifest.Digest,
|
|
len(sourceBundle.Manifest.Files),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
for _, file := range sourceBundle.Manifest.Files {
|
|
if _, err := fmt.Fprintf(w, " - %s size=%d sha256=%s\n", file.Path, file.Size, file.SHA256); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|