LoginSignup
3

More than 5 years have passed since last update.

groongaコマンドをPryの上で

Posted at

Pry上でgroongaコマンドを動かすgroonga-pry.rbというのを作った。こんなことができる。

  • Pryの機能が使える
    • 行編集、ヒストリ機能、カラー表示
    • その他いろいろ
  • groongaコマンドの機能が使える
    • コマンドラインは同じ(そのままgroongaコマンドに渡している)
    • オプションに合わせたそれなりな補完ができる(……はず)
    • コマンド出力をJSON処理する
  • jqコマンドで整形できる
    • 最後に実行したgroongaコマンドの結果を整形して表示する
    • jqコマンドのフィルタの指定ができる

groongaコマンドの出力はJSON.parseで処理する。(dumpを除く。) 処理して得られた値を_に設定する。なお、もともとの出力であるテキストは_.to_sで得られる。

実行例:


$ ./groonga-pry.rb intro.db
[1] groonga(main)> status
=> [[0, 1417874110.88637, 6.31809234619141e-05],
 {"alloc_count"=>147,
  "starttime"=>1417874108,
  "uptime"=>2,
  "version"=>"4.0.7",
  "n_queries"=>0,
  "cache_hit_rate"=>0.0,
  "command_version"=>1,
  "default_command_version"=>1,
  "max_command_version"=>2}]
[2] groonga(main)> _[1]['alloc_count']
=> 147
[3] groonga(main)> _
=> 147

jqコマンドには直接JSONテキストを与えることもできるが、特に指定しなければ最後に実行したgroongaコマンドの出力を対象として処理を行う。


[4] groonga(main)> jq
[
  [
    0,
    1417874110.88637,
    6.31809234619141e-05
  ],
  {
    "alloc_count": 147,
    "starttime": 1417874108,
    "uptime": 2,
    "version": "4.0.7",
    "n_queries": 0,
    "cache_hit_rate": 0,
    "command_version": 1,
    "default_command_version": 1,
    "max_command_version": 2
  }
]
[5] groonga(main)> jq .[1].alloc_count
147

jqコマンドのオプションは指定できない。指定できたほうがいいのかも。

groonga-pry.rbの設定は~/.groonga-pryrcで行う。~/.pryrcではない。ヒストリも~/.groonga-pry_historyとpryとは別になるようにしている。

作りながらRroongaで実装したほうが良いかもと少し考えたんだけど、裏でgroongaコマンドを動かす形にもそれなりに意味はありそうなのでそのまま突き進んだ。こう、なんというか、Pryの勉強に、なった?

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
3