diff --git a/cmd/nvidia-smi-json.go b/cmd/nvidia-smi-json.go index a01e08f..c21f3f9 100644 --- a/cmd/nvidia-smi-json.go +++ b/cmd/nvidia-smi-json.go @@ -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) diff --git a/nvidiasmijson.go b/nvidiasmijson.go index f1adf63..184b277 100644 --- a/nvidiasmijson.go +++ b/nvidiasmijson.go @@ -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 } @@ -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" {