diff --git a/bin/cfenv-launch b/bin/cfenv-launch new file mode 100755 index 0000000..ef614ae --- /dev/null +++ b/bin/cfenv-launch @@ -0,0 +1,25 @@ +#!/usr/bin/env node + +// Licensed under the Apache License. See footer for details. + +var path = require("path") +var fs = require("fs") +var lib = path.join(path.dirname(fs.realpathSync(__filename)), "../lib") + +require(lib + "/cfenv-launch").main() + +//------------------------------------------------------------------------------ +// Copyright IBM Corp. 2014 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//------------------------------------------------------------------------------ diff --git a/lib-src/cfenv-launch.coffee b/lib-src/cfenv-launch.coffee new file mode 100644 index 0000000..e77e1ea --- /dev/null +++ b/lib-src/cfenv-launch.coffee @@ -0,0 +1,89 @@ +# Licensed under the Apache License. See footer for details. + +path = require "path" +child_process = require "child_process" + +cfenv = require("./cfenv") + +PROGRAM = path.basename(__filename).split(".")[0] + +#------------------------------------------------------------------------------- +main = -> + appEnv = cfenv.getAppEnv() + appName = appEnv.name + + unless appName? + log "unable to determine app name from manifest.yml" + process.exit 1 + + getLiveServices appName + +#------------------------------------------------------------------------------- +getLiveServices = (appName) -> + command = "cf env #{appName}" + process = child_process.exec command, (err, stdout, stderr) -> + gotLiveServices appName, err, stdout, stderr + +#------------------------------------------------------------------------------- +gotLiveServices = (appName, err, stdout, stderr) -> + if err? + log "error running #{command}: #{err.code}" + process.exit 1 + + pattern = /\"VCAP_SERVICES\": \{([\s\S]*?)\n\}\n/ + + stdout += "" + match = stdout.match pattern + + unless match? + log "no services found for #{appName}" + launch() + return + + vcapServices = "{#{match[1]}" + vcapServices = JSON.parse vcapServices + + launch vcapServices + +#------------------------------------------------------------------------------- +launch = (vcapServices) -> + vcapServices = JSON.stringify vcapServices if vcapServices? + + cmd = process.argv[2] + args = process.argv[3..] + env = process.env + stdio = "inherit" + + env.VCAP_SERVICES = vcapServices + + unless cmd? + log "no command specified to launch" + process.exit 1 + + log "launching `#{cmd} #{args.join ' '}`" + child_process.spawn cmd, args, {env, stdio} + +#------------------------------------------------------------------------------- +log = (message) -> + console.log "#{PROGRAM}: #{message}" + +#------------------------------------------------------------------------------- +exports.main = main + +main() if require.main == module + +#------------------------------------------------------------------------------- +# Copyright IBM Corp. 2014 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#------------------------------------------------------------------------------- diff --git a/lib/cfenv-launch.js b/lib/cfenv-launch.js new file mode 100644 index 0000000..8496131 --- /dev/null +++ b/lib/cfenv-launch.js @@ -0,0 +1,82 @@ +// Generated by CoffeeScript 1.8.0 +(function() { + var PROGRAM, cfenv, child_process, getLiveServices, gotLiveServices, launch, log, main, path; + + path = require("path"); + + child_process = require("child_process"); + + cfenv = require("./cfenv"); + + PROGRAM = path.basename(__filename).split(".")[0]; + + main = function() { + var appEnv, appName; + appEnv = cfenv.getAppEnv(); + appName = appEnv.name; + if (appName == null) { + log("unable to determine app name from manifest.yml"); + process.exit(1); + } + return getLiveServices(appName); + }; + + getLiveServices = function(appName) { + var command, process; + command = "cf env " + appName; + return process = child_process.exec(command, function(err, stdout, stderr) { + return gotLiveServices(appName, err, stdout, stderr); + }); + }; + + gotLiveServices = function(appName, err, stdout, stderr) { + var match, pattern, vcapServices; + if (err != null) { + log("error running " + command + ": " + err.code); + process.exit(1); + } + pattern = /\"VCAP_SERVICES\": \{([\s\S]*?)\n\}\n/; + stdout += ""; + match = stdout.match(pattern); + if (match == null) { + log("no services found for " + appName); + launch(); + return; + } + vcapServices = "{" + match[1]; + vcapServices = JSON.parse(vcapServices); + return launch(vcapServices); + }; + + launch = function(vcapServices) { + var args, cmd, env, stdio; + if (vcapServices != null) { + vcapServices = JSON.stringify(vcapServices); + } + cmd = process.argv[2]; + args = process.argv.slice(3); + env = process.env; + stdio = "inherit"; + env.VCAP_SERVICES = vcapServices; + if (cmd == null) { + log("no command specified to launch"); + process.exit(1); + } + log("launching `" + cmd + " " + (args.join(' ')) + "`"); + return child_process.spawn(cmd, args, { + env: env, + stdio: stdio + }); + }; + + log = function(message) { + return console.log("" + PROGRAM + ": " + message); + }; + + exports.main = main; + + if (require.main === module) { + main(); + } + +}).call(this); diff --git a/lib/cfenv.js b/lib/cfenv.js index cf008ee..2c0a875 100644 --- a/lib/cfenv.js +++ b/lib/cfenv.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.0 (function() { var AppEnv, URL, cfenv, fs, getApp, getBind, getName, getPort, getServices, getURLs, pkg, ports, throwError, yaml, _; diff --git a/lib/server.js b/lib/server.js index a6c66ab..f9c3850 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.0 (function() { var JL, JS, cfenv, generateDump, http; diff --git a/package.json b/package.json index 09b3bc4..319c0e8 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "author": "pmuellr", "license": "Apache-2.0", "homepage": "https://github.com/cloudfoundry-community/node-cfenv", + "bin": { + "cfenv-launch": "./bin/cfenv-launch" + }, "repository": { "type": "git", "url": "https://github.com/cloudfoundry-community/node-cfenv.git" @@ -13,7 +16,7 @@ "dependencies": { "js-yaml": "3.2.x", "ports": "1.1.x", - "underscore": "1.7.x" + "underscore": "1.8.x" }, "devDependencies": { "coffee-script": "1.8.x",