Skip to content

Commit 3490eda

Browse files
author
Patrick Mueller
committedApr 18, 2018
add support for vcapFile option
fixes #31
1 parent 4c29ce1 commit 3490eda

11 files changed

+154
-46
lines changed
 

‎.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock = false

‎CONTRIBUTING.md

+7-25
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
Contributing
22
================================================================================
33

4-
Awesome! I'm happy that you want to contribute.
4+
Awesome! We're happy that you want to contribute.
55

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

88

99
Building from source
1010
--------------------------------------------------------------------------------
1111

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

15-
To install `jbuild`, use the command
16-
17-
```text
18-
npm -g install jbuild
19-
```
20-
21-
The `jbuild` command runs tasks defined in the `jbuild.coffee` file. The
22-
task you will most likely use is `watch`, which you can run with the
23-
command:
24-
25-
```text
26-
jbuild watch
27-
```
28-
29-
When you run this command, the application will be built from source, the server
30-
started, and tests run. When you subsequently edit and then save one of the
31-
source files, the application will be re-built, the server re-started, and the
32-
tests re-run. For ever. Use Ctrl-C to exit the `jbuild watch` loop.
33-
34-
You can run those build, server, and test tasks separately. Run `jbuild`
35-
with no arguments to see what tasks are available, along with a short
36-
description of them.
15+
* `npm run build` - build the library from the CoffeeScript source
16+
* `npm run serve` - run a test server
17+
* `npm test` - run the tests
18+
* `npm run watch` - watch for source file changes, then `build`, then `test`
3719

3820

3921
GitHub usage

‎README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ your application is running in Cloud Foundry. These include:
4343
If these aren't set, the `getAppEnv()` API will still return useful values,
4444
as appropriate. This means you can use this package in your program and
4545
it will provide useful values when you're running in Cloud Foundry AND
46-
when you're running locally.
46+
when you're running locally. You can supply local versions of
47+
`VCAP_SERVICES` and/or `VCAP_APPLICATION` by using the `vcap` and `vcapFile`
48+
options described below.
4749

4850

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

9092
This option property is ignored if not running locally.
9193

94+
* `vcapFile` - provide the same function as the `vcap` option, but instead of
95+
setting the option value to an object, you set it to the name of a JSON
96+
file, which will be parsed and used as the `vcap` option value is used,
97+
as described above.
98+
99+
When both `vcap` and `vcapFile` options are provided, the values in `vcap`
100+
are ignored.
101+
102+
This option property is ignored if not running locally.
103+
92104
This function returns an object with the following properties:
93105

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

