0

I need to add some bash shell completion with words read from a json file:

...
{
    'bundle': 'R20_B1002_ORDERSB1_FROMB1',
    'version':'0.1',
    'envs': ['DEV','QUAL','PREPROD2'],
},
{
    'bundle': 'R201_QA069_ETIQETTENS_FROMSAP',
    'version': '0.1',
    'envs': ['DEV','QUAL','QUAL2','PREPROD'],
}
...

To get a words list I can run this command line and it returns all expected words from my file :

grep  'bundle' liste_routes.py | sed "s/'bundle': '//" | sed "s/',//" | grep -v '#' 

for instance, with an addtional "grep R20" it returns :

R20_B1002_ORDERSB1_FROMB1
R201_QA069_ETIQETTENS_FROMSAP
R202_LOG287_LIVRAISONSORTANTE_FROMLSP
R203_PP052_FULLSTOCKSAP_FROMSAP
R204_CO062_PRIXTRANSF_FROMOLGA
R206_LOG419_NOMENCLBOMPROD_FROMTDX
R207_CERTIFNFGAZ
R208_SAL363_ARTICLEPRICING_FROMSAP
R209_LOG451_WHSCON_FROMTDX

Now I put this in this compgen file and source it i my bash session.

_find_routenames()
{
search="$cur"
grep  'bundle' liste_routes.py | sed "s/'bundle': '//" | sed "s/',//" | sed "s/\r//g" |  grep -v '#' | awk '{$1=$1;print}'
}

_esbdeploy_completions()
{
#local IFS=$'\n'

COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
cur="${COMP_WORDS[1]}"
COMPREPLY=( $( compgen -W "$(_find_routenames)" -- "$cur" ) )

#####  COMPREPLY=($(compgen -W "$(grep  'bundle' liste_routes.py | sed \"s/'bundle': '//\" | sed \"s/',//\" | grep -v '#')" -- "${COMP_WORDS}"))
}

complete -F _esbdeploy_completions d.py
complete -F _esbdeploy_completions deploy_karaf_v4.py
complete -F _esbdeploy_completions show.py

The problem is when I type

./d.py R20<TAB>

I get thoses suggestions :

R201_QA069_ETIQETTENS_FROMSAP          R203_PP052_FULLSTOCKSAP_FROMSAP        R206_LOG419_NOMENCLBOMPROD_FROMTDX     R208_SAL363_ARTICLEPRICING_FROMSAP
R202_LOG287_LIVRAISONSORTANTE_FROMLSP  R204_CO062_PRIXTRANSF_FROMOLGA         R207_CERTIFNFGAZ                       R209_LOG451_WHSCON_FROMTDX

It misses R20_B1002_ORDERSB1_FROMB1 from my first grep test. I don't think it deals with the underscore , as other tests with "./d.py R10" do suggests "R10_xxxx".

Loko
  • 120
  • 9
  • `echo $COMP_WORDBREAKS` ? – KamilCuk Dec 03 '21 at 09:10
  • As an aside, that's a [useless use of `grep`.](https://www.iki.fi/era/unix/award.html#grep) The fragment `grep 'bundle' liste_routes.py | sed "s/'bundle': '//" | sed "s/',//" | sed "s/\r//g" | grep -v '#' | awk '{$1=$1;print}'` can be refactored to just something like `awk '/bundle/ && !/#/ { sub("\047bundle\047: \047", ""); sub("\047,", ""); sub("\r", ""); $1=$1; print }' liste_routes.py` (where I had to guess some things about what the input file looks like, but you get the idea). – tripleee Dec 03 '21 at 09:42
  • ... A more minimal fix would at the very least merge the multiple `sed` scripts into a single script (and probably lose the `grep`s by refactoring them into the `sed` script too). – tripleee Dec 03 '21 at 09:43
  • @KamiclCuk : it gives returns : "'@><=;|&(: – Loko Dec 03 '21 at 09:53
  • @tripleee : ok for thoses optimizations, mais they are only optimizations : I tried them and got the same wrong result. I put the JSON file format on the beginning of my question. – Loko Dec 03 '21 at 09:53
  • That's why it says "as an aside". – tripleee Dec 03 '21 at 09:54

0 Answers0