TypeError: JSON5.parse is not a function - json

I have installed JSON5 by npm install -d -g json5 and trying to parse the json string. But it is showing me [[object Object]]JSON5.parse is not a function. What the problem actually here is? and how we can resolve this? I am using the following code-
const JSON5 = require("json5")
var jsonV = JSON5.parse(fileStr)
Thanks!

Related

Gulp function to print out less verbose console log message?

I'm new to Gulp (or Grunt) tools. I created a function to print out the version value in my package.json file, like this
const {dest, parallel, series, src } = require('gulp');
const semver = require('semver');
const package_json = path.join(path.dirname(__filename),'package.json');
const pkg = JSON.parse(fs.readFileSync(package_json));
const version = semver.parse(pkg.version);
function get_version(cb) {
console.log(version.version);
cb();
}
exports.get_version = get_version
and I get
$ gulp get_version -f gulpfile-version.js
[16:07:08] Using gulpfile ~/some_path/gulpfile-version.js
[16:07:08] Starting 'get_version'...
1.4.6
[16:07:08] Finished 'get_version' after 1.91 ms
How can I modify my function to be less verbose and only print out the 1.4.6 string, and not the other time-stamped lines?
You can use --silent flag in your command to suppress the gulp logs.
Refer the doc
https://github.com/gulpjs/gulp/blob/master/docs/CLI.md
Your example
$ gulp get_version -f gulpfile-version.js --silent

How can I prettyprint JSON on the command line, but allow invalid JSON objects to pass though?

I'm currently tailing some logs in bash that are half JSON, half text like below:
{"response":{"message":"asdfasdf"}}
{"log":{"example":"asdfasdf"}}
here is some text
{"another":{"example":"asdfasdf"}}
more text
Each line is either a full valid JSON object or some text that would fail a JSON parser.
I've looked at jq and underscore-cli to see if they have options to return the invalid object in the case of failure, but I'm not seeing any.
I've also tried to use a || operator to cat the piped input, but I'm losing the value somehow. Maybe I should read up on pipes more? Example: getLogs -t | (underscore print || cat)
I think I could write a script that stores the input. Format it, and return the output if successful. If it fails returned the stored value. I feel like there should be a simpler way though. Any thoughts?
You can use this node library
install with
$ npm install -g js-beautify
Here is what I did:
$ js-beautify -r test.js
beautified test.js
I tested it with an incomplete json file and it worked
jq can check for invalid json
#!/bin/bash
while read p; do
if jq -e . >/dev/null 2>&1 <<<"$p"; then
echo $p | jq
else
echo 'Skipping invalid json'
fi
done < /tmp/tst.txt
{
"response": {
"message": "asdfasdf"
}
}
{
"log": {
"example": "asdfasdf"
}
}
Skipping invalid json
{
"another": {
"example": "asdfasdf"
}
}
Skipping invalid json

write all parameters jenkins in JSON file using ${params}

I'm a beginner with shell scripting and i have some issues while a jenkins job parametrized. I want to write all parameters of jenkins job pipeline build with parameters into a JSON file using ${params}!
In my case i have 4 parameters(apis:multi-select,name:string,version:single-select and status:Boolean), there is the script Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
script{
sh "./test.sh ${params}"
}
}
}
}
}
Content of test.sh
#!/bin/bash
echo $# > file.json
The output in jenkins
+ ./test.sh [apis:dev,qa,prod, name:CC, version:g3, status:true]
Result in file.json
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
My question is how format the output to obtain a clean result in file.json ? please i need help.
Add this to the top of your script:
import groovy.json.JsonOutput
Then use this line instead of sh "./test.sh ${params}":
writeFile file: 'params.json', text: JsonOutput.toJson(params)
This uses a Groovy library and a native Jenkins method for writing files, which means you don't need to use the sh method.

Haskell Yesod and writeFile

I am trying to learn Yesod and trying to implement a simple REST app where everytime a I get a GET request I write something to a file. Right now I have the following handler function:
getTestR =
do
return $ writeFile "test.txt" "Just something"
return $ object ["result" .= "Ok"]
What I was expecting is that the file test.txt would be created and I would obtain a JSON with {result=Ok}. However, I am obtaining the JSON, but the file is not being created.
I guess the writeFile is not being evaluated because of the lazy evaluation, but I have no idea how to overcome this problem. Thanks in advance.
just use liftIO:
getTestR =
do
liftIO $ writeFile "test.txt" "Just something"
return $ object ["result" .= "Ok"]

How to inject environment variables in Varnish configuration

I have 2 environments variables :
echo $FRONT1_PORT_8080_TCP_ADDR # 172.17.1.80
echo $FRONT2_PORT_8081_TCP_ADDR # 172.17.1.77
I want to inject them in a my default.vcl like :
backend front1 {
.host = $FRONT1_PORT_8080_TCP_ADDR;
}
But I got an syntax error on the $ char.
I've also tried with user variables but I can't define them outside vcl_recv.
How can I retrieve my 2 values in the VCL ?
I've managed to parse my vcl
backend front1 {
.host = ${FRONT1_PORT_8080_TCP_ADDR};
}
With a script:
envs=`printenv`
for env in $envs
do
IFS== read name value <<< "$env"
sed -i "s|\${${name}}|${value}|g" /etc/varnish/default.vcl
done
Now you can use the VMOD Varnish Standard Module (std) to get environment variables in the VCL, for example:
set req.backend_hint = app.backend(std.getenv("VARNISH_BACKEND_HOSTNAME"));
See documentation: https://varnish-cache.org/docs/trunk/reference/vmod_std.html#std-getenv
Note: it doesn't work for backend configuration, but could work elsewhere. Apparently backends are expecting constant strings and if you try, you'll get Expected CSTR got 'std.fileread'.
You can use the fileread function of the std module, and create a file for each of your environment variables.
before running varnishd, you can run:
mkdir -p /env; \
env | while read envline; do \
k=${envline%%=*}; \
v=${envline#*=}; \
echo -n "$v" >"/env/$k"; \
done
And then, within your varnish configuration:
import std;
...
backend front1 {
.host = std.fileread("/env/FRONT1_PORT_8080_TCP_ADDR");
.port = std.fileread("/env/FRONT1_PORT_8080_TCP_PORT");
}
I haven't tested it yet. Also, I don't know if giving a string to the port configuration of the backend would work. In that case, converting to an integer should work:
.port = std.integer(std.fileread("/env/FRONT1_PORT_8080_TCP_PORT"), 0);
You can use use echo to eval strings.
Usually you can do something like:
VAR=test # Define variables
echo "my $VAR string" # Eval string
But, If you have the text in a file, you can use "eval" to have the same behaviour:
VAR=test # Define variables
eval echo $(cat file.vcl) # Eval string from the given file
Sounds like a job for envsubst.
Just use standard env var syntax in your config $MY_VAR and ...
envsubst < myconfig.tmpl > myconfig.vcl
You can install with apt get install gettext in Ubuntu.