New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(xo-web/new-vm): ability to escape cloud config template variables #4501
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
153a6a4
fix eslint
badrAZ 2abe1a1
ability to respace template vars
badrAZ fb76fd1
update message
badrAZ 3ba0c55
CHANGELOG
badrAZ df564ce
comment
badrAZ 44c1579
comment
badrAZ 661c95e
revert eslint changes
badrAZ 69eca1e
better implementation
badrAZ e805c75
improvements
badrAZ a02fa0b
improvements
badrAZ 214bfc7
improvements
badrAZ 963c190
update README
badrAZ c154000
CHANGELOG
badrAZ 0b374a1
fixes
badrAZ d4c1d01
toto -> bar
badrAZ ad45305
test name
badrAZ e347cc8
CHANGELOG
badrAZ 0b75ef5
improvements
badrAZ d4dbaca
fix
badrAZ c0c7a50
improvements
badrAZ File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = require('../../@xen-orchestra/babel-config')( | ||
require('./package.json') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# @xen-orchestra/template [![Build Status](https://travis-ci.org/vatesfr/xen-orchestra.png?branch=master)](https://travis-ci.org/vatesfr/xen-orchestra) | ||
|
||
## Install | ||
|
||
Installation of the [npm package](https://npmjs.org/package/@xen-orchestra/template): | ||
|
||
``` | ||
> npm install --save @xen-orchestra/template | ||
``` | ||
|
||
## Usage | ||
|
||
Create a string replacer based on a pattern and a list of rules. | ||
|
||
```js | ||
const myReplacer = compileTemplate('{name}_COPY_\{name}_{id}_%\%', { | ||
'{name}': vm => vm.name_label, | ||
'{id}': vm => vm.id, | ||
'%': (_, i) => i | ||
}) | ||
|
||
const newString = myReplacer({ | ||
name_label: 'foo', | ||
id: 42, | ||
}, 32) | ||
|
||
newString === 'foo_COPY_{name}_42_32%' // true | ||
``` | ||
|
||
## Development | ||
|
||
``` | ||
# Install dependencies | ||
> yarn | ||
|
||
# Run the tests | ||
> yarn test | ||
|
||
# Continuously compile | ||
> yarn dev | ||
|
||
# Continuously run the tests | ||
> yarn dev-test | ||
|
||
# Build for production (automatically called by npm install) | ||
> yarn build | ||
``` | ||
|
||
## Contributions | ||
|
||
Contributions are *very* welcomed, either on the documentation or on | ||
the code. | ||
|
||
You may: | ||
|
||
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues) | ||
you've encountered; | ||
- fork and create a pull request. | ||
|
||
## License | ||
|
||
ISC © [Vates SAS](https://vates.fr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@xen-orchestra/template", | ||
"version": "0.0.0", | ||
"license": "ISC", | ||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/template", | ||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues", | ||
"repository": { | ||
"directory": "@xen-orchestra/template", | ||
"type": "git", | ||
"url": "https://github.com/vatesfr/xen-orchestra.git" | ||
}, | ||
"author": { | ||
"name": "Julien Fontanet", | ||
"email": "julien.fontanet@vates.fr" | ||
}, | ||
"preferGlobal": false, | ||
"main": "dist/", | ||
"files": [ | ||
"dist/" | ||
], | ||
"browserslist": [ | ||
">2%" | ||
], | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/preset-env": "^7.0.0", | ||
"cross-env": "^5.1.3", | ||
"rimraf": "^3.0.0" | ||
}, | ||
"scripts": { | ||
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/", | ||
"clean": "rimraf dist/", | ||
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/", | ||
"prebuild": "yarn run clean", | ||
"predev": "yarn run prebuild", | ||
"prepublishOnly": "yarn run build", | ||
"postversion": "npm publish" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.15" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import escapeRegExp from 'lodash/escapeRegExp' | ||
|
||
const compareLengthDesc = (a, b) => b.length - a.length | ||
|
||
export function compileTemplate(pattern, rules) { | ||
const matches = Object.keys(rules) | ||
.sort(compareLengthDesc) | ||
.map(escapeRegExp) | ||
.join('|') | ||
const regExp = new RegExp(`\\\\(?:\\\\|${matches})|${matches}`, 'g') | ||
return (...params) => | ||
pattern.replace(regExp, match => { | ||
if (match[0] === '\\') { | ||
return match.slice(1) | ||
} | ||
const rule = rules[match] | ||
return typeof rule === 'function' ? rule(...params) : rule | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-env jest */ | ||
import { compileTemplate } from '.' | ||
|
||
it("correctly replaces the template's variables", () => { | ||
const replacer = compileTemplate( | ||
'{property}_\\{property}_\\\\{property}_{constant}_%_FOO', | ||
{ | ||
'{property}': obj => obj.name, | ||
'{constant}': 1235, | ||
'%': (_, i) => i, | ||
} | ||
) | ||
expect(replacer({ name: 'bar' }, 5)).toBe('bar_{property}_\\bar_1235_5_FOO') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those changes are unrelated, leave them, but please avoid in the future.