Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow erb in yaml #2

Merged
merged 1 commit into from Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -54,7 +54,7 @@ Usage: alerty [options] -- command
--lock LOCK_FILE exclusive lock file to prevent running a command duplicatedly (default: no lock)
--retry-limit NUMBER number of retries (default: 0)
--retry-wait SECONDS retry interval = retry wait +/- 12.5% randomness (default: 1.0)
-d, --debug debug mode (same with --log-level debug)
-d, --debug debug mode
```

## Plugins
Expand Down
2 changes: 1 addition & 1 deletion example.yml
Expand Up @@ -5,6 +5,6 @@ lock_path: log/lock
plugins:
- type: stdout
- type: file
path: log/file.log
path: log/<%= ENV['FILE'] || 'file' %>.log
- type: exec
command: cat > log/exec.log
4 changes: 2 additions & 2 deletions lib/alerty/cli.rb
Expand Up @@ -43,8 +43,8 @@ def parse_options(argv = ARGV)
op.on('--retry-wait SECONDS', "retry interval = retry wait +/- 12.5% randomness (default: 1.0)") {|v|
opts[:retry_wait] = v.to_f
}
op.on('-d', '--debug', "debug mode (same with --log-level debug)") {|v|
opts[:log_level] = 'debug'
op.on('-d', '--debug', "debug mode") {|v|
opts[:debug] = true
}

op.parse!(argv)
Expand Down
14 changes: 13 additions & 1 deletion lib/alerty/config.rb
@@ -1,4 +1,5 @@
require 'yaml'
require 'erb'
require 'hashie/mash'

class Alerty
Expand All @@ -15,7 +16,14 @@ def config_path
end

def config
@config ||= Hashie::Mash.new(YAML.load_file(config_path))
@config ||=
begin
content = File.read(config_path)
erb = ERB.new(content, nil, '-')
erb_content = erb.result
puts erb_content if debug?
Hashie::Mash.new(YAML.load(erb_content))
end
end

def log_path
Expand Down Expand Up @@ -50,6 +58,10 @@ def retry_wait
opts[:retry_wait] || config.retry_wait || 1.0
end

def debug?
!!opts[:debug]
end

def retry_interval
@random ||= Random.new
randomness = retry_wait * 0.125
Expand Down