Skip to content

Commit

Permalink
add support for vcapFile option
Browse files Browse the repository at this point in the history
fixes #31
  • Loading branch information
Patrick Mueller committed Apr 18, 2018
1 parent 4c29ce1 commit 3490eda
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 46 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock = false
32 changes: 7 additions & 25 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
Contributing
================================================================================

Awesome! I'm happy that you want to contribute.
Awesome! We're happy that you want to contribute.

Make sure that you're read and understand the [Code of Conduct](CODE_OF_CONDUCT.md).


Building from source
--------------------------------------------------------------------------------

If you want to modify the source to play with it, you'll also want to have the
`jbuild` program installed.
The following `npm` scripts are available when doing development on this
package:

To install `jbuild`, use the command

```text
npm -g install jbuild
```

The `jbuild` command runs tasks defined in the `jbuild.coffee` file. The
task you will most likely use is `watch`, which you can run with the
command:

```text
jbuild watch
```

When you run this command, the application will be built from source, the server
started, and tests run. When you subsequently edit and then save one of the
source files, the application will be re-built, the server re-started, and the
tests re-run. For ever. Use Ctrl-C to exit the `jbuild watch` loop.

You can run those build, server, and test tasks separately. Run `jbuild`
with no arguments to see what tasks are available, along with a short
description of them.
* `npm run build` - build the library from the CoffeeScript source
* `npm run serve` - run a test server
* `npm test` - run the tests
* `npm run watch` - watch for source file changes, then `build`, then `test`


GitHub usage
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ your application is running in Cloud Foundry. These include:
If these aren't set, the `getAppEnv()` API will still return useful values,
as appropriate. This means you can use this package in your program and
it will provide useful values when you're running in Cloud Foundry AND
when you're running locally.
when you're running locally. You can supply local versions of
`VCAP_SERVICES` and/or `VCAP_APPLICATION` by using the `vcap` and `vcapFile`
options described below.


api
Expand Down Expand Up @@ -89,6 +91,16 @@ The `options` parameter is optional, and can contain the following properties:

This option property is ignored if not running locally.

* `vcapFile` - provide the same function as the `vcap` option, but instead of
setting the option value to an object, you set it to the name of a JSON
file, which will be parsed and used as the `vcap` option value is used,
as described above.

When both `vcap` and `vcapFile` options are provided, the values in `vcap`
are ignored.

This option property is ignored if not running locally.

This function returns an object with the following properties:

* `app`: object version of `VCAP_APPLICATION` env var
Expand Down Expand Up @@ -311,6 +323,12 @@ When you visit the site, you'll see the output of various cfenv calls.
changes
================================================================================

**1.1.0** - 2018/04/18

