-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sets and gets information from a VMX file
The information stored and retrieved is limited to the needs of this project.
- Loading branch information
Showing
4 changed files
with
126 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package vmware | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func readvmx(vmxpath string) (map[string]string, error) { | ||
data, err := ioutil.ReadFile(vmxpath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vmx := make(map[string]string) | ||
for _, line := range strings.Split(string(data), "\n") { | ||
values := strings.Split(line, "=") | ||
if len(values) != 2 { | ||
continue | ||
} | ||
|
||
k := strings.TrimSpace(values[0]) | ||
v := strings.TrimSpace(values[1]) | ||
vmx[strings.ToLower(k)] = strings.Trim(v, `"`) | ||
} | ||
|
||
return vmx, nil | ||
} | ||
|
||
func writevmx(vmxpath string, vmx map[string]string) error { | ||
f, err := os.Create(vmxpath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
|
||
i := 0 | ||
keys := make([]string, len(vmx)) | ||
for k := range vmx { | ||
keys[i] = k | ||
i++ | ||
} | ||
|
||
sort.Strings(keys) | ||
|
||
var buf bytes.Buffer | ||
for _, key := range keys { | ||
buf.WriteString(key + " = " + `"` + vmx[key] + `"`) | ||
buf.WriteString("\n") | ||
} | ||
|
||
if _, err = io.Copy(f, &buf); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters