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

     1  // Use and distribution licensed under the Apache license version 2.
     2  //
     3  // See the COPYING file in the root project directory for full text.
     4  //
     5  
     6  package usb
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  
    12  	"github.com/jaypipes/ghw/pkg/context"
    13  	"github.com/jaypipes/ghw/pkg/marshal"
    14  	"github.com/jaypipes/ghw/pkg/option"
    15  )
    16  
    17  type Device struct {
    18  	Driver     string `json:"driver"`
    19  	Type       string `json:"type"`
    20  	VendorID   string `json:"vendor_id"`
    21  	ProductID  string `json:"product_id"`
    22  	Product    string `json:"product"`
    23  	RevisionID string `json:"revision_id"`
    24  	Interface  string `json:"interface"`
    25  }
    26  
    27  func (d Device) String() string {
    28  	kvs := []struct {
    29  		name  string
    30  		value string
    31  	}{
    32  		{"driver", d.Driver},
    33  		{"type", d.Type},
    34  		{"vendorID", d.VendorID},
    35  		{"productID", d.ProductID},
    36  		{"product", d.Product},
    37  		{"revisionID", d.RevisionID},
    38  		{"interface", d.Interface},
    39  	}
    40  
    41  	var str strings.Builder
    42  
    43  	i := 0
    44  	for _, s := range kvs {
    45  		k := s.name
    46  		v := s.value
    47  
    48  		if v == "" {
    49  			continue
    50  		}
    51  		needsQuotationMarks := strings.ContainsAny(v, " \t")
    52  
    53  		if i > 0 {
    54  			str.WriteString(" ")
    55  		}
    56  		i++
    57  		str.WriteString(k)
    58  		str.WriteString("=")
    59  		if needsQuotationMarks {
    60  			str.WriteString("\"")
    61  		}
    62  		str.WriteString(v)
    63  		if needsQuotationMarks {
    64  			str.WriteString("\"")
    65  		}
    66  
    67  	}
    68  
    69  	return str.String()
    70  }
    71  
    72  // Info describes all network interface controllers (NICs) in the host system.
    73  type Info struct {
    74  	ctx     *context.Context
    75  	Devices []*Device `json:"devices"`
    76  }
    77  
    78  // String returns a short string with information about the networking on the
    79  // host system.
    80  func (i *Info) String() string {
    81  	return fmt.Sprintf(
    82  		"USB (%d USBs)",
    83  		len(i.Devices),
    84  	)
    85  }
    86  
    87  // New returns a pointer to an Info struct that contains information about the
    88  // network interface controllers (NICs) on the host system
    89  func New(opts ...*option.Option) (*Info, error) {
    90  	ctx := context.New(opts...)
    91  	info := &Info{ctx: ctx}
    92  	if err := ctx.Do(info.load); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return info, nil
    97  }
    98  
    99  // simple private struct used to encapsulate usb information in a
   100  // top-level "usb" YAML/JSON map/object key
   101  type usbPrinter struct {
   102  	Info *Info `json:"usb"`
   103  }
   104  
   105  // YAMLString returns a string with the net information formatted as YAML
   106  // under a top-level "net:" key
   107  func (i *Info) YAMLString() string {
   108  	return marshal.SafeYAML(i.ctx, usbPrinter{i})
   109  }
   110  
   111  // JSONString returns a string with the net information formatted as JSON
   112  // under a top-level "net:" key
   113  func (i *Info) JSONString(indent bool) string {
   114  	return marshal.SafeJSON(i.ctx, usbPrinter{i}, indent)
   115  }