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



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zugzwang/cli.rb', line 15

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

    database = sanitize_path(database)
    extension = options[:extension].sub(?.,'').to_sym
    override = false

    loop do
        if Zugzwang::EXTENSIONS.include?(extension) || override

            begin
                db = Sequel.sqlite("#{database}.#{extension}")
                Zugzwang::Create[db, database, extension, items]
            rescue Sequel::DatabaseConnectionError => e
                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)}\e[0m? [Y/n]")
                    if %w[Y y YES Yes yes].include? response
                        puts
                        empty_directory(File.dirname(database))
                        begin
                            db = Sequel.sqlite("#{database}.#{extension}")
                            Zugzwang::Create[db, database, extension, items]
                        rescue Sequel::DatabaseConnectionError
                            puts "\n\e[1;91mERROR\e[0m: Unable to create database with extension \e[1m#{extension}\e[0m"
                        end
                        pass = true
                    elsif %w[N n NO No no].include? response
                        pass = true
                    end
                end

            end

            return

        else

            puts "\n\e[1;93mWARNING\e[0m: Extension argument should be one of [#{Zugzwang::EXTENSIONS*', '}]."

            pass = false
            until pass
                response = ask("\e[1mPROMPT: \e[0mAttempt to create database with extension \e[1m#{extension}\e[0m? [Y/n]")
                if %w[Y y YES Yes yes].include? response
                    override = true
                    pass = true
                elsif %w[N n NO No no].include? response
                    return
                end
            end

        end
    end
end