module Teamster module CLI def self.included(base) base.extend ClassMethods end module ClassMethods def quit(msg, code = 0) warn msg exit code end def create_config(team_name) if team_name @config[:title] = team_name else ask_user_for :title, "What is your team name" end File.open(CONFIG_FILE, 'w') {|fh| fh.write @config.to_yaml} end def create_user puts "Creating default user.." users = [{"name" => "Administrator", "pass" => b64_enc(BCrypt::Password.create("password"))}] File.open("./data/users", "w") do |fh| fh.write({"users" => users}.to_yaml) end FileUtils.chmod 0600, './data/users' end def b64_enc(obj) Base64.strict_encode64(obj) end def ask_user_for(opt, question) unless @config[opt] @config[opt] = ask_user question end end def ask_user(question) print "#{question}: " STDIN.gets.strip end def create_adapter_for(name) puts "Creating placeholders for adapter #{name}...\n" FileUtils.mkdir_p "lib/teamster-adapters/#{name}/views" create_file "lib/teamster-adapters/#{name}.rb", "adapter_placeholder_for", name create_file "lib/teamster-adapters/#{name}/#{name}_helper.rb", "adapter_helper_placeholder_for", name create_file "lib/teamster-adapters/#{name}/views/#{name}.erb", "view_placeholder_for", name create_file "lib/teamster-adapters/#{name}/views/#{name}_summary.erb", "view_summary_placeholder_for", name puts "\nBasic adapter creation done!" puts "Controller : \"lib/teamster-adapters/#{name}.rb\"" puts "Helper : \"lib/teamster-adapters/#{name}/#{name}_helper.rb\"" puts "View : \"lib/teamster-adapters/#{name}/views/#{name}.erb\"" puts "Summary View : \"lib/teamster-adapters/#{name}/views/#{name}_summary.erb\"" end def import_adapter_for(name) puts "Importing adapter: #{name}" zip_file = "#{name}.zip" if File.exists?(zip_file) if `which unzip`.length != 0 `unzip #{zip_file}` puts "\nSuccessfully imported #{name}. Please restart teamster to use." else puts "\nUnable to import adapter. Export depends on cli tool \"unzip\"." end else puts "Unable to find file: #{zip_file}" end end def export_adapter_for(name) puts "Exporting adapter: #{name}" zip_file = "#{name}.zip" puts "The following files will be zipped:" puts "- lib/teamster-adapters/#{name}.rb" puts '- Everything in folder lib/teamster-adapters/#{name}/' if `which zip`.length != 0 `zip -r #{zip_file} lib/teamster-adapters/#{name}.rb lib/teamster-adapters/#{name}/` puts "\nExported to #{zip_file}!" else puts "\nUnable to export adapter. Export depends on cli tool \"zip\"." end end def create_file(filename, method, *args) case [File.exists?(filename), !!@config[:overwrite]] when [true, false] puts "File \"#{filename}\" exists. Run with --overwrite to overwrite file." else puts "Creating file #{filename}..." File.open(filename, "w") {|fh| fh.write(send(method.to_sym, *args))} end end def show_version "Current version of teamster: #{VERSION}" end def usage <<-HELP Initialize application: teamster init Run web application: teamster start Run web application in production (uses unix socket & state file): teamster start --prod [--socket-file FILE] [--state-file FILE] Verify by opening browser and navigating to "http://localhost:9292". For more detailed help, please run "teamster --help". HELP end def detailed_usage <<-DETAIL Teamster is a simple and extensible web portal for teams. Current version: #{VERSION} Usage: teamster [COMMAND] [OPTIONS] Commands: init, start, stop, restart Options (standalone): --help Display this detailed help. --version Version of the teamster used. --create-adapter NAME Creates a stub of a adapter --import-adapter FILE << PENDING IMPLEMENTATION >> --export-adapter FILE << PENDING IMPLEMENTATION >> Options used with \"start\": --prod Binds a unix socket to be used by a web server (eg. Nginx) and creates a state file that is used by the Puma app server. --socket-file FILE Relative/absolute path to the UNIX socket file. --state-file FILE Relative/absolute path to the Puma state file. Options used with \"stop\": --socket-file FILE Relative/absolute path to the UNIX socket file. --state-file FILE Relative/absolute path to the Puma state file. Options used with \"restart\": --socket-file FILE Relative/absolute path to the UNIX socket file. --state-file FILE Relative/absolute path to the Puma state file. DETAIL end def config_ru <<-CODE require 'teamster' Dir.glob("lib/*.rb").each {|file| require File.absolute_path(file)} Dir.mkdir("log") unless Dir.exists?("log") log_file = File.new("log/teamster-#{Time.now.strftime("%Y%m%d-%H%M")}.log", "a+") $stdout.reopen(log_file) $stderr.reopen(log_file) $stdout.sync=true $stderr.sync=true run Teamster::Core::App CODE end def teamster_adapters <<-CODE Dir.glob(File.dirname(__FILE__) + '/teamster-adapters/*.rb').each do |mdl| require mdl end CODE end def adapter_placeholder_for(name) <<-CODE require_relative \"#{name}/#{name}_helper\" \# NOTE: If the namespace is changed, please take care of the \# namespace of the sub-class and helper adapters. module Teamster module Adapters class #{name.capitalize} < Sinatra::Base \# Class methods that contain Teamster-Adapter specific helpers. include BaseAdapter \# Stuff that needs to be done before registration with core. has_helpers #{name.capitalize}Helper \# Add adapters here (comma separated) if there are helper adapters. views_at \"\#\{File.dirname(__FILE__)\}/#{name}/views\" under_development \# Remove this line when development is finished. \# Register this class so it can be used. register self configure do enable :logging \# To log, use \"logger.info \". Log messages are captured \# in the file specified in config.ru. end get '/#{name}/?' do erb :#{name} end end end end CODE end def adapter_helper_placeholder_for(name) <<-CODE module Teamster module Adapters module #{name.capitalize}Helper def #{name}_summary? true end def #{name}_summary erb :#{name}_summary end end end end CODE end def view_placeholder_for(name) <<-CODE

PLACEHOLDER FOR #{name.upcase}

Page under construction. Please check back later!

CODE end def view_summary_placeholder_for(name) <<-CODE

Under development right now..

CODE end end end end