#!/usr/bin/env ruby require 'thor' require 'net/http' require 'json' class MockScheduler def method_missing(*args) yield end end def send_event(id, data) req = Net::HTTP::Post.new("/widgets/#{id}") req["content-type"] = "application/json" req.body = JSON.unparse(data.merge(:auth_token => Dashing::CLI.auth_token)) res = Net::HTTP.new('localhost', 3030).start { |http| http.request(req) } puts "Data Sent to #{id}: #{data}" end SCHEDULER = MockScheduler.new module Dashing class CLI < Thor include Thor::Actions class << self attr_accessor :auth_token end attr_accessor :name no_tasks do ['widget', 'dashboard', 'job'].each do |type| define_method "generate_#{type}" do |name| @name = Thor::Util.snake_case(name) directory type.to_sym, File.join("#{type}s") end end end def self.source_root File.expand_path('../../templates', __FILE__) end desc "new PROJECT_NAME", "Sets up ALL THE THINGS needed for your dashboard project." def new(name) @name = Thor::Util.snake_case(name) directory :project, @name end desc "generate (widget/dashboard/job) NAME", "Creates a new widget, dashboard, or job." def generate(type, name) send("generate_#{type}".to_sym, name) rescue NoMethodError => e puts "Invalid generator. Either use widget, dashboard, or job" end map "g" => :generate desc "start", "Starts the server in style!" method_option :job_path, :desc => "Specify the directory where jobs are stored" def start(*args) port_option = args.include?('-p')? '' : ' -p 3030' args = args.join(" ") command = "bundle exec thin -R config.ru start #{port_option} #{args}" command.prepend "export JOB_PATH=#{options[:job_path]}; " if options[:job_path] system(command) end map "s" => :start desc "job JOB_NAME AUTH_TOKEN(optional)", "Runs the specified job. Make sure to supply your auth token if you have one set." def job(name, auth_token = "") Dir[File.join(Dir.pwd, 'lib/**/*.rb')].each {|file| require file } self.class.auth_token = auth_token f = File.join(Dir.pwd, "jobs", "#{name}.rb") require f end end end Dashing::CLI.start