#!/usr/bin/env ruby require 'boson/runner' require 'pathname' require 'yaml' $:.unshift File.expand_path("../../lib", __FILE__) require 'babot' DEFAULT_CONFIG = { 'consumer_key' => "", 'consumer_secret' => "", 'oauth_token' => "", 'oauth_token_secret' => "" } class BabotRunner < Boson::Runner def initialize cmd "mkdir -p '#{root}'" end desc "Add bot [name] from [repository]" def add(name, repository) if File.directory? repository cmd "ln -s '#{File.realpath repository}' '#{root.join(name)}'" else cmd "git clone '#{repository}' '#{root.join(name)}'" end cmd "mkdir -p '#{Pathname.new(root).join name, 'config'}'" File.open(root.join(name, "config", "credentials.yml").to_s, 'w') do |config| config.write DEFAULT_CONFIG.to_yaml end configure name end desc "Update bot [name]" def update(name) cmd "cd '#{root.join(name)}' && git pull --rebase origin master && bundle install" end desc "Update crontab" def schedule `ls #{root}`.split.each do |name| cmd "whenever -i #{name} -f #{root.join(name, 'config', 'schedule.rb')}" end end desc "Run bot [name]" def run(name) options = YAML::load_file root.join(name, "config", 'credentials.yml') Twitter.configure do |config| config.consumer_key = options['consumer_key'] config.consumer_secret = options['consumer_secret'] config.oauth_token = options['oauth_token'] config.oauth_token_secret = options['oauth_token_secret'] end load root.join(name, 'babot.run').to_s end desc "Delete bot [name]" def delete(name) cmd "rm -rf '#{root.join name}' '#{root.join name, 'config', 'credentials.yml'}'" end desc "Configure bot [name]" def configure(name) cmd "#{ENV['EDITOR'] || 'nano'} '#{root.join name, "config", 'credentials.yml'}'" end desc "List bots" def list cmd "ls '#{root}'" end desc "Push to remote server [remote]" def push(remote) cmd "ssh '#{remote}' 'rm -rf ~/.babot'" cmd "scp -qr '#{root}' '#{remote}:~/.'" cmd "ssh '#{remote}' 'babot schedule'" end private def root Pathname.new(ENV["HOME"]).join ".babot" end def cmd(command) puts command system command end end BabotRunner.start