-
Notifications
You must be signed in to change notification settings - Fork 181
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
Serialize information to JSON and YAML #78
Comments
That would be really useful ! |
Agree, i'd like to have that functionality, glad to help if needed! |
@gedw99 @yrobla OK folks, I'm just wrapping up this work now. Here's what it looks like in practice:
I'll be pushing a pull request shortly for your review. Thanks for your patience! |
Adds support for serializing the primary XXXInfo structs in `ghw` as JSON and YAML. Each `ghw.XXXInfo` struct now has a `YAMLString()` and `JSONString()` method that returns a string with the struct's information serialized to that format. The `ghwc` client has been enhanced with a `--format` CLI option that allows users to print the hardware information in human-readable (the default), JSON or YAML format: ``` [jaypipes@uberbox ghw]$ go run cmd/ghwc/main.go -f yaml memory 2>/dev/null memory: supported_page_sizes: - 1073741824 - 2097152 total_physical_bytes: 25263415296 total_usable_bytes: 25263415296 [jaypipes@uberbox ghw]$ go run cmd/ghwc/main.go -f json memory 2>/dev/null {"memory":{"total_physical_bytes":25263415296,"total_usable_bytes":25263415296,"supported_page_sizes":[1073741824,2097152]}} ``` The `--format json` CLI option can be paired with a `--pretty` CLI option to provide indented output: ``` [jaypipes@uberbox ghw]$ go run cmd/ghwc/main.go -f json --pretty memory 2>/dev/null { "memory": { "total_physical_bytes": 25263415296, "total_usable_bytes": 25263415296, "supported_page_sizes": [ 1073741824, 2097152 ] } } ``` Issue #78
Before this patch, we were outputting the JSON strings for different elements one after another. So, when executing `ghwc -f json` without specifying a particular type of information to query (e.g. memory), we were outputting invalid JSON, like this: ``` { "memory": ... } { "block": ... } ``` This patch adds a YAMLString() and JSONString() method to the HostInfo struct and modifies the `ghwc` command line tool to use the String(), JSONString() or YAMLString() methods when outputting information about the host (and all the subcomponents). So now the JSON emitted is valid: ``` { "memory": ..., "block": ..., ... } Issue #78
OK, so 95% of this has now been completed with the merge of PR #112. Remaining items are around the PCI device information serialization (which will be dependent on the pcidb library gaining serialization support first, see this issue there: jaypipes/pcidb#16) along with adding some tests. |
With this patch, PCI device information (currently only on the GPU/GraphicsCard struct) is serialized to JSON and YAML. In order to prevent outputting all of the nested PCIDB structs, we required a custom MarshalJSON marshaler for the PCIDevice struct pointer that only outputs the ID and name of the vendor, product, class and subsystem instead of all the vendor's subproducts, etc. ``` [jaypipes@uberbox ghw]$ go run cmd/ghwc/main.go gpu -fyaml gpu: cards: - address: "0000:03:00.0" index: 0 pci: address: "0000:03:00.0" class: id: "03" name: Display controller product: id: 1c82 name: GP107 [GeForce GTX 1050 Ti] programming_interface: id: "00" name: VGA controller subclass: id: "00" name: VGA compatible controller subsystem: id: "8613" name: unknown vendor: id: 10de name: NVIDIA Corporation ``` Issue #78
Add ability to serialize any collected information from the
ghw
*Info
structs to JSON and YAML.The text was updated successfully, but these errors were encountered: