├── .gitignore ├── .rspec ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── dashdog.gemspec ├── exe └── dashdog ├── lib ├── dashdog.rb └── dashdog │ ├── actions.rb │ ├── cli.rb │ ├── client.rb │ ├── converter.rb │ ├── dsl_context.rb │ ├── logger.rb │ ├── utils.rb │ └── version.rb └── spec ├── dashdog_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | /vendor 11 | .envrc 12 | Boardfile 13 | /*.rb 14 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.1.5 5 | before_install: gem install bundler -v 1.12.5 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.4.0 2 | 3 | - Add `exclude-title` option [#12][] ([@aibou][]) 4 | 5 | ## 0.3.4 6 | 7 | - Stop warning when execute `dashdog apply` [#11][] ([@aibou][]) 8 | 9 | ## 0.3.3 10 | 11 | - Exclude 'created_by' key because it is always overwritten in owner info of API key [#10][] ([@aibou][]) 12 | 13 | ## 0.3.2 14 | 15 | - Bugfix: `delete timeboard` API returns 204 when the deletion was successful [#9][] ([@aibou][]) 16 | - Decrease the `get (time|screen)boards` API parallelism 17 | - Fix some messages 18 | 19 | ## 0.3.1 20 | 21 | - Bugfix: Fix nil exception when widgets is empty [#8][] ([@n0ts][]) 22 | - Bugfix: The screenboard will be created at dryrun [#7][] ([@n0ts][]) 23 | 24 | ## 0.3.0 25 | 26 | - Add template support [#5][] ([@winebarrel][]) 27 | - Diff the sorted json [#6][] ([@winebarrel][]) 28 | 29 | ## 0.2.0 30 | 31 | - Add `split` opton [#4][] ([@winebarrel][]) 32 | 33 | ## 0.1.1 34 | 35 | - Add color option / check stdout tty [#3][] ([@winebarrel][]) 36 | - Use parallel for speeding up [#2][] ([@winebarrel][]) 37 | - Bugfix: Error occurs when widgets is nil. [#1][] ([@winebarrel][]) 38 | - Fix a strange diff colorize 39 | 40 | ## 0.1.0 41 | 42 | - Initial release 43 | 44 | 45 | [#1]: https://github.com/serverworks/dashdog/issues/1 46 | [#2]: https://github.com/serverworks/dashdog/issues/2 47 | [#3]: https://github.com/serverworks/dashdog/issues/3 48 | [#4]: https://github.com/serverworks/dashdog/issues/4 49 | [#5]: https://github.com/serverworks/dashdog/issues/5 50 | [#6]: https://github.com/serverworks/dashdog/issues/6 51 | [#7]: https://github.com/serverworks/dashdog/issues/7 52 | [#8]: https://github.com/serverworks/dashdog/issues/8 53 | [#9]: https://github.com/serverworks/dashdog/issues/9 54 | [#10]: https://github.com/serverworks/dashdog/issues/10 55 | [#11]: https://github.com/serverworks/dashdog/issues/11 56 | [#12]: https://github.com/serverworks/dashdog/issues/12 57 | [@aibou]: https://github.com/aibou 58 | [@n0ts]: https://github.com/n0ts 59 | [@winebarrel]: https://github.com/winebarrel -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in dashdog.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Serverworks Co.,Ltd. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dashdog 2 | 3 | [![Gem Version](https://badge.fury.io/rb/dashdog.svg)](https://badge.fury.io/rb/dashdog) 4 | 5 | Datadog dashboards management tool with Ruby DSL. 6 | 7 | ## Installation 8 | 9 | Add this line to your application's Gemfile: 10 | 11 | ```ruby 12 | gem 'dashdog' 13 | ``` 14 | 15 | And then execute: 16 | 17 | $ bundle 18 | 19 | Or install it yourself as: 20 | 21 | $ gem install dashdog 22 | 23 | ## Usage 24 | 25 | Please set your API,Application key in the environment variables. 26 | 27 | ```sh 28 | export DD_API_KEY= 29 | export DD_APP_KEY= 30 | ``` 31 | 32 | Available commands: 33 | 34 | ``` 35 | Commands: 36 | dashdog apply # Apply the dashboard configurations 37 | dashdog export # Export the dashboard configurations 38 | dashdog help [COMMAND] # Describe available commands or one specific command 39 | 40 | Options: 41 | -f, [--file=FILE] # Configuration file 42 | # Default: Boardfile 43 | [--color], [--no-color] # Disable colorize 44 | # Default: true 45 | ``` 46 | 47 | ## Commands 48 | 49 | ### export 50 | Export the dashboard configurations 51 | 52 | ```sh 53 | Usage: 54 | dashdog export 55 | 56 | Options: 57 | -w, [--write], [--no-write] # Write the configuration to the file 58 | [--split], [--no-split] # Split configuration file 59 | -f, [--file=FILE] # Configuration file 60 | # Default: Boardfile 61 | [--color], [--no-color] # Disable colorize 62 | # Default: true 63 | ``` 64 | 65 | ### apply 66 | Apply the dashboard configurations 67 | 68 | ```sh 69 | Usage: 70 | dashdog apply 71 | 72 | Options: 73 | -d, [--dry-run], [--no-dry-run] # Dry run (Only display the difference) 74 | -f, [--file=FILE] # Configuration file 75 | # Default: Boardfile 76 | [--color], [--no-color] # Disable colorize 77 | # Default: true 78 | -e, [--exclude-title=EXCLUDE_TITLE] # Exclude patterns of title 79 | ``` 80 | 81 | ## Development 82 | 83 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 84 | 85 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 86 | 87 | ## Contributing 88 | 89 | Bug reports and pull requests are welcome on GitHub at https://github.com/serverworks/dashdog. 90 | 91 | ## License 92 | 93 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 94 | 95 | ## Copyright 96 | 97 | Copyright (c) 2016-2018 Serverworks Co.,Ltd. See [LICENSE](https://github.com/serverworks/dashdog/blob/master/LICENSE.txt) for details. 98 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "dashdog" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /dashdog.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'dashdog/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "dashdog" 8 | spec.version = Dashdog::VERSION 9 | spec.authors = ["Serverworks Co.,Ltd."] 10 | spec.email = ["terui@serverworks.co.jp"] 11 | 12 | spec.summary = %q{Datadog dashboards management tool with Ruby DSL.} 13 | spec.description = %q{Datadog dashboards management tool with Ruby DSL.} 14 | spec.homepage = "https://github.com/serverworks/dashdog" 15 | spec.license = "MIT" 16 | 17 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 18 | spec.bindir = "exe" 19 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 20 | spec.require_paths = ["lib"] 21 | 22 | spec.add_dependency "dogapi" 23 | spec.add_dependency "dslh", "~> 0.3.9" 24 | spec.add_dependency "thor", "~> 0.19.1" 25 | spec.add_dependency "coderay" 26 | spec.add_dependency "diffy" 27 | spec.add_dependency "parallel" 28 | spec.add_dependency "hashie" 29 | 30 | spec.add_development_dependency "bundler", "~> 1.12" 31 | spec.add_development_dependency "rake", "~> 10.0" 32 | spec.add_development_dependency "rspec", "~> 3.0" 33 | end 34 | -------------------------------------------------------------------------------- /exe/dashdog: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | $: << File.expand_path('../../lib', __FILE__) 4 | 5 | require 'dashdog' 6 | 7 | Dashdog::Cli.start(ARGV) 8 | -------------------------------------------------------------------------------- /lib/dashdog.rb: -------------------------------------------------------------------------------- 1 | require "dashdog/version" 2 | require "dashdog/logger" 3 | require "dashdog/utils" 4 | 5 | require "dashdog/client" 6 | require "dashdog/converter" 7 | require "dashdog/actions" 8 | require "dashdog/cli" 9 | require "dashdog/dsl_context" 10 | 11 | module Dashdog 12 | # Your code goes here... 13 | end 14 | -------------------------------------------------------------------------------- /lib/dashdog/actions.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | require 'json' 3 | 4 | module Dashdog 5 | class Actions 6 | include Dashdog::Logger::Helper 7 | 8 | def initialize 9 | @client = Dashdog::Client.new 10 | @converter = Dashdog::Converter.new 11 | end 12 | 13 | def export(options) 14 | dsl = @converter.timeboards_to_dsl(@client.get_timeboards) 15 | dsl << @converter.screenboards_to_dsl(@client.get_screenboards) 16 | if options['write'] 17 | _export_to_file(dsl, options) 18 | else 19 | Dashdog::Utils.print_ruby(dsl, color: options[:color]) 20 | end 21 | end 22 | 23 | def apply(options) 24 | dry_run = options['dry_run'] ? '[Dry run] ' : '' 25 | conf = @converter.to_h(options['file']) 26 | 27 | _apply_timeboards(conf['timeboards'], @client.get_timeboards, dry_run, options) 28 | _apply_screenboards(conf['screenboards'], @client.get_screenboards, dry_run, options) 29 | end 30 | 31 | private 32 | 33 | def _apply_timeboards(local, remote, dry_run, options) 34 | local.each do |l| 35 | next if !options['exclude_title'].nil? && l['title'].match(options['exclude_title']) 36 | r = _choice_by_title(remote, l['title']) 37 | if r.nil? 38 | info("#{dry_run}Create the new timeboard '#{l['title']}'") 39 | @client.create_timeboard(l) if dry_run.empty? 40 | else 41 | l['id'] = r['id'] 42 | ['created', 'modified', 'created_by'].each do |field| 43 | r.delete(field) 44 | l.delete(field) 45 | end 46 | if l == r 47 | info("#{dry_run}No changes '#{l['title']}'") 48 | else 49 | warn("#{dry_run}Update the timeboard '#{l['title']}'") 50 | STDERR.puts Dashdog::Utils.diff(r, l) 51 | @client.update_timeboard(l) if dry_run.empty? 52 | end 53 | end 54 | end 55 | 56 | remote.each do |r| 57 | next if !options['exclude_title'].nil? && r['title'].match(options['exclude_title']) 58 | if _choice_by_title(local, r['title']).nil? 59 | warn("#{dry_run}Delete the timeboard '#{r['title']}'") 60 | @client.delete_timeboard(r['id']) if dry_run.empty? 61 | end 62 | end 63 | end 64 | 65 | def _apply_screenboards(local, remote, dry_run, options) 66 | local.each do |l| 67 | next if !options['exclude_title'].nil? && l['title'].match(options['exclude_title']) 68 | r = _choice_by_title(remote, l['board_title']) 69 | if r.nil? 70 | info("#{dry_run}Create the new screenboards '#{l['board_title']}'") 71 | @client.create_screenboard(l) if dry_run.empty? 72 | else 73 | l['id'] = r['id'] 74 | ['created', 'modified', 'created_by'].each do |field| 75 | r.delete(field) 76 | l.delete(field) 77 | end 78 | widgets = r['widgets'] || [] 79 | r['widgets'] = [] 80 | widgets.each do |wd| 81 | wd.delete('board_id') 82 | r['widgets'] << wd 83 | end 84 | l['widgets'] = [] if widgets.empty? 85 | if l == r 86 | info("#{dry_run}No changes '#{l['board_title']}'") 87 | else 88 | warn("#{dry_run}Update the screenboard '#{l['board_title']}'") 89 | STDERR.puts Dashdog::Utils.diff(r, l) 90 | @client.update_screenboard(l) if dry_run.empty? 91 | end 92 | end 93 | end 94 | 95 | remote.each do |r| 96 | next if !options['exclude_title'].nil? && r['title'].match(options['exclude_title']) 97 | if _choice_by_title(local, r['board_title']).nil? 98 | warn("#{dry_run}Delete the screenboard '#{r['board_title']}'") 99 | @client.delete_screenboard(r['id']) if dry_run.empty? 100 | end 101 | end 102 | end 103 | 104 | def _choice_by_title(boards, title) 105 | boards.each do |b| 106 | return b if b['title'] == title 107 | return b if b['board_title'] == title 108 | end 109 | nil 110 | end 111 | 112 | def _export_to_file(dsl, options) 113 | file = options['file'] 114 | 115 | if options['split'] 116 | dsls = dsl.strip.split(/^(timeboard|screenboard)\b/).slice(1..-1).each_slice(2).map(&:join) 117 | requires = [] 118 | 119 | dsls.each do |splitted| 120 | splitted.strip! 121 | title = splitted.each_line.first.strip.gsub(/\A(?:timeboard|screenboard)\s+"([^"]+)"\s+do/, '\\1') 122 | title.gsub!(/\W+/, '_') 123 | requires << title 124 | File.write("#{title}.rb", splitted + "\n") 125 | info("Write '#{title}.rb'") 126 | end 127 | 128 | open(file, 'w') do |f| 129 | requires.each {|r| f.puts "require #{r.inspect}" } 130 | end 131 | 132 | info("Write '#{file}'") 133 | else 134 | File.write(file, dsl) 135 | info("Write '#{file}'") 136 | end 137 | end 138 | end 139 | end 140 | -------------------------------------------------------------------------------- /lib/dashdog/cli.rb: -------------------------------------------------------------------------------- 1 | require 'thor' 2 | 3 | module Dashdog 4 | class Cli < Thor 5 | class_option :file, aliases: '-f', desc: 'Configuration file', type: :string, default: 'Boardfile' 6 | class_option :color, desc: 'Disable colorize', type: :boolean, default: $stdout.tty? 7 | class_option :exclude_title, aliases: '-e', desc: 'Exclude patterns of title', type: :string, default: nil 8 | 9 | def initialize(*args) 10 | @actions = Dashdog::Actions.new 11 | super(*args) 12 | end 13 | 14 | desc "export", "Export the dashboard configurations" 15 | option :write, aliases: '-w', desc: 'Write the configuration to the file', type: :boolean, default: false 16 | option :split, desc: 'Split configuration file', type: :boolean, default: false 17 | def export 18 | @actions.export(options) 19 | end 20 | 21 | desc "apply", "Apply the dashboard configurations" 22 | option :dry_run, aliases: '-d', desc: 'Dry run (Only display the difference)', type: :boolean, default: false 23 | def apply 24 | @actions.apply(options) 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/dashdog/client.rb: -------------------------------------------------------------------------------- 1 | require 'dogapi' 2 | require 'parallel' 3 | 4 | module Dashdog 5 | class Client 6 | 7 | def initialize 8 | @api = Dogapi::Client.new(ENV['DD_API_KEY'], ENV['DD_APP_KEY']) 9 | end 10 | 11 | def get_timeboards 12 | ret = [] 13 | Parallel.each(@api.get_dashboards[1]['dashes'], in_threads: 4) do |bd| 14 | ret << @api.get_dashboard(bd['id'])[1]['dash'] 15 | end 16 | return ret 17 | end 18 | 19 | def get_screenboards 20 | ret = [] 21 | Parallel.each(@api.get_all_screenboards[1]['screenboards'], in_threads: 4) do |bd| 22 | ret << @api.get_screenboard(bd['id'])[1] 23 | end 24 | return ret 25 | end 26 | 27 | def create_timeboard(tb) 28 | ret = @api.create_dashboard( 29 | tb['title'], 30 | tb['description'], 31 | tb['graphs'], 32 | tb['template_variables']) 33 | raise RuntimeError, ret[1]['errors'] if ret[0] != '200' 34 | end 35 | 36 | def update_timeboard(tb) 37 | ret = @api.update_dashboard( 38 | tb['id'], 39 | tb['title'], 40 | tb['description'], 41 | tb['graphs'], 42 | tb['template_variables']) 43 | raise RuntimeError, ret[1]['errors'] if ret[0] != '200' 44 | end 45 | 46 | def delete_timeboard(id) 47 | ret = @api.delete_dashboard(id) 48 | raise RuntimeError, ret[1]['errors'] unless ['200', '204'].include?(ret[0]) 49 | end 50 | 51 | def create_screenboard(sb) 52 | ret = @api.create_screenboard(sb) 53 | raise RuntimeError, ret[1]['errors'] if ret[0] != '200' 54 | end 55 | 56 | def update_screenboard(sb) 57 | id = sb['id'] 58 | sb.delete('id') 59 | ret = @api.update_screenboard(id, sb) 60 | raise RuntimeError, ret[1]['errors'] if ret[0] != '200' 61 | end 62 | 63 | def delete_screenboard(id) 64 | ret = @api.delete_screenboard(id) 65 | raise RuntimeError, ret[1]['errors'] unless ['200', '204'].include?(ret[0]) 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/dashdog/converter.rb: -------------------------------------------------------------------------------- 1 | require 'dslh' 2 | 3 | DELETE_KEYS = ['id', 'board_title', 'created', 'modified', 'created_by'] 4 | 5 | module Dashdog 6 | class Converter 7 | def initialize 8 | @boards = {'timeboards' => [], 'screenboards' => []} 9 | end 10 | 11 | def timeboards_to_dsl(tbs) 12 | exclude_key = proc do |k| 13 | false 14 | end 15 | 16 | ret = '' 17 | tbs.each do |tb| 18 | title = tb['title'] 19 | DELETE_KEYS.each {|k| tb.delete(k) } 20 | dsl = Dslh.deval( 21 | tb, 22 | exclude_key: exclude_key) 23 | dsl.gsub!(/^/, ' ').strip! 24 | ret << <<-EOS 25 | timeboard #{title.inspect} do 26 | #{dsl} 27 | end 28 | 29 | EOS 30 | end 31 | ret 32 | end 33 | 34 | def screenboards_to_dsl(screenboards) 35 | exclude_key = proc do |k| 36 | false 37 | end 38 | 39 | ret = '' 40 | screenboards.each do |sb| 41 | title = sb['board_title'] 42 | DELETE_KEYS.each {|k| sb.delete(k) } 43 | widgets = sb['widgets'] || [] 44 | sb['widgets'] = [] 45 | widgets.each do |wd| 46 | wd.delete('board_id') 47 | sb['widgets'] << wd 48 | end 49 | dsl = Dslh.deval( 50 | sb, 51 | exclude_key: exclude_key) 52 | dsl.gsub!(/^/, ' ').strip! 53 | ret << <<-EOS 54 | screenboard #{title.inspect} do 55 | #{dsl} 56 | end 57 | 58 | EOS 59 | end 60 | ret 61 | end 62 | 63 | def to_h(dsl_file) 64 | context = DSLContext.new 65 | context.eval_dsl(dsl_file) 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/dashdog/dsl_context.rb: -------------------------------------------------------------------------------- 1 | require 'hashie' 2 | 3 | module Dashdog 4 | class DSLContext 5 | def initialize 6 | @boards = {'timeboards' => [], 'screenboards' => []} 7 | @templates = {} 8 | @context = Hashie::Mash.new() 9 | end 10 | 11 | def eval_dsl(dsl_file) 12 | @_dsl_file = dsl_file 13 | instance_eval(File.read(dsl_file), dsl_file) 14 | @boards 15 | end 16 | 17 | private 18 | 19 | def template(name, &block) 20 | @templates[name.to_s] = block 21 | end 22 | 23 | def context 24 | @context 25 | end 26 | 27 | def require(file) 28 | boardfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@_dsl_file), file)) 29 | 30 | if File.exist?(boardfile) 31 | instance_eval(File.read(boardfile), boardfile) 32 | elsif File.exist?(boardfile + '.rb') 33 | instance_eval(File.read(boardfile + '.rb'), boardfile + '.rb') 34 | else 35 | Kernel.require(file) 36 | end 37 | end 38 | 39 | def timeboard(value = nil, &block) 40 | hash = dslh_eval(block) 41 | hash['title'] = value 42 | @boards['timeboards'] << hash 43 | end 44 | 45 | def screenboard(value = nil, &block) 46 | hash = dslh_eval(block) 47 | hash['board_title'] = value 48 | @boards['screenboards'] << hash 49 | end 50 | 51 | def dslh_eval(block) 52 | scope_hook = proc do |scope| 53 | scope.instance_eval(<<-'EOS') 54 | def include_template(template_name, context = {}) 55 | tmplt = @templates[template_name.to_s] 56 | 57 | unless tmplt 58 | raise "Template '#{template_name}' is not defined" 59 | end 60 | 61 | context_orig = @context 62 | @context = @context.merge(context) 63 | instance_eval(&tmplt) 64 | @context = context_orig 65 | end 66 | 67 | def context 68 | @context 69 | end 70 | EOS 71 | end 72 | 73 | scope_vars = {templates: @templates, context: @context} 74 | 75 | Dslh.eval(allow_empty_args: true, scope_hook: scope_hook, scope_vars: scope_vars, &block) 76 | end 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /lib/dashdog/logger.rb: -------------------------------------------------------------------------------- 1 | require 'logger' 2 | require 'singleton' 3 | 4 | module Dashdog 5 | class TermColor 6 | class << self 7 | 8 | def green(msg) 9 | colorize 32, msg 10 | end 11 | 12 | def yellow(msg) 13 | colorize 33, msg 14 | end 15 | 16 | def red(msg) 17 | colorize 31, msg 18 | end 19 | 20 | def colorize(num, msg) 21 | "\e[#{num}m#{msg}\e[0m" 22 | end 23 | 24 | end 25 | end 26 | 27 | class Logger < Logger 28 | include Singleton 29 | 30 | def initialize 31 | super(STDERR) 32 | 33 | self.formatter = proc do |severity, datetime, progname, msg| 34 | "#{msg}\n" 35 | end 36 | 37 | self.level = Logger::INFO 38 | end 39 | 40 | def debug(progname = nil, method_name = nil, msg) 41 | super(progname) { { method_name: method_name, message: msg } } 42 | end 43 | 44 | def info(msg) 45 | super { Dashdog::TermColor.green(msg) } 46 | end 47 | 48 | def warn(msg) 49 | super { Dashdog::TermColor.yellow(msg) } 50 | end 51 | 52 | def fatal(msg) 53 | super { Dashdog::TermColor.red(msg) } 54 | end 55 | 56 | def error(progname = nil, method_name = nil, msg, backtrace) 57 | super(progname) { { method_name: method_name, message: msg, backtrace: backtrace } } 58 | end 59 | 60 | module Helper 61 | 62 | def log(level, message) 63 | logger = Dashdog::Logger.instance 64 | logger.send(level, message) 65 | end 66 | 67 | def info(msg) 68 | log(:info, msg) 69 | end 70 | 71 | def warn(msg) 72 | log(:warn, msg) 73 | end 74 | 75 | def fatal(msg) 76 | log(:error, msg) 77 | end 78 | 79 | def debug(msg) 80 | log(:debug, msg) 81 | end 82 | 83 | module_function :log, :info, :warn, :fatal, :debug 84 | end 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /lib/dashdog/utils.rb: -------------------------------------------------------------------------------- 1 | require 'json' 2 | require 'diffy' 3 | require 'coderay' 4 | 5 | module Dashdog 6 | class Utils 7 | 8 | def self.diff(hash1, hash2) 9 | Diffy::Diff.new( 10 | JSON.pretty_generate(self.deep_sort_hash(hash1)) + "\n", 11 | JSON.pretty_generate(self.deep_sort_hash(hash2)) + "\n", 12 | :diff => '-u' 13 | ).to_s(:color) 14 | end 15 | 16 | def self.print_yaml(yaml) 17 | CodeRay::Encoders::Terminal::TOKEN_COLORS[:key] = { 18 | self: "\e[32m", 19 | } 20 | puts CodeRay.scan(yaml, :yaml).terminal 21 | end 22 | 23 | def self.print_ruby(ruby, options = {}) 24 | if options[:color] 25 | puts CodeRay.scan(ruby, :ruby).terminal 26 | else 27 | puts ruby 28 | end 29 | end 30 | 31 | def self.print_json(json) 32 | puts CodeRay.scan(json, :json).terminal 33 | end 34 | 35 | def self.deep_sort_hash(obj) 36 | case obj 37 | when Hash 38 | new_hash = {} 39 | 40 | obj.sort_by{|k, _| k.to_s }.each do |key, value| 41 | new_hash[key] = self.deep_sort_hash(value) 42 | end 43 | 44 | new_hash 45 | when Array 46 | obj.map do |value| 47 | self.deep_sort_hash(value) 48 | end 49 | else 50 | obj 51 | end 52 | end 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/dashdog/version.rb: -------------------------------------------------------------------------------- 1 | module Dashdog 2 | VERSION = "0.4.0" 3 | end 4 | -------------------------------------------------------------------------------- /spec/dashdog_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Dashdog do 4 | it 'has a version number' do 5 | expect(Dashdog::VERSION).not_to be nil 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'dashdog' 3 | --------------------------------------------------------------------------------