Skip to content

yaml file traversal by bash tab completion #1263

@shakibamoshiri

Description

@shakibamoshiri

Please describe your feature request.
It is about 3 months heavily I am using yq to parse, modify, check yaml files
I have to check many yaml files locally or on some servers (after ssh-ing into them)
Since I code in bash and create their tab-completion as well, I could create a tab-completion script and fulfill me needs.
My need was checking each object's value rather than cat those files and scroll the terminal

But yq has no option to select a file (e.g docker-compose -f file-name.yaml) explicitly

So I wish yq had these two features or at least the first one

  1. An option to select a file , a yaml file to parse
  2. Support tab-completion script to have this traversal feature ( I can contribute)
yq -f file.yml <TAB>

Describe the solution you'd like
Solution are

  1. Add -f or any other available letter in order the explicitly select a file (I solved this using an other script)
  2. Refactor your tab-completion (it is ready)

As an input

kind: database
type: source
project: project-name
salt: my-salt
encrypt:
  object:
    - .company.people.data
    - .company.employee.data
  file:
    - .company.people
    - .company.employee
decrypt:
  object:
    - .company.people.data
    - .company.employee.data
  file:
    - .company.people
    - .company.employee

And we run a command:

yq -f sample.yml .<TAB>

it could output

.decrypt.file.0    .decrypt.object.1  .encrypt.object.0  .project           
.decrypt.file.1    .encrypt.file.0    .encrypt.object.1  .salt              
.decrypt.object.0  .encrypt.file.1    .kind              .type

My solution for -f option

#!/bin/bash

yq $2 $1 ${@:3}

My solution for object traversal

cmd_name='yqi';   # $1
current_arg='';   # $2
previous_arg='';  # $3


yqi_path=$(which yqi);
yqi_short_opt=($(perl -lne 'print $& if /-[a-zA-Z0-9]\b/' <($yqi_path --help) | sort | uniq))
yqi_long_opt=($(perl -lne 'print $& if  /--[a-zA-Z0-9-]+\b/' <($yqi_path --help 2>&1) | sort | uniq));


_comp_yqi () {
    current_arg=${COMP_WORDS[$COMP_CWORD]};
    previous_arg=${COMP_WORDS[$COMP_CWORD-1]};

    case ${previous_arg} in
        yqi )
            files=($(echo *.yml *.json *.xml));
            COMPREPLY=(${files[@]});
        ;;

    esac

    case ${current_arg} in
        .* )
            keys=($(yq -o props ${COMP_WORDS[1]} | perl -lpe 's/^/./; s/ =.*$//g'));
            COMPREPLY=( $(egrep -o "${current_arg//./\\.}[^ ]+" <<< ${keys[@]}) );
        ;;
        [a-zA-Z0-9]* )
            COMPREPLY=( $(egrep -o "${current_arg//./\\.}[^ ]+" <<< ${files[@]}) );
        ;;
    esac
}

complete -o bashdefault -o default -o nospace -F _comp_yqi yqi

This tab-completion script is pretty short , but smart enough to give us list of keys to check their value.
Actually by any TAB hit - it parses the yaml file using yq -o props and generates a new list .

how to test and use

  1. save the script as yqi and give the right permission
  2. source the second script to activate Tab-Completion for first script yqi

here is a sceenshot of it
win-20220703_10o3541

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions