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_module_for(name) puts "Creating placeholders for module #{name}...\n" FileUtils.mkdir_p "lib/teamster-modules/#{name}/views" create_file "lib/teamster-modules/#{name}.rb", "module_placeholder_for", name create_file "lib/teamster-modules/#{name}/#{name}_helper.rb", "module_helper_placeholder_for", name create_file "lib/teamster-modules/#{name}/views/#{name}.erb", "view_placeholder_for", name create_file "lib/teamster-modules/#{name}/views/#{name}_summary.erb", "view_summary_placeholder_for", name puts "\nBasic module creation done!" puts "Controller : \"lib/teamster-modules/#{name}.rb\"" puts "Helper : \"lib/teamster-modules/#{name}/#{name}_helper.rb\"" puts "View : \"lib/teamster-modules/#{name}/views/#{name}.erb\"" puts "Summary View : \"lib/teamster-modules/#{name}/views/#{name}_summary.erb\"" end def import_module_for(name) puts "Importing module: #{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 module. Export depends on cli tool \"unzip\"." end else puts "Unable to find file: #{zip_file}" end end def export_module_for(name) puts "Exporting module: #{name}" zip_file = "#{name}.zip" puts "The following files will be zipped:" puts "- lib/teamster-modules/#{name}.rb" puts '- Everything in folder lib/teamster-modules/#{name}/' if `which zip`.length != 0 `zip -r #{zip_file} lib/teamster-modules/#{name}.rb lib/teamster-modules/#{name}/` puts "\nExported to #{zip_file}!" else puts "\nUnable to export module. 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-module NAME Creates a stub of a module --import-module FILE << PENDING IMPLEMENTATION >> --export-module 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)} run Teamster::Core::App CODE end def teamster_modules <<-CODE Dir.glob(File.dirname(__FILE__) + '/teamster-modules/*.rb').each do |mdl| require mdl end CODE end def module_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 modules. module Teamster module Modules class #{name.capitalize} < Sinatra::Base \# Class methods that contain Teamster-Module specific helpers. include BaseModule \# Stuff that needs to be done before registration with core. has_helpers #{name.capitalize}Helper \# Add modules here (comma separated) if there are helper modules. 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 get '/#{name}/?' do erb :#{name} end end end end CODE end def module_helper_placeholder_for(name) <<-CODE module Teamster module Modules 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
Page under construction. Please check back later!
CODE end def view_summary_placeholder_for(name) <<-CODEUnder development right now..
CODE end end end end