-
Notifications
You must be signed in to change notification settings - Fork 13
Added command for image inspect #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,118 @@ | ||||||||||||||||||||||
| package main | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import ( | ||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||
| "io" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| "github.com/open-edge-platform/os-image-composer/internal/image/imageinspect" | ||||||||||||||||||||||
| "github.com/open-edge-platform/os-image-composer/internal/utils/logger" | ||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||
| "gopkg.in/yaml.v3" | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // cmd needs only these two methods. | ||||||||||||||||||||||
| type inspector interface { | ||||||||||||||||||||||
| Inspect(imagePath string) (*imageinspect.ImageSummary, error) | ||||||||||||||||||||||
| DisplaySummary(w io.Writer, summary *imageinspect.ImageSummary) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Allow tests to inject a fake inspector. | ||||||||||||||||||||||
| var newInspector = func() inspector { | ||||||||||||||||||||||
| return imageinspect.NewDiskfsInspector() // returns *DiskfsInspector which satisfies inspector | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Output format command flags | ||||||||||||||||||||||
| var ( | ||||||||||||||||||||||
| outputFormat string = "text" // Output format for the inspection results | ||||||||||||||||||||||
| prettyJSON bool = true // Pretty-print JSON output | ||||||||||||||||||||||
|
magerstam marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // createInspectCommand creates the inspect subcommand | ||||||||||||||||||||||
| func createInspectCommand() *cobra.Command { | ||||||||||||||||||||||
| validateCmd := &cobra.Command{ | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. variable name is confusing:
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will address, thank you! |
||||||||||||||||||||||
| Use: "inspect [flags] IMAGE_FILE", | ||||||||||||||||||||||
| Short: "inspects a RAW image file", | ||||||||||||||||||||||
| Long: `Inspect performs a deep inspection of a generated | ||||||||||||||||||||||
| RAW image and provides useful details of the image such as | ||||||||||||||||||||||
| partition table layout, filesystem type, bootloader type and | ||||||||||||||||||||||
| configuration and overall SBOM details if available.`, | ||||||||||||||||||||||
| Args: cobra.ExactArgs(1), | ||||||||||||||||||||||
| PreRunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||||
| switch outputFormat { | ||||||||||||||||||||||
| case "text", "json", "yaml": | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| default: | ||||||||||||||||||||||
| return fmt.Errorf("unsupported --format %q (supported: text, json, yaml)", outputFormat) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| RunE: executeInspect, | ||||||||||||||||||||||
| ValidArgsFunction: templateFileCompletion, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Add flags | ||||||||||||||||||||||
| validateCmd.Flags().StringVar(&outputFormat, "format", "text", | ||||||||||||||||||||||
| "Specify the output format for the inspection results") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| validateCmd.Flags().BoolVar(&prettyJSON, "pretty", false, | ||||||||||||||||||||||
| "Pretty-print JSON output (only for --format json)") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return validateCmd | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // executeInspect handles the inspect command execution logic | ||||||||||||||||||||||
| func executeInspect(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||||
| log := logger.Logger() | ||||||||||||||||||||||
| imageFile := args[0] | ||||||||||||||||||||||
| log.Infof("Inspecting image file: %s", imageFile) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| inspector := newInspector() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| inspectionResults, err := inspector.Inspect(imageFile) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return fmt.Errorf("image inspection failed: %v", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if err := writeInspectionResult(cmd, inspectionResults, outputFormat, prettyJSON); err != nil { | ||||||||||||||||||||||
| return err | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func writeInspectionResult(cmd *cobra.Command, summary *imageinspect.ImageSummary, format string, pretty bool) error { | ||||||||||||||||||||||
| out := cmd.OutOrStdout() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| switch format { | ||||||||||||||||||||||
| case "text": | ||||||||||||||||||||||
| imageinspect.PrintSummary(out, summary) | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| case "json": | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
| var ( | ||||||||||||||||||||||
| b []byte | ||||||||||||||||||||||
| err error | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| if pretty { | ||||||||||||||||||||||
| b, err = json.MarshalIndent(summary, "", " ") | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| b, err = json.Marshal(summary) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return fmt.Errorf("marshal json: %w", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| _, _ = fmt.Fprintln(out, string(b)) | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. erros ignored |
||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| case "yaml": | ||||||||||||||||||||||
| b, err := yaml.Marshal(summary) | ||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| return fmt.Errorf("marshal yaml: %w", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| _, _ = fmt.Fprintln(out, string(b)) | ||||||||||||||||||||||
| return nil | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| default: | ||||||||||||||||||||||
| return fmt.Errorf("unsupported output format: %s", format) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.