Debian Codegraph @

rpcvmx/rpcvmx.go

raw symbols SCIP
1 // Copyright 2016 VMware, Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package rpcvmx
16
17 import (
18 "fmt"
19 "strconv"
20 "strings"
21
22 "github.com/vmware/vmw-guestinfo/rpcout"
23 )
24
25 const (
26 prefix = "guestinfo"
27 )
28
29 // Config gives access to the vmx config through the VMware backdoor
30 type Config struct{}
31
32 // NewConfig creates a new Config object
33 func NewConfig() *Config {
34 return &Config{}
35 }
36
37 // String returns the config string in the guestinfo.* namespace
38 func (c *Config) String(key string, defaultValue string) (string, error) {
39 // add "guestinfo." prefix if missing
40 if !strings.HasPrefix(key, prefix) {
41 key = fmt.Sprintf("%s.%s", prefix, key)
42 }
43
44 out, ok, err := rpcout.SendOne("info-get %s", key)
45 if err != nil {
46 return "", err
47 } else if !ok {
48 return defaultValue, nil
49 }
50 return string(out), nil
51 }
52
53 // Bool returns the config boolean in the guestinfo.* namespace
54 func (c *Config) Bool(key string, defaultValue bool) (bool, error) {
55 val, err := c.String(key, fmt.Sprintf("%t", defaultValue))
56 if err != nil {
57 return false, err
58 }
59 res, err := strconv.ParseBool(val)
60 if err != nil {
61 return defaultValue, nil
62 }
63 return res, nil
64 }
65
66 // Int returns the config integer in the guestinfo.* namespace
67 func (c *Config) Int(key string, defaultValue int) (int, error) {
68 val, err := c.String(key, "")
69 if err != nil {
70 return 0, err
71 }
72 res, err := strconv.Atoi(val)
73 if err != nil {
74 return defaultValue, nil
75 }
76 return res, nil
77 }
78
79 // SetString sets the guestinfo.KEY with the string VALUE
80 func (c *Config) SetString(key string, value string) error {
81 // add "guestinfo." prefix if missing
82 if !strings.HasPrefix(key, prefix) {
83 key = fmt.Sprintf("%s.%s", prefix, key)
84 }
85
86 _, _, err := rpcout.SendOne("info-set %s %s", key, value)
87 if err != nil {
88 return err
89 }
90 return nil
91 }
92
93 // SetString sets the guestinfo.KEY with the bool VALUE
94 func (c *Config) SetBool(key string, value bool) error {
95 return c.SetString(key, strconv.FormatBool(value))
96 }
97
98 // SetString sets the guestinfo.KEY with the int VALUE
99 func (c *Config) SetInt(key string, value int) error {
100 return c.SetString(key, strconv.Itoa(value))
101 }