YQ Tool: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Created page with "{| | valign="top" | <syntaxhighlight lang="bash"> # ubuntu/debian cat << EXE|sudo bash PLATFORM=$(uname -s)_$(dpkg --print-architecture) YQ_BINARY=$(echo "yq_${PLATFORM}"|tr '[:upper:]' '[:lower:]') wget https://github.com/mikefarah/yq/releases/latest/download/${YQ_BINARY} -O /usr/bin/yq chmod +x /usr/bin/yq EXE </syntaxhighlight> | valign="top" | '''windows:''' 1. Press '''⊞ + R''' 2. Type in '''PowerShell''' '''run as administrator user''' 3. Press '''Ctrl + Sh..."
 
 
(12 intermediate revisions by the same user not shown)
Line 4: Line 4:
# ubuntu/debian
# ubuntu/debian
cat << EXE|sudo bash
cat << EXE|sudo bash
PLATFORM=$(uname -s)_$(dpkg --print-architecture)
PLATFORM=\$(uname -s)_\$(dpkg --print-architecture)
YQ_BINARY=$(echo "yq_${PLATFORM}"|tr '[:upper:]' '[:lower:]')
YQ_BINARY=\$(echo "yq_\${PLATFORM}"|tr '[:upper:]' '[:lower:]')
wget https://github.com/mikefarah/yq/releases/latest/download/${YQ_BINARY} -O /usr/bin/yq
wget https://github.com/mikefarah/yq/releases/latest/download/\${YQ_BINARY} -O /usr/bin/yq
chmod +x /usr/bin/yq
chmod +x /usr/bin/yq
EXE
EXE
Line 29: Line 29:
  brew install yq
  brew install yq


|-
| colspan="3" |
----
|-
| valign="top" colspan="3"|
<syntaxhighlight lang="bash">
    apt    info      moreutils
    apt    search    moreutils
sudo apt-get install -y moreutils
</syntaxhighlight>
|}
==YQ » JSON » YAML==
{|class='wikitable mw-collapsible mw-collapsed'
!scope='col' colspan='2' style='width:1000px'|
'''YQ » Convert » JSON » YAML'''
|-
|valign='top' style='width:490px'|
<syntaxhighlight lang="json">
yq -P <<'JSN'
{
"Version": "2012-10-17",
"Statement": [{
  "Action": [
      "admin:SetTier",
      "admin:ListTier" ],
  "Effect": "Allow",
  "Sid": "EnableRemoteTierManagement" },{
  "Action": [
      "s3:PutLifecycleConfiguration",
      "s3:GetLifecycleConfiguration" ],
  "Resource": [
      "arn:aws:s3:::*" ],
  "Effect": "Allow",
  "Sid": "EnableLifecycleManagementRules" }]}
JSN
</syntaxhighlight>
|valign='top' style='width:490px'|
<syntaxhighlight lang="yaml">
yq -o=json <<'YML'
---
Version: "2012-10-17"
Statement:
  - Action:
      - admin:SetTier
      - admin:ListTier
    Effect: Allow
    Sid: EnableRemoteTierManagement
  - Action:
      - s3:PutLifecycleConfiguration
      - s3:GetLifecycleConfiguration
    Resource:
      - arn:aws:s3:::*
    Effect: Allow
    Sid: EnableLifecycleManagementRules
YML
</syntaxhighlight>
|}
|}


