Skip to content
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

Closed
jaypipes opened this issue Oct 19, 2018 · 5 comments
Closed

Serialize information to JSON and YAML #78

jaypipes opened this issue Oct 19, 2018 · 5 comments
Assignees
Milestone

Comments

@jaypipes
Copy link
Owner

Add ability to serialize any collected information from the ghw *Info structs to JSON and YAML.

@jaypipes jaypipes added this to the 1.0 milestone Oct 19, 2018
@ghost
Copy link

ghost commented Oct 25, 2018

That would be really useful !

@yrobla
Copy link

yrobla commented Jan 28, 2019

Agree, i'd like to have that functionality, glad to help if needed!

@jaypipes jaypipes modified the milestones: 1.0, 0.3 Jan 28, 2019
@jaypipes jaypipes self-assigned this Jan 28, 2019
@jaypipes
Copy link
Owner Author

@gedw99 @yrobla OK folks, I'm just wrapping up this work now. Here's what it looks like in practice:

[jaypipes@uberbox ghw]$ go run cmd/ghwc/main.go -f yaml memory 2>/dev/null
memory:
  total_physical_bytes: 25263415296
  total_usable_bytes: 25263415296
  supported_page_sizes:
  - 1073741824
  - 2097152
[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
    ]
  }
}

I'll be pushing a pull request shortly for your review. Thanks for your patience!
-jay

jaypipes added a commit that referenced this issue Jan 30, 2019
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
@jaypipes
Copy link
Owner Author

@gedw99 if you have a chance, please test the code in PR #112 and let me know if that meets your needs and expectations.

Thanks!
-jay

jaypipes added a commit that referenced this issue Jan 30, 2019
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
@jaypipes
Copy link
Owner Author

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.

jaypipes added a commit that referenced this issue Feb 16, 2019
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants