From 94efd95eecf3d92a8ecdadf4311dce8f924ec110 Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Fri, 26 Jan 2024 11:40:37 -0500 Subject: [PATCH] feat(psutil): add list commands for cpu gpu and memory (#175) Signed-off-by: Artur Troian --- operator/psutil.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/operator/psutil.go b/operator/psutil.go index 98f10350..2ccaf8da 100644 --- a/operator/psutil.go +++ b/operator/psutil.go @@ -32,6 +32,7 @@ func cmdPsutil() *cobra.Command { }, } + cmd.AddCommand(cmdPsutilList()) cmd.AddCommand(cmdPsutilServe()) return cmd @@ -72,6 +73,44 @@ func cmdPsutilServe() *cobra.Command { return cmd } +func cmdPsutilList() *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "dump node hardware spec into stdout", + Args: cobra.ExactArgs(1), + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + var res interface{} + var err error + switch args[0] { + case "cpu": + res, err = cpu.New() + case "gpu": + res, err = gpu.New() + case "memory": + res, err = memory.New() + default: + return fmt.Errorf("invalid command \"%s\"", args[0]) // nolint: goerr113 + } + + if err != nil { + return err + } + + data, err := json.MarshalIndent(res, "", " ") + if err != nil { + return err + } + + fmt.Printf("%s\n", string(data)) + + return nil + }, + } + + return cmd +} + func cpuInfoHandler(w http.ResponseWriter, r *http.Request) { res, err := cpu.New() if err != nil {