==Knowledge==
==Playground==
{|class='wikitable mw-collapsible' style='width:100%'
!scope='col' style='width:100%'|
'''YQ » Playground'''
|-
|valign='top'|
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# append or replace ports
cat docker-compose.yml|yq -r '.services.proxy.ports'
yq eval '.services.proxy.ports += ["127.0.0.1:1980:8080"]' -i docker-compose.yml
yq eval '.services.proxy.ports  = ["127.0.0.1:1980:8080"]' -i docker-compose.yml
# read decoded docker password
yq -r '.auths."hub.chorke.org".auth' ~/.docker/config.json|base64 --decode && echo
yq -r '.auths."hub.chorke.org".auth' ~/.docker/config.json|base64 --decode && echo
yq -r '.auths."reg.chorke.org".auth' ~/.docker/config.json|base64 --decode && echo
yq -r '.auths."reg.chorke.org".auth' ~/.docker/config.json|base64 --decode && echo
# append extra_hosts for host.docker.internal
yq eval '.services.core.extra_hosts    += ["host.docker.internal:host-gateway"]' -i docker-compose.yml
yq eval '.services.registry.extra_hosts += ["host.docker.internal:host-gateway"]' -i docker-compose.yml
</syntaxhighlight>
----
<syntaxhighlight lang="bash">
cat ~/.docker/config.json|yq -P 'del(.auths["harbor.shahed.biz"])'
cat ~/.docker/config.json|jq -r 'del(.auths["harbor.shahed.biz"])'
cat ~/.docker/config.json|jq -r 'del(.auths["harbor.shahed.biz"])'|sponge ~/.docker/config.json
</syntaxhighlight>
----
<syntaxhighlight lang="bash">
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json | yq -P '.clients[]|select(.clientId == "web_app")'
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json | yq -P '.users[]  |select(.username == "admin").credentials'
</syntaxhighlight>
----
<syntaxhighlight lang="bash">
echo;\
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json \
| jq -r '.clients|sort_by(.clientId)|.[]
| [.id, .clientId, .rootUrl, .adminUrl, .enabled, .secret]|@tsv' \
| awk -F'\t' '{printf "%-36s %-24s %-24s %-24s %-5s %-36s\n", $1, $2, $3, $4, $5, $6}'
</syntaxhighlight>
----
<syntaxhighlight lang="bash">
echo;\
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json \
| jq -r '.users|sort_by(.username)|.[]
| [.id, .username, .firstName, .lastName, .enabled, (.credentials[0].secretData|fromjson|.value)]|@tsv' \
| awk -F'\t' '{printf "%-36s %-8s %-8s %-16s %-5s %-90s\n", $1, $2, $3, $4, $5, $6}'
</syntaxhighlight>
----
<syntaxhighlight lang="bash">
echo;\
REPOS_NAME=docker;\
IMAGE_NAME=sdm/stg/onboarding;\
curl -fsSL -u \
  "$(cat ~/.nexus-cli|jq -r .username):$(cat ~/.nexus-cli|jq -r .password)" \
  "$(cat ~/.nexus-cli|jq -r .url)/service/rest/v1/search?repository=${REPOS_NAME}&name=${IMAGE_NAME}" \
  | jq -r  '.items|sort_by(.assets[0].lastModified)|.[]
  | [.id, .name, .version, .assets[0].lastModified, .assets[0].lastDownloaded, .assets[0].uploader, .assets[0].uploaderIp, .assets[0].fileSize]|@tsv' \
  | awk -F'\t' '{printf "%-22s %-30s %-12s %-12s %-12s %-20s %-20s %-10s\n", $1, $2, $3, $4, $5, $6, $7, $8}'
</syntaxhighlight>
</syntaxhighlight>
|}


