How to prevent comma in VimL string concatenation? - vim

I have an object defined like so:
let g:lightline = {
\ 'component': {
\ 'fugitive': '%{exists("*fugitive#statusline") ? "⎇ " . fugitive#statusline() : ""}'
\ },
\ }
The output of fugitive#statusline() is GIT(master), so the final string eventually appears in my statusline as ⎇ ,GIT(master) with a comma.
Why is there a comma? How can we avoid the comma?
I'm using lightline.vim to customize my status line, and the whole configuration looks like this:
let g:lightline = {
\ 'active': {
\ 'left': [
\ [ 'mode', 'paste' ],
\ [ 'filename', 'readonly', 'modified' ],
\ [ 'fugitive', ],
\ ]
\ },
\ 'inactive': {
\ 'left': [
\ [ 'filename', 'readonly', 'modified' ],
\ [ 'fugitive', ],
\ ]
\ },
\ 'component': {
\ 'readonly': '%{&readonly?"x":""}',
\ 'fugitive': '%{exists("*fugitive#statusline") ? "⎇ " . fugitive#statusline() . "" : ""}'
\ },
\ 'component_visible_condition': {
\ 'fugitive': '(exists("*fugitive#head") && ""!=fugitive#head())'
\ },
\ 'separator': { 'left': '', 'right': '' },
\ 'subseparator': { 'left': '|', 'right': '|' }
\ }

This code in the fugitive plugin either prepends a comma, or encloses the element in square braces. These two styles are also offered by the built-in statusline elements.
You can remove the undesired comma by taking only a substring ([1:]) off the result of the fugitive invocation:
'fugitive': '%{exists("*fugitive#statusline") ? "⎇ " . fugitive#statusline()[1:] : ""}'

Related

vim-startify configuration in lua with custom functions

I'm trying to migrate my vimrc configuration to lua, and I'm stuck on migrating my vim-startify configuration. In particular, how can I write the gitModified and gitUntraked lists?
vimscript:
function! s:gitModified()
let files = systemlist('git ls-files -m 2>/dev/null')
return map(files, "{'line': v:val, 'path': v:val}")
endfunction
function! s:gitUntracked()
let files = systemlist('git ls-files -o --exclude-standard 2>/dev/null')
return map(files, "{'line': v:val, 'path': v:val}")
endfunction
let g:startify_lists = [
\ { 'type': 'dir', 'header': [' MRU '. getcwd()] },
\ { 'type': 'sessions', 'header': [' Sessions'] },
\ { 'type': 'bookmarks', 'header': [' Bookmarks'] },
\ { 'type': function('s:gitModified'), 'header': [' git modified']},
\ { 'type': function('s:gitUntracked'), 'header': [' git untracked']},
\ { 'type': 'commands', 'header': [' Commands'] },
\ ]
My current lua:
vim.g.startify_lists = {
{ type = "commands", header = { " Commands" } }, -- Commands from above
{ type = "dir", header = { " MRU " .. vim.fn.getcwd() } }, -- MRU files from CWD
{ type = "sessions", header = {" Sessions"} },
{ type = "bookmarks", header = {" Bookmarks"} },
}
Here I'm missing the two git related items.
Any idea?
Thanks in advance.
Here's what I did:
function CommandToStartifyTable(command)
return function()
local cmd_output = vim.fn.systemlist(command .. " 2>/dev/null")
local files =
vim.tbl_map(
function(v)
return {line = v, path = v}
end,
cmd_output
)
return files
end
end
vim.g.startify_lists = {
{type = "dir", header = {" MRU " .. vim.fn.fnamemodify(vim.fn.getcwd(), ":t")}},
{type = CommandToStartifyTable("git ls-files -m"), header = {" Git modified"}},
{type = CommandToStartifyTable("git ls-files -o --exclude-standard"), header = {" Git untracked"}}
}

how to use loop with some conditions in shell script

