github.com/jaypipes/ghw@v0.21.1/host.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package ghw
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/jaypipes/ghw/pkg/context"
    13  	"github.com/jaypipes/ghw/pkg/usb"
    14  
    15  	"github.com/jaypipes/ghw/pkg/accelerator"
    16  	"github.com/jaypipes/ghw/pkg/baseboard"
    17  	"github.com/jaypipes/ghw/pkg/bios"
    18  	"github.com/jaypipes/ghw/pkg/block"
    19  	"github.com/jaypipes/ghw/pkg/chassis"
    20  	"github.com/jaypipes/ghw/pkg/cpu"
    21  	"github.com/jaypipes/ghw/pkg/gpu"
    22  	"github.com/jaypipes/ghw/pkg/marshal"
    23  	"github.com/jaypipes/ghw/pkg/memory"
    24  	"github.com/jaypipes/ghw/pkg/net"
    25  	"github.com/jaypipes/ghw/pkg/pci"
    26  	"github.com/jaypipes/ghw/pkg/product"
    27  	"github.com/jaypipes/ghw/pkg/topology"
    28  )
    29  
    30  // HostInfo is a wrapper struct containing information about the host system's
    31  // memory, block storage, CPU, etc
    32  type HostInfo struct {
    33  	ctx         *context.Context
    34  	Memory      *memory.Info      `json:"memory"`
    35  	Block       *block.Info       `json:"block"`
    36  	CPU         *cpu.Info         `json:"cpu"`
    37  	Topology    *topology.Info    `json:"topology"`
    38  	Network     *net.Info         `json:"network"`
    39  	GPU         *gpu.Info         `json:"gpu"`
    40  	Accelerator *accelerator.Info `json:"accelerator"`
    41  	Chassis     *chassis.Info     `json:"chassis"`
    42  	BIOS        *bios.Info        `json:"bios"`
    43  	Baseboard   *baseboard.Info   `json:"baseboard"`
    44  	Product     *product.Info     `json:"product"`
    45  	PCI         *pci.Info         `json:"pci"`
    46  	USB         *usb.Info         `json:"usb"`
    47  }
    48  
    49  // Host returns a pointer to a HostInfo struct that contains fields with
    50  // information about the host system's CPU, memory, network devices, etc
    51  func Host(opts ...*WithOption) (*HostInfo, error) {
    52  	ctx := context.New(opts...)
    53  
    54  	memInfo, err := memory.New(opts...)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	blockInfo, err := block.New(opts...)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	cpuInfo, err := cpu.New(opts...)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	topologyInfo, err := topology.New(opts...)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	netInfo, err := net.New(opts...)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	gpuInfo, err := gpu.New(opts...)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	acceleratorInfo, err := accelerator.New(opts...)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	chassisInfo, err := chassis.New(opts...)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	biosInfo, err := bios.New(opts...)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	baseboardInfo, err := baseboard.New(opts...)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	productInfo, err := product.New(opts...)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	pciInfo, err := pci.New(opts...)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	usbInfo, err := usb.New(opts...)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	return &HostInfo{
   108  		ctx:         ctx,
   109  		CPU:         cpuInfo,
   110  		Memory:      memInfo,
   111  		Block:       blockInfo,
   112  		Topology:    topologyInfo,
   113  		Network:     netInfo,
   114  		GPU:         gpuInfo,
   115  		Accelerator: acceleratorInfo,
   116  		Chassis:     chassisInfo,
   117  		BIOS:        biosInfo,
   118  		Baseboard:   baseboardInfo,
   119  		Product:     productInfo,
   120  		PCI:         pciInfo,
   121  		USB:         usbInfo,
   122  	}, nil
   123  }
   124  
   125  // String returns a newline-separated output of the HostInfo's component
   126  // structs' String-ified output
   127  func (info *HostInfo) String() string {
   128  	return fmt.Sprintf(
   129  		"%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
   130  		info.Block.String(),
   131  		info.CPU.String(),
   132  		info.GPU.String(),
   133  		info.Accelerator.String(),
   134  		info.Memory.String(),
   135  		info.Network.String(),
   136  		info.Topology.String(),
   137  		info.Chassis.String(),
   138  		info.BIOS.String(),
   139  		info.Baseboard.String(),
   140  		info.Product.String(),
   141  		info.PCI.String(),
   142  		info.USB.String(),
   143  	)
   144  }
   145  
   146  // YAMLString returns a string with the host information formatted as YAML
   147  // under a top-level "host:" key
   148  func (i *HostInfo) YAMLString() string {
   149  	return marshal.SafeYAML(i.ctx, i)
   150  }
   151  
   152  // JSONString returns a string with the host information formatted as JSON
   153  // under a top-level "host:" key
   154  func (i *HostInfo) JSONString(indent bool) string {
   155  	return marshal.SafeJSON(i.ctx, i, indent)
   156  }