Skip to content

Commit

Permalink
Add HasNvidiaSmi
Browse files Browse the repository at this point in the history
  • Loading branch information
fffaraz committed Jun 8, 2022
1 parent a16ce7a commit b3e09cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cmd/nvidia-smi-json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ func main() {
flag.BoolVar(&query, "q", false, "run nvidia-smi -q")
flag.Parse()
if query {
fmt.Println(nvidiasmijson.XmlToJson(nvidiasmijson.RunNvidiaSmi()))
if nvidiasmijson.HasNvidiaSmi() {
fmt.Println(nvidiasmijson.XmlToJson(nvidiasmijson.RunNvidiaSmi()))
} else {
fmt.Fprintln(os.Stderr, "nvidia-smi not found")
}
return
}
input, err := ioutil.ReadAll(os.Stdin)
Expand Down
6 changes: 6 additions & 0 deletions nvidiasmijson.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func XmlToObject(input []byte) *NvidiaSmiLog {
var data NvidiaSmiLog
if err := xml.Unmarshal(input, &data); nil != err {
fmt.Fprintln(os.Stderr, "Error unmarshalling from XML:", err)
return nil
}
return &data
}
Expand All @@ -27,6 +28,11 @@ func XmlToJson(input []byte) string {
return string(output)
}

func HasNvidiaSmi() bool {
_, err := exec.LookPath("nvidia-smi")
return err == nil
}

func RunNvidiaSmi() []byte {
cmd := "nvidia-smi"
if runtime.GOOS == "windows" {
Expand Down

0 comments on commit b3e09cd

Please sign in to comment.