2015-02-26 15:47:56 +00:00
( function ( ) {
2015-08-20 19:54:42 +00:00
var Promise , _ , commandOptions , events , patterns , resin , visuals ;
2015-05-28 16:32:08 +00:00
2015-08-17 14:32:22 +00:00
Promise = require ( 'bluebird' ) ;
2015-02-26 15:47:56 +00:00
2015-08-12 12:17:46 +00:00
_ = require ( 'lodash' ) ;
2015-02-26 15:47:56 +00:00
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-08-31 21:39:48 +00:00
events = require ( 'resin-cli-events' ) ;
2015-02-26 15:47:56 +00:00
commandOptions = require ( './command-options' ) ;
2015-08-20 19:54:42 +00:00
patterns = require ( '../utils/patterns' ) ;
2015-08-17 14:32:22 +00:00
2015-02-26 15:47:56 +00:00
exports . list = {
signature : 'envs' ,
description : 'list all environment variables' ,
2015-07-27 19:20:50 +00:00
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 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9' ,
2015-02-26 15:47:56 +00:00
options : [
2015-05-28 16:32:08 +00:00
commandOptions . optionalApplication , commandOptions . optionalDevice , {
2015-02-26 15:47:56 +00:00
signature : 'verbose' ,
description : 'show private environment variables' ,
boolean : true ,
alias : 'v'
}
] ,
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-17 14:32:22 +00:00
return Promise [ "try" ] ( function ( ) {
if ( options . application != null ) {
return resin . models . environmentVariables . getAllByApplication ( options . application ) ;
} else if ( options . device != null ) {
return resin . models . environmentVariables . device . getAll ( options . device ) ;
} else {
throw new Error ( 'You must specify an application or device' ) ;
}
} ) . tap ( function ( environmentVariables ) {
var isSystemVariable ;
2015-08-18 14:03:39 +00:00
if ( _ . isEmpty ( environmentVariables ) ) {
throw new Error ( 'No environment variables found' ) ;
}
2015-08-17 14:32:22 +00:00
if ( ! options . verbose ) {
isSystemVariable = resin . models . environmentVariables . isSystemVariable ;
environmentVariables = _ . reject ( environmentVariables , isSystemVariable ) ;
2015-02-26 15:47:56 +00:00
}
2015-08-17 14:32:22 +00:00
return console . log ( visuals . table . horizontal ( environmentVariables , [ 'id' , 'name' , 'value' ] ) ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . remove = {
signature : 'env rm <id>' ,
description : 'remove an environment variable' ,
2015-05-28 16:32:08 +00:00
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 , commandOptions . booleanDevice ] ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-20 19:54:42 +00:00
return patterns . confirm ( options . yes , 'Are you sure you want to delete the environment variable?' ) . then ( function ( ) {
2015-08-17 14:32:22 +00:00
if ( options . device ) {
2015-08-31 21:39:48 +00:00
resin . models . environmentVariables . device . remove ( params . id ) ;
return events . send ( 'deviceEnvironmentVariable.delete' , {
id : params . id
} ) ;
2015-08-17 14:32:22 +00:00
} else {
2015-08-31 21:39:48 +00:00
resin . models . environmentVariables . remove ( params . id ) ;
return events . send ( 'environmentVariable.delete' , {
id : params . id
} ) ;
2015-08-17 14:32:22 +00:00
}
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . add = {
signature : 'env add <key> [value]' ,
description : 'add an environment variable' ,
2015-08-31 21:56:01 +00:00
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 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9' ,
2015-05-28 16:32:08 +00:00
options : [ commandOptions . optionalApplication , commandOptions . optionalDevice ] ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-17 14:32:22 +00:00
return Promise [ "try" ] ( function ( ) {
2015-02-26 15:47:56 +00:00
if ( params . value == null ) {
2015-08-17 14:32:22 +00:00
params . value = process . env [ params . key ] ;
if ( params . value == null ) {
throw new Error ( "Environment value not found for key: " + params . key ) ;
} else {
console . info ( "Warning: using " + params . key + "=" + params . value + " from host environment" ) ;
}
}
if ( options . application != null ) {
2015-08-31 21:39:48 +00:00
return resin . models . environmentVariables . create ( options . application , params . key , params . value ) . then ( function ( ) {
return resin . models . application . get ( options . application ) . then ( function ( application ) {
return events . send ( 'environmentVariable.create' , {
application : application . id
} ) ;
} ) ;
} ) ;
2015-08-17 14:32:22 +00:00
} else if ( options . device != null ) {
2015-08-31 21:39:48 +00:00
return resin . models . environmentVariables . device . create ( options . device , params . key , params . value ) . then ( function ( ) {
return events . send ( 'deviceEnvironmentVariable.create' , {
device : options . device
} ) ;
} ) ;
2015-02-26 15:47:56 +00:00
} else {
2015-08-17 14:32:22 +00:00
throw new Error ( 'You must specify an application or device' ) ;
2015-02-26 15:47:56 +00:00
}
2015-08-17 14:32:22 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . rename = {
signature : 'env rename <id> <value>' ,
description : 'rename an environment variable' ,
2015-05-28 16:32:08 +00:00
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' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
2015-05-28 16:32:08 +00:00
options : [ commandOptions . booleanDevice ] ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2015-08-17 14:32:22 +00:00
return Promise [ "try" ] ( function ( ) {
if ( options . device ) {
2015-08-31 21:39:48 +00:00
return resin . models . environmentVariables . device . update ( params . id , params . value ) . then ( function ( ) {
return events . send ( 'deviceEnvironmentVariable.edit' , {
id : params . id
} ) ;
} ) ;
2015-08-17 14:32:22 +00:00
} else {
2015-08-31 21:39:48 +00:00
return resin . models . environmentVariables . update ( params . id , params . value ) . then ( function ( ) {
return events . send ( 'environmentVariable.edit' , {
id : params . id
} ) ;
} ) ;
2015-08-17 14:32:22 +00:00
}
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
} ) . call ( this ) ;