Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 30, 2025
1 parent 8d6673a commit 4560b65
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 169 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/charmbracelet/bubbletea v1.2.4
github.com/charmbracelet/lipgloss v1.0.0
github.com/chigopher/pathlib v0.19.1
github.com/mistakenelf/teacup v0.4.1
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -15,6 +16,7 @@ require (
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -23,10 +25,12 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/afero v1.4.0 // indirect
golang.org/x/image v0.11.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/text v0.12.0 // indirect
)
40 changes: 38 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand All @@ -35,18 +37,24 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mistakenelf/teacup v0.4.1 h1:QPNyIqrNKeizeGZc9cE6n+nAsIBu52oUf3bCkfGyBwk=
github.com/mistakenelf/teacup v0.4.1/go.mod h1:8v/aIRCfrae6Uit1WFPHv0xzwi1XELZkAHiTybNSZTk=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand All @@ -58,22 +66,50 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
122 changes: 10 additions & 112 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"

"github.com/LandonTClipp/pciex/models"
"github.com/LandonTClipp/pciex/pcie"
tea "github.com/charmbracelet/bubbletea"
"github.com/chigopher/pathlib"
"gopkg.in/yaml.v2"
)

type Slot struct {
Expand All @@ -38,97 +36,8 @@ func NewSlotFromString(s string) Slot {
}
}

type PCIElement struct {
Slot Slot
Class string
Vendor string
Device string
}

type AdditionalDetails struct {
NumaNode *int
LocalCPUList *string
}

func NewAdditionalDetailsFromSysfs(sysfsPath *pathlib.Path) (AdditionalDetails, error) {
d := AdditionalDetails{}
numa := sysfsPath.Join("numa_node")
b, err := numa.ReadFile()
if err != nil {
return d, fmt.Errorf("reading numa_node: %w", err)
}
numaInt, err := strconv.Atoi(strings.TrimSuffix(string(b), "\n"))
if err != nil {
return d, fmt.Errorf("parsing numa_node into int: %w", err)
}
d.NumaNode = &numaInt

localCPUList := sysfsPath.Join("local_cpulist")
cpuListBytes, err := localCPUList.ReadFile()
if err != nil {
return d, fmt.Errorf("reading local_cpulist: %w", err)
}
asStr := string(cpuListBytes)
d.LocalCPUList = &asStr

return d, nil
}

type Details struct {
AdditionalDetails // Details not provided by lshw that we need to scrape ourselves
Id string
Class string
Claimed bool
Handle string
Description string
Product string
Vendor string
Physid string
Businfo string
Version string
Width int
Clock int
Serial string
Slot string
Units string
Size int
Configuration map[string]any
Capabilities map[string]any
}

func (d *Details) GetAdditionalDetails() error {
addressSplit := strings.Split(d.Businfo, "@")
if len(addressSplit) != 2 {
return nil
}
address := addressSplit[1]
sysfsPath := pathlib.NewPath("/sys/bus/pci/devices/" + address)
details, err := NewAdditionalDetailsFromSysfs(sysfsPath)
if err != nil {
return err
}
d.AdditionalDetails = details
return nil
}

func (d Details) String() string {
var s string
s += d.Class + " | "
switch d.Class {
case "bridge":
s += d.Handle
case "bus":
s += d.Description
case "display", "memory", "communication", "generic", "network":
s += d.Product
default:
s += d.Description
}
return s
}

type LshwElem struct {
Details
pcie.Details
Children []LshwElem
}

Expand All @@ -140,11 +49,7 @@ func buildPCIETreeHelper(parent *models.Node, children []LshwElem) error {
if err := child.Details.GetAdditionalDetails(); err != nil {
return fmt.Errorf("getting additional details after json unmarshal: %w", err)
}
out, err := yaml.Marshal(child.Details)
if err != nil {
return fmt.Errorf("unmarshalling yaml: %w", err)
}
childNode := parent.AddChild(child.Details.String(), string(out))
childNode := parent.AddChild(child.Details.String(), child.Details)
if err := buildPCIETreeHelper(childNode, child.Children); err != nil {
return err
}
Expand All @@ -169,7 +74,7 @@ func buildPCIETree(tree *models.TreeModel) error {
}
lshw = lshwArray[0]
}
tree.Root = models.NewNode("root", "", nil, tree)
tree.Root = models.NewNode("root", pcie.Details{}, nil, tree)
// Find PCI element
for _, child := range lshw.Children[0].Children {
if !strings.HasPrefix(child.Id, "pci") {
Expand All @@ -178,11 +83,7 @@ func buildPCIETree(tree *models.TreeModel) error {
if err := child.Details.GetAdditionalDetails(); err != nil {
return fmt.Errorf("getting additional details after json unmarshal: %w", err)
}
out, err := yaml.Marshal(child.Details)
if err != nil {
return fmt.Errorf("unmarshalling yaml: %w", err)
}
childNode := tree.Root.AddChild(child.String(), string(out))
childNode := tree.Root.AddChild(child.String(), child.Details)
if err := buildPCIETreeHelper(childNode, child.Children); err != nil {
return err
}
Expand All @@ -191,14 +92,11 @@ func buildPCIETree(tree *models.TreeModel) error {
}

func main() {
rootModel := models.NewRootModel()
root := models.NewNode("Root", "", nil, rootModel.Tree)
rootModel.Tree.Root = root

//if err := buildPCIETree(rootModel.Tree); err != nil {
// fmt.Printf("Error occurred: %v\n", err)
// os.Exit(1)
//}
rootModel, err := models.NewRootModel()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
if err := buildPCIETree(rootModel.Tree); err != nil {
fmt.Printf("Error occurred: %v\n", err)
os.Exit(1)
Expand Down
19 changes: 13 additions & 6 deletions models/nodes.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
package models

import (
"fmt"
"time"

"github.com/LandonTClipp/pciex/pcie"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss/tree"
"gopkg.in/yaml.v2"
)

type Node struct {
Name string
detail string
Detail pcie.Details
Parent *Node
// Idx is the index of Node in Parent's Children field.
Idx int
children Children
model *TreeModel
}

func NewNode(name, detail string, parent *Node, model *TreeModel) *Node {
func NewNode(name string, detail pcie.Details, parent *Node, model *TreeModel) *Node {
return &Node{
Name: name,
detail: detail,
Detail: detail,
Parent: parent,
children: Children{},
model: model,
}
}

func (n *Node) GetDetail() string {
return n.detail
out, err := yaml.Marshal(n.Detail)
if err != nil {
panic(fmt.Errorf("unmarshalling yaml: %w", err))
}
return string(out)
}

func (n *Node) RefreshDetail() tea.Cmd {
Expand All @@ -38,10 +45,10 @@ func (n *Node) RefreshDetail() tea.Cmd {
}
}

func (n *Node) AddChild(name, detail string) *Node {
func (n *Node) AddChild(name string, detail pcie.Details) *Node {
child := &Node{
Name: name,
detail: detail,
Detail: detail,
Parent: n,
model: n.model,
}
Expand Down
Loading

0 comments on commit 4560b65

Please sign in to comment.