326+
**1.1.0** - 2018/04/18
327+
328+
- add the `vcapFile` option - [issue #31][]
329+
330+
[issue #31]: https://github.com/cloudfoundry-community/node-cfenv/issues/31
331+
314332
**1.0.4** - 2017/01/13
315333

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

344362

363+
contributing
364+
================================================================================
365+
366+
See the [CONTRIBUTING.md](CONTRIBUTING.md) doc for more information on
367+
contributing to this project.
368+
369+
345370
license
346371
================================================================================
347372

‎jbuild.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ tasks.test = ->
6868
ui: "bdd"
6969
reporter: "spec"
7070
slow: 300
71-
compilers: "coffee:coffee-script"
72-
require: "coffee-script/register"
71+
compilers: "coffee:coffeescript"
72+
require: "coffeescript/register"
7373

7474
options = for key, val of options
7575
"--#{key} #{val}"

‎lib-src/cfenv.coffee

+22
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class AppEnv
2929
catch
3030
@isLocal = true
3131

32+
@_getVcapFromFile(options) if @isLocal
33+
3234
@app = getApp @, options
3335
@services = getServices @, options
3436

@@ -38,6 +40,26 @@ class AppEnv
3840
@urls = getURLs @, options
3941
@url = @urls[0]
4042

43+
#-----------------------------------------------------------------------------
44+
_getVcapFromFile: (options) ->
45+
return if not options?.vcapFile
46+
47+
contents = null
48+
try
49+
contents = fs.readFileSync options.vcapFile, 'utf8'
50+
catch err
51+
console.log "error reading vcapFile '#{options.vcapFile}': #{err}; ignoring"
52+
return
53+
54+
vcap = null
55+
try
56+
vcap = JSON.parse contents
57+
catch err
58+
console.log "error parsing vcapFile '#{options.vcapFile}': #{err}; ignoring"
59+
return
60+
61+
options.vcap = vcap
62+
4163
#-----------------------------------------------------------------------------
4264
toJSON: ->
4365
{@app, @services, @isLocal, @name, @port, @bind, @urls, @url}

‎lib/cfenv.js

+28-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/server.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
{
2-
"name": "cfenv",
3-
"main": "./lib/cfenv",
4-
"description": "easy access to your Cloud Foundry application environment",
5-
"version": "1.0.4",
6-
"author": "pmuellr",
7-
"license": "Apache-2.0",
8-
"homepage": "https://github.com/cloudfoundry-community/node-cfenv",
2+
"name": "cfenv",
3+
"main": "./lib/cfenv",
4+
"description": "easy access to your Cloud Foundry application environment",
5+
"version": "1.1.0",
6+
"author": "pmuellr",
7+
"license": "Apache-2.0",
8+
"homepage": "https://github.com/cloudfoundry-community/node-cfenv",
99
"repository": {
10-
"type": "git",
11-
"url": "https://github.com/cloudfoundry-community/node-cfenv.git"
10+
"type": "git",
11+
"url": "https://github.com/cloudfoundry-community/node-cfenv.git"
12+
},
13+
"scripts": {
14+
"build": "jbuild build",
15+
"serve": "jbuild serve",
16+
"test": "jbuild test",
17+
"watch": "jbuild watch"
1218
},
1319
"dependencies": {
14-
"js-yaml": "3.7.x",
15-
"ports": "1.1.x",
16-
"underscore": "1.8.x"
20+
"js-yaml": "3.11.x",
21+
"ports": "1.1.x",
22+
"underscore": "1.8.x"
1723
},
1824
"devDependencies": {
19-
"coffee-script": "1.12.x",
20-
"mocha": "3.2.x",
21-
"expect.js": "0.3.x"
25+
"coffeescript": "1.12.x",
26+
"expect.js": "0.3.x",
27+
"jbuild": "1.0.x",
28+
"mocha": "5.1.x"
2229
}
2330
}

‎tests/fixtures/vcap-local-bad.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this file intentially does not contain valid JSON

‎tests/fixtures/vcap-local-good.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"services": {
3+
"service-a-label": [
4+
{
5+
"name": "service-a",
6+
"label": "service-a-label",
7+
"credentials": {
8+
"url": "foo"
9+
}
10+
}
11+
]
12+
}
13+
}

‎tests/test-core.coffee

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fs = require "fs"
44
path = require "path"
55

66
_ = require "underscore"
7-
coffee = require "coffee-script"
7+
coffee = require "coffeescript"
88
expect = require "expect.js"
99
ports = require "ports"
1010

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

245+
#-----------------------------------------------------------------------------
246+
it "local - with vcapFile option", ->
247+
248+
#-------------------------------------------
249+
vcapFile = path.join(__dirname, 'fixtures', 'vcap-local-good.json')
250+
251+
appEnv = cfenv.getAppEnv {vcapFile}
252+
creds = appEnv.getServiceCreds "service-a"
253+
creds = JSON.stringify(creds)
254+
expect(creds).to.be '{"url":"foo"}'
255+
256+
#-------------------------------------------
257+
vcapFile = path.join(__dirname, 'fixtures', 'vcap-local-bad.json')
258+
259+
console.log "expecting an error printed below:"
260+
appEnv = cfenv.getAppEnv {vcapFile}
261+
creds = appEnv.getServiceCreds "service-a"
262+
expect(creds).to.be null
263+
264+
#-------------------------------------------
265+
vcap = getVCAPServicesWithCreds "service-a",
266+
url: "bar"
267+
268+
vcapFile = path.join(__dirname, 'fixtures', 'vcap-local-good.json')
269+
270+
appEnv = cfenv.getAppEnv {vcap, vcapFile}
271+
creds = appEnv.getServiceCreds "service-a"
272+
creds = JSON.stringify(creds)
273+
expect(creds).to.be '{"url":"foo"}'
274+
245275
#-----------------------------------------------------------------------------
246276
it "remote - VCAP_APPLICATION", ->
247277

0 commit comments

Comments
 (0)
Please sign in to comment.