diff --git a/go.mod b/go.mod index 4212ad9..d7330cc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/bluet/syspkg -go 1.23 +go 1.23.0 require github.com/urfave/cli/v2 v2.27.7 // direct diff --git a/manager/packageinfo.go b/manager/packageinfo.go index 1d60117..4c2eceb 100644 --- a/manager/packageinfo.go +++ b/manager/packageinfo.go @@ -52,38 +52,38 @@ const ( // unknown: Version="", NewVersion may contain repo_version type PackageInfo struct { // Name is the package name. - Name string + Name string `json:"name"` // Version is the currently installed version of the package. // Empty if package is not installed (Status=available). // Contains removed version for Delete operations. - Version string + Version string `json:"version,omitempty"` // NewVersion is the latest available version from repositories. // Used for available versions in Find operations and upgrade targets. // Empty for ListInstalled operations. // Same as Version for Install operations. - NewVersion string + NewVersion string `json:"new_version,omitempty"` // Status indicates the current PackageStatus of the package. // See PackageStatus constants for detailed descriptions. - Status PackageStatus + Status PackageStatus `json:"status"` // Category is the category/section the package belongs to. // Examples: "utilities", "development", "web", "jammy", "main" // May represent repository sections or package categories depending on package manager. - Category string + Category string `json:"category,omitempty"` // Arch is the architecture the package is built for. // Examples: "amd64", "arm64", "i386", "all" // Empty if architecture is not specified or not applicable. - Arch string + Arch string `json:"arch,omitempty"` // PackageManager is the name of the package manager used to manage this package. // Examples: "apt", "yum", "dnf", "snap", "flatpak" - PackageManager string + PackageManager string `json:"package_manager"` // AdditionalData is a map of key-value pairs for additional package-specific metadata. // Used for package manager specific information that doesn't fit standard fields. - AdditionalData map[string]string + AdditionalData map[string]string `json:"additional_data,omitempty"` } diff --git a/manager/packageinfo_test.go b/manager/packageinfo_test.go new file mode 100644 index 0000000..b80009d --- /dev/null +++ b/manager/packageinfo_test.go @@ -0,0 +1,41 @@ +package manager + +import ( + "encoding/json" + "testing" +) + +func TestPackageInfoJsonTag(t *testing.T) { + // Populate all fields to ensure they are present in the JSON output. + testPackage := PackageInfo{ + Name: "test-pkg", + Version: "1.0.0", + NewVersion: "1.1.0", + Status: PackageStatusUpgradable, + Category: "test-cat", + Arch: "amd64", + PackageManager: "test-pm", + AdditionalData: map[string]string{"foo": "bar"}, + } + + jsonAsByte, err := json.Marshal(testPackage) + if err != nil { + t.Fatalf("json.Marshal failed: %v", err) + } + + var data map[string]interface{} + if err := json.Unmarshal(jsonAsByte, &data); err != nil { + t.Fatalf("json.Unmarshal failed: %v", err) + } + + expectedTags := []string{"name", "version", "new_version", "status", "category", "arch", "package_manager", "additional_data"} + for _, tag := range expectedTags { + if _, ok := data[tag]; !ok { + t.Errorf("Expected tag '%s' not found in JSON output: %s", tag, string(jsonAsByte)) + } + } + + if len(data) != len(expectedTags) { + t.Errorf("Expected %d tags, but got %d. JSON: %s", len(expectedTags), len(data), string(jsonAsByte)) + } +}