lib/fanforce/cli/utils.rb in fanforce-cli-1.7.1 vs lib/fanforce/cli/utils.rb in fanforce-cli-2.0.0.rc1
- old
+ new
@@ -1,70 +1,90 @@
module Fanforce::CLI::Utils
- extend Fanforce::CLI::Utils
+ extend self
+ def self.included(base) base.extend(self) end
- def format(text, *args)
+ def divider(template)
+ line_width = 150
+ line_width = line_width - $1.size if template =~ /(\s+)[=-]+/
+ template.gsub('+', "\n").gsub(/[-]+/, '-' * line_width).gsub(/[=]+/, '=' * line_width)
+ end
+
+ def log(msg='')
+ puts msg
+ end
+
+ def error(msg, command=nil)
+ puts divider '+-+'
+ puts 'ERROR '.format(:red,:bold) + msg
+ puts divider '-++'
+ exit
+ end
+
+ def fmt(text, *args)
+ "#{fmt_start(*args)}#{text}#{fmt_end}"
+ end
+
+ def fmt_start(*args)
effect = args.include?(:bold) ? 1 : 0
color = if args.include?(:red) then 31
elsif args.include?(:green) then 32
elsif args.include?(:magenta) then 35
else 39 end
- "\033[#{effect};#{color}m#{text}\033[0m"
+ "\033[#{effect};#{color}m"
end
- def format_config(hash)
- hash.values.select{|v| v.is_a? Hash}.each{|h| format_config(h)}
- hash.symbolize_keys!
+ def fmt_end
+ "\033[0m"
end
- def unknown
- puts '---------------------------------------------------------------------------------------------------------------'
- puts 'OOPS'.format(:bold,:red) + '... unknown command'.format(:red)
- puts Fanforce::CLI::Help.commands(@allowed_commands)
- exit 1
- end
-
- def error(msg, command=nil)
- puts '---------------------------------------------------------------------------------------------------------------'
- puts 'ERROR... '.format(:bold,:red) + msg
- puts '---------------------------------------------------------------------------------------------------------------'
- puts ''
- if command.present?
- puts Fanforce::CLI::Help.for(command, @allowed_commands)
- else
- puts Fanforce::CLI::Help.commands(@allowed_commands)
- end
- exit 1
- end
-
- def confirm(msg)
- puts '---------------------------------------------------------------------------------------------------------------'
+ def confirm(msg, exit_unless_confirmed=true)
print "#{msg} [y/n]: "
input = $stdin.gets.strip
- exit 0 if input.downcase == 'n'
+ confirmed = !input.downcase.include?('n')
+ (!confirmed && exit_unless_confirmed) ? exit(0) : confirmed
end
- def env(environment)
- return if environment.blank?
- is_symbol = environment.is_a?(Symbol)
- env = case environment.to_sym
- when :development then :dev
- when :staging then :stg
- when :production then :prd
- else raise 'unknown environment'
- end
- is_symbol ? env : env.to_s
+ def prompt(msg, required=false)
+ print "#{msg}"
+ response = $stdin.gets.strip
+ (required && response.blank?) ? prompt(msg, required) : response
end
- def get_heroku_app_name(app, environment)
- raise 'unknown environment' if ![:production, :staging].include?(environment)
+ def find_cli_type(home_dir)
+ dir_names = home_dir.split('/')
+ types = {
+ directory_of_internals: false,
+ single_internal: false,
+ directory_of_apps: false,
+ single_app: false
+ }
- heroku_app_name = "#{env(environment)}-#{app.dir_name}"
- heroku_app_name.length > 30 ? heroku_app_name.gsub!(/(a|e|i|o|u)/, '') : heroku_app_name
+ types[:single_app] = true if File.exists?("#{home_dir}/config.json") and File.exists?("#{home_dir}/../.fanforce-app-factory")
+ types[:directory_of_apps] = true if File.exists?("#{home_dir}/.fanforce-app-factory")
+
+ if [dir_names[-1],dir_names[-2]].include?('Fanforce') && !File.exists?("#{home_dir}/.git") && !File.exists?("#{home_dir}/.fanforce-app-factory")
+ types[:directory_of_internals] = true
+ end
+
+ if [dir_names[-1],dir_names[-2],dir_names[-2]].include?('Fanforce') and File.exists?("#{home_dir}/.git") and !File.exists?("#{home_dir}/../.fanforce-app-factory")
+ types[:single_internal] = true
+ end
+
+ found_types = types.inject([]) {|found_types, (key,bool)| bool ? found_types << key : found_types }
+
+ if found_types.size != 1
+ divider '+-'
+ (found_types.size == 0) ? log('COULD NOT DEDUCE CLI TYPE') : log("CONFLICTING CLI TYPES: #{found_types.to_s}")
+ divider '+-'
+ exit
+ end
+
+ return found_types.first
end
end
class String
def format(*args)
- Fanforce::CLI::Utils.format(self, *args)
+ Fanforce::CLI::Utils.fmt(self, *args)
end
end
\ No newline at end of file