package app import ( "context" "fmt" "io" "gitea.maximumdirect.net/eric/distributor/internal/bundle" "gitea.maximumdirect.net/eric/distributor/internal/storage" ) type InspectOptions struct { Path string Stdout io.Writer OutputFormat OutputFormat } func Inspect(ctx context.Context, options InspectOptions) error { if err := ValidateOutputFormat(options.OutputFormat); err != nil { return err } if options.Path == "" { return fmt.Errorf("inspect command requires a path") } backend, err := newBackendFactory().openLocalPath(ctx, options.Path) if err != nil { return err } bundles, err := bundle.Discover(ctx, backend, "") if err != nil { return err } if IsJSONOutput(options.OutputFormat) { return WriteJSONEnvelope(options.Stdout, "inspect", true, nil, inspectResultFromBundles(bundles), nil) } return writeInspection(options.Stdout, bundles) } type inspectResult struct { 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 inspectResultFromBundles(bundles []bundle.Bundle) inspectResult { result := inspectResult{ BundleCount: len(bundles), Bundles: make([]inspectBundleResult, 0, len(bundles)), } for _, sourceBundle := range 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, bundles []bundle.Bundle) error { if w == nil { return nil } if _, err := fmt.Fprintf(w, "Bundles: %d\n", len(bundles)); err != nil { return err } for _, sourceBundle := range 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 }