2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bashでメタプログラミング

2
Posted at

VagrantのConfig例。

vagrant-config.rb
Vagrant::Config.run do |config|
  config.vm.box = "precise64"
  config.vm.network :hostonly, "192.0.2.10"
end

bashで頑張ってみようとすると、第一ステップでは、こんな感じか。

bagrant-config.sh
# !/binb/bash
#
# requires:
#  bash
#
set -e

## functions

function Bargant::Config.run() {
  local param=$1

  eval "
    function ${param}() {
      echo "key : \${param}"
      echo "val : \${*}"
    }
  "

  local line
  while read line; do
    case "${line}" in
    ${param}.*)
      ${param} "${line##${param}.}"
      ;;
    esac
  done < <(cat)
}

## main / DSL

Bargant::Config.run config <<'EOS'
  config.vm.box = "precise64"
  config.vm.network :hostonly, "192.0.2.10"
EOS

実行結果。

$ bash ./bagrant-config.sh
key : config
val : vm.box = "precise64"
key : config
val : vm.network :hostonly, "192.0.2.10"

これを成長させれば、bashでも十分それっぽい事が可能になりそうか。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?