I am using shelljs to excute shell command to find and replace content in my configuration file. My configuration is file content like bellow, I want to find all string "" by the name of my clinic (This is a parameter that i passed from the client like: var clinicName = req.body.clinicName;)
version: '3.2'
services:
# core backend service
core-backend:
image: "vany/multi-tenant-core:latest"
deploy:
replicas: 1
placement:
constraints: [node.labels.tenant==multi-tenant]
restart_policy:
condition: any
labels:
collectord.io/logs-eventpattern: '^\['
environment:
PORT: 5000
NODE_ENV: 'production'
DATABASE: 'mongodb://<tclinic-name>:yeuemdailau@10.117.134.9:27017/<tclinic-name>?authSource=<tclinic-name>'
volumes:
- /etc/localtime:/etc/localtime:ro
#volumes:
# media-upload:
# driver: lizardfs
If I using manual command like this, the result is correct.
sed -i "s/<tclinic-name>/hanoitclinic/g" clinic-api-core.yml
The result will be correct like this:
version: '3.2'
services:
# core backend service
core-backend:
image: "vany/multi-tenant-core:latest"
deploy:
replicas: 1
placement:
constraints: [node.labels.tenant==multi-tenant]
restart_policy:
condition: any
labels:
collectord.io/logs-eventpattern: '^\['
environment:
PORT: 5000
NODE_ENV: 'production'
DATABASE: **'mongodb://hanoitclinic:yeuemdailau@10.117.134.9:27017//hanoitclinic?authSource=hanoitclinic'**
volumes:
- /etc/localtime:/etc/localtime:ro
#volumes:
# media-upload:
# driver: lizardfs
But if I using shelljs, the code like this (with clinicName = "hanoitclinic" that I pass from client
var shell = require('shelljs');
shell.sed('-i', '<tclinic-name>', clinicName, "clinic-api-core.yml");
The result is not correct, it's only replace once:
DATABASE: 'mongodb://hanoitclinic:yeuemdailau@10.117.134.9:27017/<tclinic-name>?authSource=<tclinic-name>'
Please take a look. Thanks
According to example in the ShellJS documentation - and here - for
sed([options,] search_regex, replacement, file [, file ...])
the search_regex
can be a normal replace()
regexp so I'd suggest
using the g
lobal flag:
shell.sed('-i', /<tclinic-name>/g, clinicName, "aibolit-api-core.yml");