class Gemstash::CLI::Setup
This implements the command line setup task:
$ gemstash setup
Public Class Methods
new(cli)
click to toggle source
Calls superclass method
Gemstash::CLI::Base::new
# File lib/gemstash/cli/setup.rb, line 12 def initialize(cli) super @config = {} end
Public Instance Methods
run()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 17 def run if setup? && !@cli.options[:redo] @cli.say @cli.set_color("Everything is already setup!", :green) return end check_rubygems_version ask_storage ask_cache ask_database ask_protected_fetch ask_timeout check_cache check_storage check_database store_config save_metadata @cli.say @cli.set_color("You are all setup!", :green) end
Private Instance Methods
ask_cache()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 77 def ask_cache say_current_config(:cache_type, "Current cache") @config[:cache_type] = ask_with_default("Cache with what?", %w[memory memcached redis], "memory") ask_memcached_details if @config[:cache_type] == "memcached" ask_redis_details if @config[:cache_type] == "redis" end
ask_database()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 98 def ask_database say_current_config(:db_adapter, "Current database adapter") @config[:db_adapter] = ask_with_default("What database adapter?", %w[sqlite3 postgres mysql mysql2], "sqlite3") ask_database_details(@config[:db_adapter]) unless @config[:db_adapter] == "sqlite3" end
ask_database_details(database)
click to toggle source
# File lib/gemstash/cli/setup.rb, line 104 def ask_database_details(database) say_current_config(:db_url, "Current database url") default_value = if RUBY_PLATFORM == "java" "jdbc:#{database}:///gemstash" else "#{database}:///gemstash" end url = @cli.ask "Where is the database? [#{default_value}]" url = default_value if url.empty? @config[:db_url] = url end
ask_memcached_details()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 84 def ask_memcached_details say_current_config(:memcached_servers, "Current Memcached servers") servers = @cli.ask "What is the comma separated Memcached servers? [localhost:11211]" servers = "localhost:11211" if servers.empty? @config[:memcached_servers] = servers end
ask_protected_fetch()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 118 def ask_protected_fetch say_current_config(:protected_fetch, "Protected Fetch enabled") value = @cli.yes? "Use Protected Fetch for Private Gems? [y/N]" value = Gemstash::Configuration::DEFAULTS[:protected_fetch] if value.is_a?(String) && value.empty? @config[:protected_fetch] = value end
ask_redis_details()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 91 def ask_redis_details say_current_config(:redis_servers, "Current Redis servers") servers = @cli.ask "What is the comma-separated list of Redis servers? [redis://localhost:6379]" servers = "redis://localhost:6379" if servers.empty? @config[:redis_servers] = servers end
ask_storage()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 70 def ask_storage say_current_config(:base_path, "Current base path") path = @cli.ask "Where should files go? [~/.gemstash]", path: true path = Gemstash::Configuration::DEFAULTS[:base_path] if path.empty? @config[:base_path] = File.expand_path(path) end
ask_timeout()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 126 def ask_timeout say_current_config(:fetch_timeout, "Fetch timeout") timeout = @cli.ask "How many seconds to wait when fetching a gem? [20]" timeout = Gemstash::Configuration::DEFAULTS[:fetch_timeout] if timeout.to_i < 1 @config[:fetch_timeout] = timeout.to_i end
ask_with_default(prompt, options, default)
click to toggle source
# File lib/gemstash/cli/setup.rb, line 53 def ask_with_default(prompt, options, default) raise "The options must all be lower case" if options.any? {|x| x.downcase != x } result = nil displayed_options = options.map {|x| x == default ? x.upcase : x } prompt = "#{prompt} [#{displayed_options.join(", ")}]" until result result = @cli.ask prompt result = result.downcase result = default if result.empty? result = nil unless options.include?(result) end result end
check_cache()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 133 def check_cache try("cache") { gemstash_env.cache_client.alive! } end
check_database()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 137 def check_database try("database") { gemstash_env.db.test_connection } end
check_storage()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 141 def check_storage with_new_config do dir = gemstash_env.config[:base_path] if Dir.exist?(dir) # Do metadata check without using Gemstash::Storage.metadata because # we don't want to store metadata just yet metadata_file = gemstash_env.base_file("metadata.yml") break unless File.exist?(metadata_file) version = Gem::Version.new(YAML.load_file(metadata_file)[:gemstash_version]) break if Gem::Requirement.new("<= #{Gemstash::VERSION}").satisfied_by?(Gem::Version.new(version)) raise Gemstash::CLI::Error.new(@cli, "The base path already exists with a newer version of Gemstash") else @cli.say "Creating the file storage path '#{dir}'" FileUtils.mkpath(dir) end end end
config_file()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 39 def config_file @cli.options[:config_file] || Gemstash::Configuration::DEFAULT_FILE end
save_metadata()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 168 def save_metadata with_new_config do # Touch metadata to ensure it gets written Gemstash::Storage.metadata end end
say_current_config(option, label)
click to toggle source
# File lib/gemstash/cli/setup.rb, line 47 def say_current_config(option, label) return if gemstash_env.config.default?(option) @cli.say "#{label}: #{gemstash_env.config[option]}" end
say_error(title, error)
click to toggle source
# File lib/gemstash/cli/setup.rb, line 175 def say_error(title, error) return unless @cli.options[:debug] @cli.say @cli.set_color("#{title}: #{error}", :red) error.backtrace.each do |line| @cli.say @cli.set_color(" #{line}", :red) end end
setup?()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 43 def setup? File.exist?(config_file) end
store_config()
click to toggle source
# File lib/gemstash/cli/setup.rb, line 162 def store_config config_dir = File.dirname(config_file) FileUtils.mkpath(config_dir) unless Dir.exist?(config_dir) gemstash_env.atomic_write(config_file) {|f| f.write(YAML.dump(@config)) } end
try(thing, &block)
click to toggle source
# File lib/gemstash/cli/setup.rb, line 192 def try(thing, &block) @cli.say "Checking that the #{thing} is available" with_new_config(&block) rescue StandardError => e say_error "Error checking #{thing}", e raise Gemstash::CLI::Error.new(@cli, "The #{thing} is not available") end
with_new_config() { || ... }
click to toggle source
# File lib/gemstash/cli/setup.rb, line 185 def with_new_config gemstash_env.config = Gemstash::Configuration.new(config: @config) yield ensure gemstash_env.reset end