blob: 423f91fbfb5d2af16469b9cd8959c5a7430c1ec8 [file] [log] [blame]
package output
import (
"encoding/json"
"fmt"
"cos.googlesource.com/cos/tools/src/cmd/cos_image_analyzer/internal/binary"
"cos.googlesource.com/cos/tools/src/cmd/cos_image_analyzer/internal/input"
"cos.googlesource.com/cos/tools/src/cmd/cos_image_analyzer/internal/utilities"
)
// ImageDiff stores all of the differences between the two images
type ImageDiff struct {
BinaryDiff *binary.Differences
}
// Formater is a ImageDiff function that outputs the image differences based on the "-output" flag.
// Either to the terminal (default) or to a stored json object
// Input:
// (*FlagInfo) flagInfo - A struct that holds input preference from the user
// Output:
// ([]string) diffstrings/jsonObjectStr - Based on "-output" flag, either formated string
// for the terminal or a string json object
func (imageDiff *ImageDiff) Formater(flagInfo *input.FlagInfo) (string, error) {
if flagInfo.OutputSelected == "terminal" {
binaryStrings := ""
binaryFunctions := map[string]func() string{
"Version": imageDiff.BinaryDiff.FormatVersionDiff,
"BuildID": imageDiff.BinaryDiff.FormatBuildIDDiff,
"Rootfs": imageDiff.BinaryDiff.FormatRootfsDiff,
"Stateful-partition": imageDiff.BinaryDiff.FormatStatefulDiff,
"OS-config": imageDiff.BinaryDiff.FormatOSConfigDiff,
"Partition-structure": imageDiff.BinaryDiff.FormatPartitionStructureDiff,
}
for diff := range binaryFunctions {
if utilities.InArray(diff, flagInfo.BinaryTypesSelected) {
binaryStrings += binaryFunctions[diff]()
}
}
if len(binaryStrings) > 0 {
if flagInfo.Image2 == "" {
binaryStrings = "================= Binary Info =================\n" + binaryStrings
} else {
binaryStrings = "================= Binary Differences =================\n" + binaryStrings
}
}
diffStrings := binaryStrings
return diffStrings, nil
}
jsonObjectBytes, err := json.Marshal(imageDiff)
if err != nil {
return "", fmt.Errorf("failed to json marshal the image difference struct: %v", err)
}
jsonObjectStr := string(jsonObjectBytes[:])
return jsonObjectStr, nil
}
// Print is a ImageDiff method that prints out all image differences
func (imageDiff *ImageDiff) Print(differences string) {
fmt.Print(differences)
}