Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions cmd/os-image-composer/inspect_cmd.go
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)
Comment thread
magerstam marked this conversation as resolved.
}

// 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
Comment thread
magerstam marked this conversation as resolved.
Outdated
)

// createInspectCommand creates the inspect subcommand
func createInspectCommand() *cobra.Command {
validateCmd := &cobra.Command{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable name is confusing:

Suggested change
validateCmd := &cobra.Command{
inspectCmd := &cobra.Command{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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":
Copy link
Copy Markdown
Contributor

@arodage arodage Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case "json":
case "json":
enc := json.NewEncoder(out)
if pretty {
enc.SetIndent("", " ")
}
if err := enc.Encode(summary); err != nil {
return fmt.Errorf("encode json: %w", err)
}
return nil

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))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)
}
}
Loading
Loading