LoginSignup
0
0

More than 5 years have passed since last update.

rubyでyamlファイルの記述を出力し確認したい時

Last updated at Posted at 2017-09-26

こんな時に便利

  • ymlの設定ファイルでマージ(継承?)の記述がちゃんと書けているのか確認したい時、出力できると便利
  • 後述するサンプルファイルであれば出力せずともイメージできるのですが、ネストが深いとかファイル自体がそもそも長いとかってなると出力できると便利です

サンプルのymlファイル

sample.yml
default: &default
  host: '10.0.0.1'
  port: 8080
  content_type: 'application/json'
  account:
    name: 'default'
    password: 'default'


test: &test
  <<: *default
  account:
    name: 'admin'
    password: 'admin'

# development環境設定はtest環境設定を使いまわす
development:
  <<: *test

staging:
  <<: *default

production:
  <<: *default

コード

yml_to_json_print.rb
require 'pp'
require 'yaml'
require 'json'
require 'optparse'


argv_options = ARGV.getopts('f:', 'file:')
file_path    = argv_options['f'] || argv_options['file']
yaml         = YAML.load_file(file_path)

puts JSON.pretty_generate(yaml)

出力結果

$ ruby yaml_to_json_print.rb -f sample.yml
{
  "default": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "default",
      "password": "default"
    }
  },
  "test": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "admin",
      "password": "admin"
    }
  },
  "development": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "admin",
      "password": "admin"
    }
  },
  "staging": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "default",
      "password": "default"
    }
  },
  "production": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "default",
      "password": "default"
    }
  }
}

追記

  • @iyuuyaさんに教えていただいたto_yamlメソッドも加えてみました。JSONとお好みで。

追記2

  • @iyuuyaさんに教えていただいたto_yamlメソッドを使用して確認をしていたところ、マージのしかたによっては下記のような表示になりました。
  • JSON形式であれば問題無いようです。
  • 本文のサンプルyamlファイル、コード、出力結果の項目をJSON一本に戻しました。
  • 原因はまだ調べていません。どなたか教えて!
コード
require 'pp'
require 'yaml'
require 'json'
require 'optparse'


argv_options = ARGV.getopts('f:t:', 'file:type:')
type         = argv_options['t'] || argv_options['type']
file_path    = argv_options['f'] || argv_options['file']
yaml         = YAML.load_file(file_path)

case type
  when 'yml' || 'yaml'
    puts yaml.to_yaml
  when 'json'
    puts JSON.pretty_generate(yaml)
  else
    puts yaml.to_yaml
end
サンプルyamlファイル
sample.yml
default: &default
  host: '10.0.0.1'
  port: 8080
  content_type: 'application/json'
  account:
    name: 'default'
    password: 'default'


test: &test
  <<: *default
  account:
    name: 'admin'
    password: 'admin'

# development環境設定はtest環境設定を使いまわす
development:
  <<: *test

staging:
  <<: *default

production:
  <<: *default

出力結果
$ ruby yaml_to_json_print.rb -f sample.yml -type yml
---
default:
  host: 10.0.0.1
  port: 8080
  content_type: application/json
  account: &2
    name: default
    password: default
test:
  host: 10.0.0.1
  port: 8080
  content_type: application/json
  account: &1
    name: admin
    password: admin
development:
  host: 10.0.0.1
  port: 8080
  content_type: application/json
  account: *1
staging:
  host: 10.0.0.1
  port: 8080
  content_type: application/json
  account: *2
production:
  host: 10.0.0.1
  port: 8080
  content_type: application/json
  account: *2
$ ruby yaml_to_json_printer.rb -f sample.yml -t json
{
  "default": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "default",
      "password": "default"
    }
  },
  "test": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "admin",
      "password": "admin"
    }
  },
  "development": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "admin",
      "password": "admin"
    }
  },
  "staging": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "default",
      "password": "default"
    }
  },
  "production": {
    "host": "10.0.0.1",
    "port": 8080,
    "content_type": "application/json",
    "account": {
      "name": "default",
      "password": "default"
    }
  }
}

0
0
2

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
0
0