I need to run a curl command several times, and check the result every time. I used a for loop to check the output from curl, and I want to exit the loop when the correct status is reached.
The very first time the curl command is run, it will show me this output :
{
"name": "organizations/org_name/operations/long_running_operation_ID",
"metadata": {
"#type": "type.googleapis.com/google.cloud.apigee.v1.OperationMetadata",
"operationType": "INSERT",
"targetResourceName": "organizations/org_name",
"state": "IN_PROGRESS"
}
}
It takes time to complete, that's why it says IN_PROGRESS. It took around 30 to 60 sec, and if we run the same curl command after 30 to 60 sec then it will show the output below :
{
"error": {
"code": 409,
"message": "org graceful-path-310004 already associated with another project",
"status": "ALREADY_EXISTS",
"details": [
{
"#type": "type.googleapis.com/google.rpc.RequestInfo",
"requestId": "11430211642328568827"
}
]
}
}
Here is the script I have so far :
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
echo $test
while [ $test = "ALREADY EXISTS" || $test != "IN_PROGRESS" ]
do
echo $test
echo "Organization is creating"
test=$(curl -H "Authorization: Bearer $TOKEN" -X POST -H "content-type:application/json" \
-d '{
"name":"'"$ORG_NAME"'",
"displayName":"'"$ORG_DISPLAY_NAME"'",
"description":"'"$ORGANIZATION_DESCRIPTION"'",
"runtimeType":"'"$RUNTIMETYPE"'",
"analyticsRegion":"'"$ANALYTICS_REGION"'"
}' \
"https://apigee.googleapis.com/v1/organizations?parent=projects/$PROJECT_ID" | jq '.error.status')
done
echo "exit from loop"
To reduce the duplicated code, use functions.
Also, best practice to use jq to generate json -- it will safely handle any embedded quotes in the data:
json() {
jq --arg name "$ORG_NAME" \
--arg displayName "$ORG_DISPLAY_NAME" \
--arg description "$ORGANIZATION_DESCRIPTION" \
--arg runtimeType "$RUNTIMETYPE" \
--arg analyticsRegion "$ANALYTICS_REGION" \
--null-input --compact-output \
'$ARGS.named'
}
errorStatus() {
curl -H "Authorization: Bearer $TOKEN" \
-X POST \
-H "content-type:application/json" \
-d "$(json)" \
"${URL}?parent=projects/$PROJECT_ID" \
| jq -r '.error.status'
}
while true; do
status=$(errorStatus)
[ "$status" = "ALREADY EXISTS" ] && break
done
echo "exit from loop"
The while true; do some code; condition && break; done bit is the bash version of a do-while loop

Getting "The body you sent contains an unknown key." when trying to create a Contentful entry

I have created a contentful model called "User" with two fields:
id - text, unique, required
email - text, optional
When I try to create a new entry via content management API using these parameters:
Headers:
X-Contentful-Content-Type : user
Content-Type : application/vnd.contentful.management.v1+json
Method
PUT
URL
https://api.contentful.com/spaces/qilo7tiaixh8/environments/entries/
Body:
{
"id":"whatever",
"email": "peter#petervukovic.com"
}
I get the following error:
{
"requestId": "2849bbcd7ee0486bb36b47927071f37b",
"sys": {
"type": "Error",
"id": "UnknownKey"
},
"message": "The body you sent contains an unknown key.",
"details": {
"errors": [
{
"keys": [
"id",
"email"
]
}
]
}
}
I have no idea what I'm doing wrong as the examples in the official documentation aren't helpful (they assume multi-lingual content I'm not using) and there are no debugging hints.
Contentful DevRel here. 👋
I just tried it and the following CURL works for me on a user content type that defines a title field.
curl --include \
--request PUT \
--header 'Authorization: Bearer ...' \
--header 'Content-Type: application/vnd.contentful.management.v1+json' \
--header 'X-Contentful-Content-Type: user' \
--data-binary '{
"fields": {
"title": {
"en-US": "Hello Test"
}
}
}' \
https://api.contentful.com/spaces/.../environments/master/entries/\test-1
It looks like you were missing to include the fields property in your payload. About the localization part, I think it's required to provide the locale for your field values. So in my example, en-US is the default value and it is required.
For using PUT you have to define or come up with an entry id.
To create an entry without passing and defining an id have a look at the docs in Entry collection.
curl --include \
--request POST \
--header 'Authorization: Bearer ...' \
--header 'Content-Type: application/vnd.contentful.management.v1+json' \
--header 'X-Contentful-Content-Type: user' \
--data-binary '{
"fields": {
"title": {
"en-US": "Hello Test 2"
}
}
}' \
https://api.contentful.com/spaces/.../environments/master/entries/

