diff --git a/lib/actions/environment-variables.coffee b/lib/actions/environment-variables.coffee index 99b308fc..c0e4c8e8 100644 --- a/lib/actions/environment-variables.coffee +++ b/lib/actions/environment-variables.coffee @@ -24,3 +24,18 @@ exports.remove = permissions.user (params, options) -> ui.patterns.remove 'environment variable', options.yes, (callback) -> resin.models.environmentVariables.remove(params.id, callback) , resin.errors.handle + +exports.add = permissions.user (params, options) -> + if not options.application? + resin.errors.handle(new Error('You have to specify an application')) + + if not params.value? + params.value = process.env[params.key] + + if not params.value? + resin.errors.handle(new Error("Environment value not found for key: #{params.key}")) + else + resin.log.info("Warning: using #{params.key}=#{params.value} from host environment") + + resin.models.environmentVariables.create options.application, params.key, params.value, (error) -> + resin.errors.handle(error) if error? diff --git a/lib/app.coffee b/lib/app.coffee index 05b096b9..d4e7d473 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -37,6 +37,12 @@ yesOption = boolean: true alias: 'y' +applicationOption = + signature: 'application' + parameter: 'application' + description: 'application id' + alias: [ 'a', 'app' ] + # ---------- Auth Module ---------- capitano.command signature: 'login [credentials]' @@ -326,12 +332,8 @@ capitano.command ''' action: actions.env.list options: [ - { - signature: 'application' - parameter: 'application' - description: 'application id' - alias: [ 'a', 'app' ] - } + applicationOption + { signature: 'verbose' description: 'show private environment variables' @@ -340,6 +342,27 @@ capitano.command } ] +capitano.command + signature: 'env add [value]' + description: 'add an environment variable' + help: ''' + 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 + as defined in your host machine. + + If the value is grabbed from the environment, a warning message will be printed. + Use `--quiet` to remove it. + + Examples: + $ resin env add EDITOR vim -a 91 + $ resin env add TERM -a 91 + ''' + options: [ applicationOption ] + action: actions.env.add + capitano.command signature: 'env rm ' description: 'remove an environment variable' diff --git a/lib/resin/models/environment-variables.coffee b/lib/resin/models/environment-variables.coffee index f577787f..b46d41c8 100644 --- a/lib/resin/models/environment-variables.coffee +++ b/lib/resin/models/environment-variables.coffee @@ -30,6 +30,31 @@ exports.getAllByApplication = (applicationId, callback) -> .catch (error) -> return callback(error) +# Create an environment variable for an application +# +# @param {String, Number} applicationId application id +# @param {String} name environment variable name +# @param {String} value environment variable value +# @param {Function} callback callback(error) +# +# @example Create an environment variable +# resin.models.environmentVariables.create 91, 'EDITOR', 'vim', (error) -> +# throw error if error? +# +exports.create = (applicationId, name, value, callback) -> + return pine.post + resource: 'environment_variable' + data: + name: name + value: value + application: applicationId + + .then -> + return callback() + + .catch (error) -> + return callback(error) + # Remove environment variable # # @param {String, Number} id environment variable id