helper.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package smart
  2. import (
  3. "log"
  4. "os/exec"
  5. "strings"
  6. )
  7. func execCommand(executable string, args ...string) string {
  8. shell := exec.Command(executable, args...) // Run command
  9. output, err := shell.CombinedOutput() // Response from cmdline
  10. if err != nil && string(output) == "" { // If done w/ errors then
  11. log.Println(err)
  12. return ""
  13. }
  14. return string(output)
  15. }
  16. func wmicGetinfo(wmicName string, itemName string) []string {
  17. //get systeminfo
  18. var InfoStorage []string
  19. cmd := exec.Command("chcp", "65001")
  20. cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")
  21. if wmicName == "os" {
  22. cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")
  23. }
  24. if len(wmicName) > 6 {
  25. if wmicName[0:6] == "Win32_" {
  26. cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")
  27. }
  28. }
  29. out, _ := cmd.CombinedOutput()
  30. strOut := string(out)
  31. strSplitedOut := strings.Split(strOut, "\n")
  32. for _, strConfig := range strSplitedOut {
  33. if strings.Contains(strConfig, "=") {
  34. strSplitedConfig := strings.SplitN(strConfig, "=", 2)
  35. if strSplitedConfig[0] == itemName {
  36. strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)
  37. InfoStorage = append(InfoStorage, strSplitedConfigReplaced)
  38. }
  39. }
  40. }
  41. if len(InfoStorage) == 0 {
  42. InfoStorage = append(InfoStorage, "Undefined")
  43. }
  44. return InfoStorage
  45. }