escape variable in bash script curl request

I am trying to curl via bash script but unable to pass the value of var1 in curl request and getting error upon execution ...
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d '
{"msgtype": "text",
"text": {
"content": $var1
}
}'
Your variable $var1 doesn't get expanded by the shell because it is inside single quote '.
You need to use double quote to let bash do the parameter expansion, and escape your json data:
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"$var1\"
}
}"
Or you can use inline document (without the escaping hell, but the command becomes awkward):
#!/bin/bash
var1="some test message"
curl 'https://link' \
-H 'Content-Type: application/json' \
-d "$(cat <<EOT
{
"msgtype": "text",
"text": {
"content": "$var1"
}
}
EOT
)"
In general, don't use parameter expansion to define JSON dynamically like this. There is no guarantee that your template, combined with the contents of the variable, produces well-formed JSON. (This is for the same reasons you don't use string interpolation to create dynamic SQL queries.) Use a tool like jq instead.
curl 'https://link' ... \
-d "$(jq --argjson x "$var" \
-n '{msgtype: "text", text: {content: $x}}')"
Use below script.
https://aaa.com' -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": { "content": '"'$var1'"' }}'

unable to create aws emr cluster with enable-debugging

I am not able to create aws emr cluster when I add command "--enable-debugging"
I am able to create the cluster without enable-debugging command.
Getting error like: aws: error: invalid json argument for option --configurations
my script to create cluster is :
aws emr create-cluster \
--name test-cluster \
--release-label emr-5.5.0 \
--instance-groups InstanceGroupType=MASTER,InstanceCount=1,InstanceType=m3.xlarge InstanceGroupType=CORE,InstanceCount=1,InstanceType=m3.xlarge \
--no-auto-terminate \
--termination-protected \
--visible-to-all-users \
--use-default-roles \
--log-uri s3://testlogs/ \
--enable-debugging \
--tags Owner=${OWNER} Environment=Dev Name=${OWNER}-test-cluster \
--ec2-attributes KeyName=$KEY,SubnetId=$SUBNET \
--applications Name=Hadoop Name=Pig Name=Hive \
--security-configuration test-sec-config \
--configurations s3://configurations/mapreduceconfig.json
mapreduceconfig.json file is :
[
{
"Classification": "mapred-site",
"Properties": {
"mapred.tasktracker.map.tasks.maximum": 2
}
},
{
"Classification": "hadoop-env",
"Properties": {},
"Configurations": [
{
"Classification": "export",
"Properties": {
"HADOOP_DATANODE_HEAPSIZE": 2048,
"HADOOP_NAMENODE_OPTS": "-XX:GCTimeRatio=19"
}
}
]
}
]
Well, the error is self apparent. --configurations option does not support S3:// file system. As per the examples and documentation
http://docs.aws.amazon.com/cli/latest/reference/emr/create-cluster.html
It only supports file:// and a direct public link to a file in S3. like https://s3.amazonaws.com/myBucket/mapreduceconfig.json
So, your configurations gotta be Public.
Not sure how you got it working without --enable-debugging command.

Resources