Add per device environment variable support

This commit is contained in:
Juan Cruz Viotti 2015-05-28 12:32:08 -04:00
parent b04ed43bed
commit 054d5e4879
5 changed files with 140 additions and 47 deletions

View File

@ -21,6 +21,20 @@
required: 'You have to specify an application' required: 'You have to specify an application'
}, exports.optionalApplication); }, exports.optionalApplication);
exports.optionalDevice = {
signature: 'device',
parameter: 'device',
description: 'device name',
alias: 'd'
};
exports.booleanDevice = {
signature: 'device',
description: 'device name',
boolean: true,
alias: 'd'
};
exports.network = { exports.network = {
signature: 'network', signature: 'network',
parameter: 'network', parameter: 'network',

View File

@ -1,5 +1,7 @@
(function() { (function() {
var _, commandOptions, resin, visuals; var _, async, commandOptions, resin, visuals;
async = require('async');
_ = require('lodash-contrib'); _ = require('lodash-contrib');
@ -12,9 +14,9 @@
exports.list = { exports.list = {
signature: 'envs', signature: 'envs',
description: 'list all environment variables', description: 'list all environment variables',
help: 'Use this command to list all environment variables for a particular application.\nNotice we will support per-device environment variables soon.\n\nThis command lists all custom environment variables set on the devices running\nthe application. If you want to see all environment variables, including private\nones used by resin, use the verbose option.\n\nExample:\n\n $ resin envs --application 91\n $ resin envs --application 91 --verbose', help: 'Use this command to list all environment variables for\na particular application or device.\n\nThis command lists all custom environment variables.\nIf you want to see all environment variables, including private\nones used by resin, use the verbose option.\n\nExample:\n\n $ resin envs --application MyApp\n $ resin envs --application MyApp --verbose\n $ resin envs --device MyDevice',
options: [ options: [
commandOptions.application, { commandOptions.optionalApplication, commandOptions.optionalDevice, {
signature: 'verbose', signature: 'verbose',
description: 'show private environment variables', description: 'show private environment variables',
boolean: true, boolean: true,
@ -23,28 +25,41 @@
], ],
permission: 'user', permission: 'user',
action: function(params, options, done) { action: function(params, options, done) {
return resin.models.environmentVariables.getAllByApplication(options.application, function(error, environmentVariables) { return async.waterfall([
if (error != null) { function(callback) {
return done(error); if (options.application != null) {
return resin.models.environmentVariables.getAllByApplication(options.application, callback);
} else if (options.device != null) {
return resin.models.environmentVariables.device.getAll(options.device, callback);
} else {
return callback(new Error('You must specify an application or device'));
} }
}, function(environmentVariables, callback) {
var isSystemVariable;
if (!options.verbose) { if (!options.verbose) {
environmentVariables = _.reject(environmentVariables, resin.models.environmentVariables.isSystemVariable); isSystemVariable = resin.models.environmentVariables.isSystemVariable;
environmentVariables = _.reject(environmentVariables, isSystemVariable);
} }
console.log(visuals.widgets.table.horizontal(environmentVariables, ['id', 'name', 'value'])); console.log(visuals.widgets.table.horizontal(environmentVariables, ['id', 'name', 'value']));
return done(); return callback();
}); }
], done);
} }
}; };
exports.remove = { exports.remove = {
signature: 'env rm <id>', signature: 'env rm <id>',
description: 'remove an environment variable', description: 'remove an environment variable',
help: 'Use this command to remove an environment variable from an application.\n\nDon\'t remove resin specific variables, as things might not work as expected.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin env rm 215\n $ resin env rm 215 --yes', help: 'Use this command to remove an environment variable from an application.\n\nDon\'t remove resin specific variables, as things might not work as expected.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nIf you want to eliminate a device environment variable, pass the `--device` boolean option.\n\nExamples:\n\n $ resin env rm 215\n $ resin env rm 215 --yes\n $ resin env rm 215 --device',
options: [commandOptions.yes], options: [commandOptions.yes, commandOptions.booleanDevice],
permission: 'user', permission: 'user',
action: function(params, options, done) { action: function(params, options, done) {
return visuals.patterns.remove('environment variable', options.yes, function(callback) { return visuals.patterns.remove('environment variable', options.yes, function(callback) {
if (options.device) {
return resin.models.environmentVariables.device.remove(params.id, callback);
} else {
return resin.models.environmentVariables.remove(params.id, callback); return resin.models.environmentVariables.remove(params.id, callback);
}
}, done); }, done);
} }
}; };
@ -52,8 +67,8 @@
exports.add = { exports.add = {
signature: 'env add <key> [value]', signature: 'env add <key> [value]',
description: 'add an environment variable', description: 'add an environment variable',
help: 'Use this command to add an enviroment variable to an application.\n\nYou need to pass the `--application` option.\n\nIf value is omitted, the tool will attempt to use the variable\'s value\nas defined in your host machine.\n\nIf the value is grabbed from the environment, a warning message will be printed.\nUse `--quiet` to remove it.\n\nExamples:\n\n $ resin env add EDITOR vim -a 91\n $ resin env add TERM -a 91', help: 'Use this command to add an enviroment variable to an application.\n\nIf value is omitted, the tool will attempt to use the variable\'s value\nas defined in your host machine.\n\nUse the `--device` option if you want to assign the environment variable\nto a specific device.\n\nIf the value is grabbed from the environment, a warning message will be printed.\nUse `--quiet` to remove it.\n\nExamples:\n\n $ resin env add EDITOR vim --application MyApp\n $ resin env add TERM --application MyApp\n $ resin env add EDITOR vim --device MyDevice',
options: [commandOptions.application], options: [commandOptions.optionalApplication, commandOptions.optionalDevice],
permission: 'user', permission: 'user',
action: function(params, options, done) { action: function(params, options, done) {
if (params.value == null) { if (params.value == null) {
@ -64,18 +79,29 @@
console.info("Warning: using " + params.key + "=" + params.value + " from host environment"); console.info("Warning: using " + params.key + "=" + params.value + " from host environment");
} }
} }
if (options.application != null) {
return resin.models.environmentVariables.create(options.application, params.key, params.value, done); return resin.models.environmentVariables.create(options.application, params.key, params.value, done);
} else if (options.device != null) {
return resin.models.environmentVariables.device.create(options.device, params.key, params.value, done);
} else {
return done(new Error('You must specify an application or device'));
}
} }
}; };
exports.rename = { exports.rename = {
signature: 'env rename <id> <value>', signature: 'env rename <id> <value>',
description: 'rename an environment variable', description: 'rename an environment variable',
help: 'Use this command to rename an enviroment variable from an application.\n\nExamples:\n\n $ resin env rename 376 emacs', help: 'Use this command to rename an enviroment variable from an application.\n\nPass the `--device` boolean option if you want to rename a device environment variable.\n\nExamples:\n\n $ resin env rename 376 emacs\n $ resin env rename 376 emacs --device',
permission: 'user', permission: 'user',
options: [commandOptions.booleanDevice],
action: function(params, options, done) { action: function(params, options, done) {
if (options.device) {
return resin.models.environmentVariables.device.update(params.id, params.value, done);
} else {
return resin.models.environmentVariables.update(params.id, params.value, done); return resin.models.environmentVariables.update(params.id, params.value, done);
} }
}
}; };
}).call(this); }).call(this);

View File

@ -16,6 +16,18 @@ exports.application = _.defaults
required: 'You have to specify an application' required: 'You have to specify an application'
, exports.optionalApplication , exports.optionalApplication
exports.optionalDevice =
signature: 'device'
parameter: 'device'
description: 'device name'
alias: 'd'
exports.booleanDevice =
signature: 'device'
description: 'device name'
boolean: true
alias: 'd'
exports.network = exports.network =
signature: 'network' signature: 'network'
parameter: 'network' parameter: 'network'

View File

@ -1,3 +1,4 @@
async = require('async')
_ = require('lodash-contrib') _ = require('lodash-contrib')
resin = require('resin-sdk') resin = require('resin-sdk')
visuals = require('resin-cli-visuals') visuals = require('resin-cli-visuals')
@ -7,20 +8,22 @@ exports.list =
signature: 'envs' signature: 'envs'
description: 'list all environment variables' description: 'list all environment variables'
help: ''' help: '''
Use this command to list all environment variables for a particular application. Use this command to list all environment variables for
Notice we will support per-device environment variables soon. a particular application or device.
This command lists all custom environment variables set on the devices running This command lists all custom environment variables.
the application. If you want to see all environment variables, including private If you want to see all environment variables, including private
ones used by resin, use the verbose option. ones used by resin, use the verbose option.
Example: Example:
$ resin envs --application 91 $ resin envs --application MyApp
$ resin envs --application 91 --verbose $ resin envs --application MyApp --verbose
$ resin envs --device MyDevice
''' '''
options: [ options: [
commandOptions.application commandOptions.optionalApplication
commandOptions.optionalDevice
{ {
signature: 'verbose' signature: 'verbose'
@ -31,11 +34,21 @@ exports.list =
] ]
permission: 'user' permission: 'user'
action: (params, options, done) -> action: (params, options, done) ->
resin.models.environmentVariables.getAllByApplication options.application, (error, environmentVariables) -> async.waterfall([
return done(error) if error?
(callback) ->
if options.application?
resin.models.environmentVariables.getAllByApplication(options.application, callback)
else if options.device?
resin.models.environmentVariables.device.getAll(options.device, callback)
else
return callback(new Error('You must specify an application or device'))
(environmentVariables, callback) ->
if not options.verbose if not options.verbose
environmentVariables = _.reject(environmentVariables, resin.models.environmentVariables.isSystemVariable) isSystemVariable = resin.models.environmentVariables.isSystemVariable
environmentVariables = _.reject(environmentVariables, isSystemVariable)
console.log visuals.widgets.table.horizontal environmentVariables, [ console.log visuals.widgets.table.horizontal environmentVariables, [
'id' 'id'
@ -43,7 +56,9 @@ exports.list =
'value' 'value'
] ]
return done() return callback()
], done)
exports.remove = exports.remove =
signature: 'env rm <id>' signature: 'env rm <id>'
@ -56,15 +71,24 @@ exports.remove =
Notice this command asks for confirmation interactively. Notice this command asks for confirmation interactively.
You can avoid this by passing the `--yes` boolean option. You can avoid this by passing the `--yes` boolean option.
If you want to eliminate a device environment variable, pass the `--device` boolean option.
Examples: Examples:
$ resin env rm 215 $ resin env rm 215
$ resin env rm 215 --yes $ resin env rm 215 --yes
$ resin env rm 215 --device
''' '''
options: [ commandOptions.yes ] options: [
commandOptions.yes
commandOptions.booleanDevice
]
permission: 'user' permission: 'user'
action: (params, options, done) -> action: (params, options, done) ->
visuals.patterns.remove 'environment variable', options.yes, (callback) -> visuals.patterns.remove 'environment variable', options.yes, (callback) ->
if options.device
resin.models.environmentVariables.device.remove(params.id, callback)
else
resin.models.environmentVariables.remove(params.id, callback) resin.models.environmentVariables.remove(params.id, callback)
, done , done
@ -74,20 +98,25 @@ exports.add =
help: ''' help: '''
Use this command to add an enviroment variable to an application. Use this command to add an enviroment variable to an application.
You need to pass the `--application` option.
If value is omitted, the tool will attempt to use the variable's value If value is omitted, the tool will attempt to use the variable's value
as defined in your host machine. as defined in your host machine.
Use the `--device` option if you want to assign the environment variable
to a specific device.
If the value is grabbed from the environment, a warning message will be printed. If the value is grabbed from the environment, a warning message will be printed.
Use `--quiet` to remove it. Use `--quiet` to remove it.
Examples: Examples:
$ resin env add EDITOR vim -a 91 $ resin env add EDITOR vim --application MyApp
$ resin env add TERM -a 91 $ resin env add TERM --application MyApp
$ resin env add EDITOR vim --device MyDevice
''' '''
options: [ commandOptions.application ] options: [
commandOptions.optionalApplication
commandOptions.optionalDevice
]
permission: 'user' permission: 'user'
action: (params, options, done) -> action: (params, options, done) ->
if not params.value? if not params.value?
@ -98,7 +127,12 @@ exports.add =
else else
console.info("Warning: using #{params.key}=#{params.value} from host environment") console.info("Warning: using #{params.key}=#{params.value} from host environment")
if options.application?
resin.models.environmentVariables.create(options.application, params.key, params.value, done) resin.models.environmentVariables.create(options.application, params.key, params.value, done)
else if options.device?
resin.models.environmentVariables.device.create(options.device, params.key, params.value, done)
else
return done(new Error('You must specify an application or device'))
exports.rename = exports.rename =
signature: 'env rename <id> <value>' signature: 'env rename <id> <value>'
@ -106,10 +140,17 @@ exports.rename =
help: ''' help: '''
Use this command to rename an enviroment variable from an application. Use this command to rename an enviroment variable from an application.
Pass the `--device` boolean option if you want to rename a device environment variable.
Examples: Examples:
$ resin env rename 376 emacs $ resin env rename 376 emacs
$ resin env rename 376 emacs --device
''' '''
permission: 'user' permission: 'user'
options: [ commandOptions.booleanDevice ]
action: (params, options, done) -> action: (params, options, done) ->
if options.device
resin.models.environmentVariables.device.update(params.id, params.value, done)
else
resin.models.environmentVariables.update(params.id, params.value, done) resin.models.environmentVariables.update(params.id, params.value, done)

View File

@ -60,7 +60,7 @@
"open": "0.0.5", "open": "0.0.5",
"resin-cli-visuals": "^0.1.0", "resin-cli-visuals": "^0.1.0",
"resin-image": "^1.1.3", "resin-image": "^1.1.3",
"resin-sdk": "^1.4.3", "resin-sdk": "^1.7.3",
"resin-settings-client": "^1.0.0", "resin-settings-client": "^1.0.0",
"resin-vcs": "^1.2.0", "resin-vcs": "^1.2.0",
"selfupdate": "^1.1.0", "selfupdate": "^1.1.0",