Class: Zugzwang::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, Helpers
Defined in:
lib/zugzwang/cli.rb

Instance Method Summary collapse

Methods included from Helpers

#sanitize_path

Instance Method Details

#create(database = 'lichess', *items) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zugzwang/cli.rb', line 13

def create(database = 'lichess', *items)
    if items.empty?
        puts "\n\e[1;91mERROR\e[0m: No files specified." if items.empty?
        return
    end

    database_path = sanitize_path(database)
    adapter = options[:adapter].to_sym

    if Zugzwang::ADAPTERS.include?(adapter)
        begin
            Zugzwang::Connection.new(database_path, adapter).populate(items)
        rescue Sequel::DatabaseConnectionError
            puts "\n\e[1;91mERROR\e[0m: Directory \e[1m#{File.dirname(database)}\e[0m does not exist."
            pass = false
            until pass
                response = ask("\e[1mPROMPT: \e[0mCreate directory \e[1m#{File.dirname(database_path)}\e[0m? [Y/n]")
                if %w[Y y YES Yes yes].include? response
                    puts
                    empty_directory(File.dirname(database_path))
                    Zugzwang::Connection.new(database_path,adapter).populate(items)
                    pass = true
                elsif %w[N n NO No no].include? response
                    pass = true
                end
            end
        end
    else
        puts "\n\e[1;91mERROR\e[0m: Database adapter argument should be one of [#{Zugzwang::ADAPTERS*', '}]."
    end
end