- add the `vcapFile` option - [issue #31][]

[issue #31]: https://github.com/cloudfoundry-community/node-cfenv/issues/31

**1.0.4** - 2017/01/13

- fix to getServiceURL() with non-http URLs - [issue #21][]
Expand Down Expand Up @@ -342,6 +360,13 @@ changes
- initial 1.0.0 release


contributing
================================================================================

See the [CONTRIBUTING.md](CONTRIBUTING.md) doc for more information on
contributing to this project.


license
================================================================================

Expand Down
4 changes: 2 additions & 2 deletions jbuild.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ tasks.test = ->
ui: "bdd"
reporter: "spec"
slow: 300
compilers: "coffee:coffee-script"
require: "coffee-script/register"
compilers: "coffee:coffeescript"
require: "coffeescript/register"

options = for key, val of options
"--#{key} #{val}"
Expand Down
22 changes: 22 additions & 0 deletions lib-src/cfenv.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AppEnv
catch
@isLocal = true

@_getVcapFromFile(options) if @isLocal

@app = getApp @, options
@services = getServices @, options

Expand All @@ -38,6 +40,26 @@ class AppEnv
@urls = getURLs @, options
@url = @urls[0]

#-----------------------------------------------------------------------------
_getVcapFromFile: (options) ->
return if not options?.vcapFile

contents = null
try
contents = fs.readFileSync options.vcapFile, 'utf8'
catch err
console.log "error reading vcapFile '#{options.vcapFile}': #{err}; ignoring"
return

vcap = null
try
vcap = JSON.parse contents
catch err
console.log "error parsing vcapFile '#{options.vcapFile}': #{err}; ignoring"
return

options.vcap = vcap

#-----------------------------------------------------------------------------
toJSON: ->
{@app, @services, @isLocal, @name, @port, @bind, @urls, @url}
Expand Down
29 changes: 28 additions & 1 deletion lib/cfenv.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 22 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
{
"name": "cfenv",
"main": "./lib/cfenv",
"description": "easy access to your Cloud Foundry application environment",
"version": "1.0.4",
"author": "pmuellr",
"license": "Apache-2.0",
"homepage": "https://github.com/cloudfoundry-community/node-cfenv",
"name": "cfenv",
"main": "./lib/cfenv",
"description": "easy access to your Cloud Foundry application environment",
"version": "1.1.0",
"author": "pmuellr",
"license": "Apache-2.0",
"homepage": "https://github.com/cloudfoundry-community/node-cfenv",
"repository": {
"type": "git",
"url": "https://github.com/cloudfoundry-community/node-cfenv.git"
"type": "git",
"url": "https://github.com/cloudfoundry-community/node-cfenv.git"
},
"scripts": {
"build": "jbuild build",
"serve": "jbuild serve",
"test": "jbuild test",
"watch": "jbuild watch"
},
"dependencies": {
"js-yaml": "3.7.x",
"ports": "1.1.x",
"underscore": "1.8.x"
"js-yaml": "3.11.x",
"ports": "1.1.x",
"underscore": "1.8.x"
},
"devDependencies": {
"coffee-script": "1.12.x",
"mocha": "3.2.x",
"expect.js": "0.3.x"
"coffeescript": "1.12.x",
"expect.js": "0.3.x",
"jbuild": "1.0.x",
"mocha": "5.1.x"
}
}
1 change: 1 addition & 0 deletions tests/fixtures/vcap-local-bad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this file intentially does not contain valid JSON
13 changes: 13 additions & 0 deletions tests/fixtures/vcap-local-good.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"services": {
"service-a-label": [
{
"name": "service-a",
"label": "service-a-label",
"credentials": {
"url": "foo"
}
}
]
}
}
32 changes: 31 additions & 1 deletion tests/test-core.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fs = require "fs"
path = require "path"

_ = require "underscore"
coffee = require "coffee-script"
coffee = require "coffeescript"
expect = require "expect.js"
ports = require "ports"

Expand Down Expand Up @@ -242,6 +242,36 @@ describe "appEnv", ->
creds = JSON.stringify(creds)
expect(creds).to.be '{"url":"foo"}'

#-----------------------------------------------------------------------------
it "local - with vcapFile option", ->

#-------------------------------------------
vcapFile = path.join(__dirname, 'fixtures', 'vcap-local-good.json')

appEnv = cfenv.getAppEnv {vcapFile}
creds = appEnv.getServiceCreds "service-a"
creds = JSON.stringify(creds)
expect(creds).to.be '{"url":"foo"}'

#-------------------------------------------
vcapFile = path.join(__dirname, 'fixtures', 'vcap-local-bad.json')

console.log "expecting an error printed below:"
appEnv = cfenv.getAppEnv {vcapFile}
creds = appEnv.getServiceCreds "service-a"
expect(creds).to.be null

#-------------------------------------------
vcap = getVCAPServicesWithCreds "service-a",
url: "bar"

vcapFile = path.join(__dirname, 'fixtures', 'vcap-local-good.json')

appEnv = cfenv.getAppEnv {vcap, vcapFile}
creds = appEnv.getServiceCreds "service-a"
creds = JSON.stringify(creds)
expect(creds).to.be '{"url":"foo"}'

#-----------------------------------------------------------------------------
it "remote - VCAP_APPLICATION", ->

Expand Down

0 comments on commit 3490eda

Please sign in to comment.