Skip to content

Commit

Permalink
設定ファイルを basename で指定するようにした
Browse files Browse the repository at this point in the history
fixed #31
  • Loading branch information
ochaochaocha3 committed Mar 29, 2015
1 parent b2fd24f commit 5b33b34
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'https://rubygems.org'
gem 'activesupport', '~> 4.2'
gem 'cinch', github: 'cinchrb/cinch', ref: 'e4e33ce154977168963e35067c22805d3677a8e4'
gem 'twitter', '~> 5.11'
gem 'lumberjack', '~> 1.0'
gem 'sysexits', '~> 1.2'

group :development, :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ GEM
http_parser.rb (0.6.0)
i18n (0.7.0)
json (1.8.2)
lumberjack (1.0.9)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (0.8.2)
Expand Down Expand Up @@ -126,6 +127,7 @@ DEPENDENCIES
activesupport (~> 4.2)
cinch!
coveralls
lumberjack (~> 1.0)
pry (~> 0.10)
rake (~> 10.4)
rspec (~> 3.1)
Expand Down
65 changes: 51 additions & 14 deletions lib/rgrb/exec/irc_bot.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# vim: fileencoding=utf-8

require 'lumberjack'
require 'cinch'
require 'optparse'
require 'sysexits'
Expand All @@ -12,8 +13,8 @@ module RGRB
module Exec
# IRC ボットの実行ファイルの処理を担うクラス
class IRCBot
# 設定ファイルの標準パス(相対パス)
DEFAULT_CONFIG_PATH = 'config/rgrb.yaml'
# 既定の設定 ID
DEFAULT_CONFIG_ID = 'rgrb'

# 新しい RGRB::Exec::IRCBot インスタンスを返す
# @param [String] rgrb_root_path RGRB のルートディレクトリの絶対パス
Expand All @@ -23,15 +24,24 @@ def initialize(rgrb_root_path, argv)
@argv = argv

@debug = false
@config_path = "#{@root_path}/#{DEFAULT_CONFIG_PATH}"
@config_id = DEFAULT_CONFIG_ID
@opt = new_opt_parser
@logger = new_logger
end

# プログラムを実行する
# @return [void]
def execute
@opt.parse!(@argv)
load_config

@logger.level =
if @debug
Lumberjack::Logger::DEBUG
else
Lumberjack::Logger::INFO
end

@config = load_config(@config_id, @root_path, @logger)
load_plugins
bot = new_bot

Expand All @@ -58,11 +68,27 @@ def print_error(message)
private :print_error

# 設定を読み込む
# @return [void]
def load_config
@config = RGRB::Config.load_yaml_file(@config_path)
# @param [String] config_id 設定 ID
# @param [String] root_path RGRB のルートディレクトリの絶対パス
# @param [Logger] logger ロガー
# @raise [ArgumentError] config_id に '../' が含まれる場合
# @return [Config]
def load_config(config_id, root_path, logger)
if config_id.include?('../')
fail(
ArgumentError,
"#{config_id}: ディレクトリトラバーサルの疑い"
)
end

yaml_path = "#{root_path}/config/#{config_id}.yaml"
RGRB::Config.load_yaml_file(yaml_path)

logger.warn("設定 #{config_id} を読み込みました")
rescue => e
print_error("設定ファイルの読み込みに失敗しました (#{e})")
logger.fatal('設定ファイルの読み込みに失敗しました')
logger.fatal(e)

Sysexits.exit(:config_error)
end
private :load_config
Expand Down Expand Up @@ -132,19 +158,18 @@ def new_opt_parser
opt.banner = <<EOS
使用法: #{opt.program_name} [オプション]
汎用ダイスボット RGRB - IRC ボット
汎用ボット RGRB - IRC ボット
EOS
opt.version = RGRB::VERSION

opt.separator('')
opt.separator('オプション:')

opt.on(
'-c',
'--config=CONFIG_FILE',
'設定ファイルとして CONFIG_FILE を読み込みます'
) do |config_file|
@config_path = config_file
'-c', '--config=CONFIG_ID',
'設定 CONFIG_ID を読み込みます'
) do |config_id|
@config_id = config_id
end

opt.on(
Expand All @@ -156,6 +181,18 @@ def new_opt_parser
end
end
private :new_opt_parser

# 新しいロガーを作り、設定して返す
# @return [Logger]
def new_logger
logger = Lumberjack::Logger.new($stdout)

logger.progname = self.class.to_s
logger.level = Lumberjack::Logger::INFO

logger
end
private :new_logger
end
end
end

0 comments on commit 5b33b34

Please sign in to comment.