Skip to content

Commit

Permalink
Merge pull request #54 from duglah/master
Browse files Browse the repository at this point in the history
Implemented getextradata and setextradata
  • Loading branch information
michaelsanford committed Jun 5, 2018
2 parents cb260e9 + e92ca67 commit 021d2f4
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -53,3 +53,5 @@ node_modules
######################
# Projects checked out to this root
######################

\.vscode/
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -296,6 +296,35 @@ virtualbox.guestproperty(function guestproperty_callback(machines, error) {
});
```

Obtaining an extra property by key name:

```javascript
var options = {
vm: "machine_name",
key: "GUI/Fullscreen"
}

virtualbox.extradata.get(options, function extradataget_callback(error, value) {
if (error) throw error;
console.log('Virtual Machine "%s" extra "%s" value is "%s"', options.vm, options.key, value);
});
```

Writing an extra property by key name:

```javascript
var options = {
vm: "machine_name",
key: "GUI/Fullscreen",
value: "true"
}

virtualbox.extradata.set(options, function extradataset_callback(error) {
if (error) throw error;
console.log('Set Virtual Machine "%s" extra "%s" value to "%s"', options.vm, options.key, options.value);
});
```

# Putting it all together

```javascript
Expand Down Expand Up @@ -346,6 +375,8 @@ virtualbox.start("machine_name", function start_callback(error) {
- `.snapshotTake({vm:"machine_name"}, {vm:"snapshot_name"}, callback)`
- `.snapshotDelete({vm:"machine_name"}, {vm:"snapshot_UUID"}, callback)`
- `.snapshotRestore({vm:"machine_name"}, {vm:"snapshot_UUID"}, callback)`
- `.extradata.get({vm:"machine_name", key:"keyname"}, callback)`
- `.extradata.set({vm:"machine_name", key:"keyname", value:"val"}, callback)`

# Troubleshooting

Expand Down
33 changes: 33 additions & 0 deletions lib/virtualbox.js
Expand Up @@ -453,6 +453,38 @@ var guestproperty = {

};

var extradata = {
get: function(options, callback) {
var vm = options.vm || options.name || options.vmname || options.title,
key = options.key,
value = options.defaultValue || options.value;

var cmd = 'getextradata "' + vm + '" "' + key + '"';
vboxmanage(cmd, function(error, stdout) {
if (error) {
callback(error);
return;
}
var value = stdout.substr(stdout.indexOf(':') + 1).trim();
if (value === 'No value set!') {
value = undefined;
}
callback(null, value);
});
},

set: function(options, callback) {
var vm = options.vm || options.name || options.vmname || options.title,
key = options.key,
value = options.defaultValue || options.value;

var cmd = 'setextradata "' + vm + '" "' + key + '" "' + value + '"';
vboxmanage(cmd, function(error, stdout) {
callback(error);
});
}
};

module.exports = {
'exec': vmExec,
'kill': vmKill,
Expand All @@ -475,6 +507,7 @@ module.exports = {
'snapshotDelete': snapshotDelete,
'snapshotRestore': snapshotRestore,
'isRunning': isRunning,
'extradata': extradata,

'SCAN_CODES': require('./scan-codes')
};
12 changes: 12 additions & 0 deletions test/getextradata.js
@@ -0,0 +1,12 @@
"use strict";

var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2);

virtualbox.extradata.get({vm: args[0], key: args[1]}, function(error, value){
if(error) {
throw error;
}

console.log('Virtual Machine "%s" extra "%s" value is "%s"', args[0], args[1], value);
});
12 changes: 12 additions & 0 deletions test/setextradata.js
@@ -0,0 +1,12 @@
"use strict";

var virtualbox = require('../lib/virtualbox'),
args = process.argv.slice(2);

virtualbox.extradata.set({vm: args[0], key: args[1], value: args[2]}, function(error, value){
if(error) {
throw error;
}

console.log('Set Virtual Machine "%s" extra "%s" value to "%s"', args[0], args[1], args[2]);
});

0 comments on commit 021d2f4

Please sign in to comment.