3

I am developing a pre-prod server for a Symfony2 app

I made a little hook script to update all of my git repositories which contain a Symfony app.

# ...

# Save old database
DATABASE_NAME=`YAML Parser Command` 'app/config/parameters.yml' database_name
TODAY=`date +"%Y-%m-%d-%H:%M:%S"`
logger -t "Update website Symfony2" "Save SQL in $TODAY.sql"
mysqldump $DATABASE_NAME > sqlSave/$TODAY.sql 2>&1 | logger -t "Update website Symfony2"
# Update database with doctrine
php app/console doctrine:schema:update --force  2>&1 | logger -t "Update website Symfony2"

# ...

How can I have access to the database name on this script ?

My server is on Debian. Is there a package to parse YAML?

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
olive007
  • 760
  • 1
  • 12
  • 32
  • Would you be alright with adding a dependency on a scripting language? Many of them have YAML parsing libraries so you could call one of them and have it output the data you want from YAML. – Shane Madden Apr 11 '15 at 16:39

2 Answers2

3

You can parse yaml/json file directly in your shell/bash with niet.

Easy to install and easy to use:

$ pip install -U niet

Consider the following example:

$ cat dump.yaml
foo:
  bar:
    key: value
  baz:
    key: value
  tags:
    - one
    - two

You can parse this example file like this:

  $ niet dump.yaml foo.bar.key
  value
  $ for el in $(niet dump.yaml foo.tags); do echo ${el}; done
  one
  two

Niet have a good integration with shell and others bash like.

Niet yaml parser documentation, source code, and samples.

Herve
  • 127
  • 4
2

Depending on the structure of your app/config/parameters.yml and the location of database_name within it, you might be able to use this parser. It will parse

foo:
  bar:
    key: value
  baz:
    key: value

into bash associative arrays. 100% Bash, but it needs to be Bash 4.x. If you can't stick to Bash 4.x then there are other answers on this question that may help you.

Community
  • 1
  • 1
starfry
  • 9,273
  • 7
  • 66
  • 96