==References==
==References==
{|
{|
| valign="top" |
| valign="top" |
* [https://medium.com/how-tos-for-coders/https-medium-com-how-tos-for-coders-parse-json-data-using-jq-and-curl-from-command-line-5aa8a05cd79b Parse JSON data using JQ Tool]
* [https://github.com/mikefarah/yq/releases YQ » Releases]
* [https://stackoverflow.com/questions/42716734/ JSON file in-place using <code>jq</code>]
* [https://mikefarah.gitbook.io/yq YQ » Docs]
* [https://github.com/mikefarah/yq/releases YQ Tool » Releases]
* [https://github.com/mikefarah/yq YQ]
* [https://github.com/mikefarah/yq YQ Tool]
 
| valign="top" |
 
| valign="top" |
 
|-
| colspan="3" |
----
|-
| valign="top" |
* [[HTTPie]]
* [[JQ Tool]]
* [[JQ Tool]]
* [[HTTPie]]
* [[TMux]]
* [[TMux]]
* [[Wrk]]
* [[Wrk]]
| valign="top" |


| valign="top" |
| valign="top" |


|}
|}

Latest revision as of 21:36, 5 October 2025

# ubuntu/debian
cat << EXE|sudo bash
PLATFORM=\$(uname -s)_\$(dpkg --print-architecture)
YQ_BINARY=\$(echo "yq_\${PLATFORM}"|tr '[:upper:]' '[:lower:]')
wget https://github.com/mikefarah/yq/releases/latest/download/\${YQ_BINARY} -O /usr/bin/yq
chmod +x /usr/bin/yq
EXE
windows:
1. Press ⊞ + R
2. Type in PowerShell
run as administrator user
3. Press Ctrl + Shift + Enter
4. Choose Yes and Press Enter
5. choco install yq
macos:



brew doctor
brew update
brew install yq

     apt     info       moreutils
     apt     search     moreutils
sudo apt-get install -y moreutils

YQ » JSON » YAML

YQ » Convert » JSON » YAML

yq -P <<'JSN'
{
"Version": "2012-10-17",
"Statement": [{
   "Action": [
      "admin:SetTier",
      "admin:ListTier" ],
   "Effect": "Allow",
   "Sid": "EnableRemoteTierManagement" },{
   "Action": [
      "s3:PutLifecycleConfiguration",
      "s3:GetLifecycleConfiguration" ],
   "Resource": [
      "arn:aws:s3:::*" ],
   "Effect": "Allow",
   "Sid": "EnableLifecycleManagementRules" }]}
JSN
yq -o=json <<'YML'
---
Version: "2012-10-17"
Statement:
  - Action:
      - admin:SetTier
      - admin:ListTier
    Effect: Allow
    Sid: EnableRemoteTierManagement
  - Action:
      - s3:PutLifecycleConfiguration
      - s3:GetLifecycleConfiguration
    Resource:
      - arn:aws:s3:::*
    Effect: Allow
    Sid: EnableLifecycleManagementRules
YML

Playground

YQ » Playground

# append or replace ports
cat docker-compose.yml|yq -r '.services.proxy.ports'
yq eval '.services.proxy.ports += ["127.0.0.1:1980:8080"]' -i docker-compose.yml
yq eval '.services.proxy.ports  = ["127.0.0.1:1980:8080"]' -i docker-compose.yml

# read decoded docker password
yq -r '.auths."hub.chorke.org".auth' ~/.docker/config.json|base64 --decode && echo
yq -r '.auths."reg.chorke.org".auth' ~/.docker/config.json|base64 --decode && echo

# append extra_hosts for host.docker.internal
yq eval '.services.core.extra_hosts     += ["host.docker.internal:host-gateway"]' -i docker-compose.yml
yq eval '.services.registry.extra_hosts += ["host.docker.internal:host-gateway"]' -i docker-compose.yml

cat ~/.docker/config.json|yq -P 'del(.auths["harbor.shahed.biz"])'
cat ~/.docker/config.json|jq -r 'del(.auths["harbor.shahed.biz"])'
cat ~/.docker/config.json|jq -r 'del(.auths["harbor.shahed.biz"])'|sponge ~/.docker/config.json

cat ../gateway/src/main/docker/realm-config/jhipster-realm.json | yq -P '.clients[]|select(.clientId == "web_app")'
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json | yq -P '.users[]  |select(.username == "admin").credentials'

echo;\
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json \
 | jq -r '.clients|sort_by(.clientId)|.[]
 | [.id, .clientId, .rootUrl, .adminUrl, .enabled, .secret]|@tsv' \
 | awk -F'\t' '{printf "%-36s %-24s %-24s %-24s %-5s %-36s\n", $1, $2, $3, $4, $5, $6}'

echo;\
cat ../gateway/src/main/docker/realm-config/jhipster-realm.json \
 | jq -r '.users|sort_by(.username)|.[]
 | [.id, .username, .firstName, .lastName, .enabled, (.credentials[0].secretData|fromjson|.value)]|@tsv' \
 | awk -F'\t' '{printf "%-36s %-8s %-8s %-16s %-5s %-90s\n", $1, $2, $3, $4, $5, $6}'

echo;\
REPOS_NAME=docker;\
IMAGE_NAME=sdm/stg/onboarding;\
curl -fsSL -u \
  "$(cat ~/.nexus-cli|jq -r .username):$(cat ~/.nexus-cli|jq -r .password)" \
  "$(cat ~/.nexus-cli|jq -r .url)/service/rest/v1/search?repository=${REPOS_NAME}&name=${IMAGE_NAME}" \
  | jq -r  '.items|sort_by(.assets[0].lastModified)|.[]
  | [.id, .name, .version, .assets[0].lastModified, .assets[0].lastDownloaded, .assets[0].uploader, .assets[0].uploaderIp, .assets[0].fileSize]|@tsv' \
  | awk -F'\t' '{printf "%-22s %-30s %-12s %-12s %-12s %-20s %-20s %-10s\n", $1, $2, $3, $4, $5, $6, $7, $8}'

References