JavaScript: Configuration parsing

Last update: 12 Dec 23:20
0
Students

getForwardedVariables.js

Implement and export as default a function that accepts the contents of the configuration file as a string, finds environment variables in it that need to be passed and returns them as a string in the format "name1=value1,name2=value2,name3=value3,...".

Environment variables in the configuration file are set by the environment command, after which there is a comma-separated list of variables in quotation marks.

  environment="X_FORWARDED_MAIL=tirion@google.com,X_FORWARDED_HOME=/home/tirion,language=en"

Those variables that need to be forwarded start with the prefix X_FORWARDED_. Variable names in the output should be without this prefix. For example, if in the configuration file the variable is set like this: X_FORWARDED_HOME=/home/tirion, then in the output line it should look like this: "HOME=/home/tirion".

[program:prepare]
command=sudo -HEu tirion /bin/bash -c 'cd /usr/src/app && make prepare'
autorestart=false
environment="X_FORWARDED_MAIL=tirion@google.com,X_FORWARDED_HOME=/home/tirion,language=en"

[program:http_server]
command=sudo -HEu tirion /bin/bash -c 'cd /usr/src/app && make environment'
environment="key5=value5,X_FORWARDED_var3=value,key6=value6"
// Read the file contents and write it to the content constant. You don't need to implement this in your solution
const content = fs.readFileSync("s.conf", 'utf-8');

// Pass file contents to a function
const result = getForwardedVariables(content);
console.log(result); // => "MAIL=tirion@google.com,HOME=/home/tirion,var3=value"

Tips

  • Examples of configuration files can be found in the __fixtures__ directory

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests