|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/open-edge-platform/os-image-composer/internal/image/imageinspect" |
| 9 | + "github.com/open-edge-platform/os-image-composer/internal/utils/logger" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +// cmd needs only these two methods. |
| 15 | +type inspector interface { |
| 16 | + Inspect(imagePath string) (*imageinspect.ImageSummary, error) |
| 17 | + DisplaySummary(w io.Writer, summary *imageinspect.ImageSummary) |
| 18 | +} |
| 19 | + |
| 20 | +// Allow tests to inject a fake inspector. |
| 21 | +var newInspector = func() inspector { |
| 22 | + return imageinspect.NewDiskfsInspector() // returns *DiskfsInspector which satisfies inspector |
| 23 | +} |
| 24 | + |
| 25 | +// Output format command flags |
| 26 | +var ( |
| 27 | + outputFormat string = "text" // Output format for the inspection results |
| 28 | + prettyJSON bool = false // Pretty-print JSON output |
| 29 | +) |
| 30 | + |
| 31 | +// createInspectCommand creates the inspect subcommand |
| 32 | +func createInspectCommand() *cobra.Command { |
| 33 | + validateCmd := &cobra.Command{ |
| 34 | + Use: "inspect [flags] IMAGE_FILE", |
| 35 | + Short: "inspects a RAW image file", |
| 36 | + Long: `Inspect performs a deep inspection of a generated |
| 37 | + RAW image and provides useful details of the image such as |
| 38 | + partition table layout, filesystem type, bootloader type and |
| 39 | + configuration and overall SBOM details if available.`, |
| 40 | + Args: cobra.ExactArgs(1), |
| 41 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + switch outputFormat { |
| 43 | + case "text", "json", "yaml": |
| 44 | + return nil |
| 45 | + default: |
| 46 | + return fmt.Errorf("unsupported --format %q (supported: text, json, yaml)", outputFormat) |
| 47 | + } |
| 48 | + }, |
| 49 | + RunE: executeInspect, |
| 50 | + ValidArgsFunction: templateFileCompletion, |
| 51 | + } |
| 52 | + |
| 53 | + // Add flags |
| 54 | + validateCmd.Flags().StringVar(&outputFormat, "format", "text", |
| 55 | + "Specify the output format for the inspection results") |
| 56 | + |
| 57 | + validateCmd.Flags().BoolVar(&prettyJSON, "pretty", false, |
| 58 | + "Pretty-print JSON output (only for --format json)") |
| 59 | + |
| 60 | + return validateCmd |
| 61 | +} |
| 62 | + |
| 63 | +// executeInspect handles the inspect command execution logic |
| 64 | +func executeInspect(cmd *cobra.Command, args []string) error { |
| 65 | + log := logger.Logger() |
| 66 | + imageFile := args[0] |
| 67 | + log.Infof("Inspecting image file: %s", imageFile) |
| 68 | + |
| 69 | + inspector := newInspector() |
| 70 | + |
| 71 | + inspectionResults, err := inspector.Inspect(imageFile) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("image inspection failed: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if err := writeInspectionResult(cmd, inspectionResults, outputFormat, prettyJSON); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func writeInspectionResult(cmd *cobra.Command, summary *imageinspect.ImageSummary, format string, pretty bool) error { |
| 84 | + out := cmd.OutOrStdout() |
| 85 | + |
| 86 | + switch format { |
| 87 | + case "text": |
| 88 | + imageinspect.PrintSummary(out, summary) |
| 89 | + return nil |
| 90 | + |
| 91 | + case "json": |
| 92 | + var ( |
| 93 | + b []byte |
| 94 | + err error |
| 95 | + ) |
| 96 | + if pretty { |
| 97 | + b, err = json.MarshalIndent(summary, "", " ") |
| 98 | + } else { |
| 99 | + b, err = json.Marshal(summary) |
| 100 | + } |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("marshal json: %w", err) |
| 103 | + } |
| 104 | + _, _ = fmt.Fprintln(out, string(b)) |
| 105 | + return nil |
| 106 | + |
| 107 | + case "yaml": |
| 108 | + b, err := yaml.Marshal(summary) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("marshal yaml: %w", err) |
| 111 | + } |
| 112 | + _, _ = fmt.Fprintln(out, string(b)) |
| 113 | + return nil |
| 114 | + |
| 115 | + default: |
| 116 | + return fmt.Errorf("unsupported output format: %s", format) |
| 117 | + } |
| 118 | +} |
0 commit comments