rpcvmx.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package rpcvmx
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/vmware/vmw-guestinfo/rpcout"
  6. )
  7. // Config gives access to the vmx config through the VMware backdoor
  8. type Config struct{}
  9. // NewConfig creates a new Config object
  10. func NewConfig() *Config {
  11. return &Config{}
  12. }
  13. // String returns the config string in the guestinfo.* namespace
  14. func (c *Config) String(key string, defaultValue string) (string, error) {
  15. out, ok, err := rpcout.SendOne("info-get guestinfo.%s", key)
  16. if err != nil {
  17. return "", err
  18. } else if !ok {
  19. return defaultValue, nil
  20. }
  21. return string(out), nil
  22. }
  23. // Bool returns the config boolean in the guestinfo.* namespace
  24. func (c *Config) Bool(key string, defaultValue bool) (bool, error) {
  25. val, err := c.String(key, fmt.Sprintf("%t", defaultValue))
  26. if err != nil {
  27. return false, err
  28. }
  29. res, err := strconv.ParseBool(val)
  30. if err != nil {
  31. return defaultValue, nil
  32. }
  33. return res, nil
  34. }
  35. // Int returns the config integer in the guestinfo.* namespace
  36. func (c *Config) Int(key string, defaultValue int) (int, error) {
  37. val, err := c.String(key, "")
  38. if err != nil {
  39. return 0, err
  40. }
  41. res, err := strconv.Atoi(val)
  42. if err != nil {
  43. return defaultValue, nil
  44. }
  45. return res, nil
  46. }
  47. // SetString sets the guestinfo.KEY with the string VALUE
  48. func (c *Config) SetString(key string, value string) error {
  49. _, _, err := rpcout.SendOne("info-set guestinfo.%s %s", key, value)
  50. if err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. // SetString sets the guestinfo.KEY with the bool VALUE
  56. func (c *Config) SetBool(key string, value bool) error {
  57. return c.SetString(key, strconv.FormatBool(value))
  58. }
  59. // SetString sets the guestinfo.KEY with the int VALUE
  60. func (c *Config) SetInt(key string, value int) error {
  61. return c.SetString(key, strconv.Itoa(value))
  62. }