Class: Chronicler::CLI
- Inherits:
-
Thor
- Object
- Thor
- Chronicler::CLI
show all
- Defined in:
- lib/chronicler/cli.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
256
257
258
|
# File 'lib/chronicler/cli.rb', line 256
def method_missing(method, *args)
raise Error, "Unrecognized command \"#{method}\". Please consult `crn help`."
end
|
Instance Method Details
#branch ⇒ Object
67
68
69
70
|
# File 'lib/chronicler/cli.rb', line 67
def branch
puts "On repository #{repository.name}"
repository.git("branch", :system)
end
|
#checkout(identifier = nil) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/chronicler/cli.rb', line 167
def checkout(identifier = nil)
identifier ||= begin
branches = repository.branches - [repository.branch]
branches[Ask.list("Which branch do you want to checkout? (on #{repository.branch})", branches)]
end
if repository.dirty? && Ask.confirm("Current state is dirty. Do you want to commit first?")
commit
end
repository.checkout identifier
puts "Successfully loaded database#{"s" unless repository.databases.size == 1} of '#{identifier}'"
end
|
#commit ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/chronicler/cli.rb', line 138
def commit
if repository.new?
select
else
unless repository.dirty?
puts "Nothing to commit."
exit!
end
if (tag = options[:tag]) && !repository.git("rev-parse #{tag}", :verbose).empty?
unless Ask.confirm("Tag '#{tag}' already exists. Do you want to move the tag?")
puts "fatal: Commit aborted."
exit!
end
end
end
message = options[:message] || "Updated databases"
repository.commit message, tag
end
|
#destroy ⇒ Object
189
190
191
192
193
194
195
196
197
|
# File 'lib/chronicler/cli.rb', line 189
def destroy
if File.exists?(Chronicler.config_path)
if Ask.confirm("Are you sure you want to continue? (Everything will be LOST)")
Chronicler.destroy!
end
else
puts "Nothing to destroy."
end
end
|
#init(name = defacto_name) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/chronicler/cli.rb', line 9
def init(name = defacto_name)
ensure_store
if path = repository(name).init
puts "Created empty Chronicler repository at #{path}"
end
config_use name
end
|
#list ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/chronicler/cli.rb', line 47
def list
if Chronicler.repositories.any?
name = [current_name, whoami].detect do |x|
Chronicler.repositories.include?(x)
end
puts "Located at #{Chronicler.store.gsub(Dir.home, "~")}"
Chronicler.repositories.each do |repo|
current = (repo == name)
prefix = current ? "* " : " "
repo = repo.green if current
puts "#{prefix}#{repo}"
end
else
puts "No Chronicler repositories initialized yet."
end
end
|
#load ⇒ Object
160
161
162
163
164
|
# File 'lib/chronicler/cli.rb', line 160
def load
if Ask.confirm("Are you sure you want to load databases from the #{repository.name.blue}:#{Git.head(repository.path)[0..7].yellow} commit? (Changes will be LOST)")
repository.load
end
end
|
#log(interface = nil) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/chronicler/cli.rb', line 94
def log(interface = nil)
repository
interface ||= begin
ensure_interface
Chronicler.config[:interface]
end
command = (interface == "git") ? "git log" : interface
repository.run(command, :system)
end
|
#new(branch = nil) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/chronicler/cli.rb', line 111
def new(branch = nil)
if repository && branch.nil? && Git.current.nil?
puts "Please specify the branch name as Chronicler cannot determine one."
else
repository.new(branch || Git.branch)
end
end
|
#open ⇒ Object
42
43
44
|
# File 'lib/chronicler/cli.rb', line 42
def open
repository.run "open ."
end
|
#reset(commit) ⇒ Object
182
183
184
185
186
|
# File 'lib/chronicler/cli.rb', line 182
def reset(commit)
if Ask.confirm("Are you sure you want to reset #{repository.name}:#{repository.branch} to #{commit.yellow}?")
repository.reset(commit)
end
end
|
#select ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/chronicler/cli.rb', line 120
def select
repository_databases = repository.databases
databases = []
options = `mysql -u root -e "SHOW DATABASES" -sN`.split(/\s+/).reject do |database|
%w(information_schema mysql performance_schema test).include? database
end
selected = options.collect{|database| repository_databases.include?(database)}
Ask.checkbox("Which database(s) would you like to store?", options, default: selected).each_with_index do |checked, index|
databases << options[index] if checked
end
repository.select databases
end
|
#status ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/chronicler/cli.rb', line 73
def status
puts "On branch #{repository.name}:#{repository.branch}"
if (changes = repository.changes).empty?
puts "Nothing to commit."
else
puts "Changes for commit:"
puts
changes[:added].each do |(table, checksum)|
puts " added: #{table}"
end
changes[:modified].each do |(table, checksum)|
puts " modified: #{table} (#{checksum})"
end
changes[:deleted].each do |(table, checksum)|
puts " deleted: #{table}"
end
puts
end
end
|
#tree ⇒ Object
105
106
107
108
|
# File 'lib/chronicler/cli.rb', line 105
def tree
puts "On repository #{repository.name}"
repository.git("log --graph --oneline --decorate --date=relative --all", :system)
end
|
#use(name = nil) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/chronicler/cli.rb', line 18
def use(name = nil)
current = current_name
repositories = Chronicler.repositories - [current]
if name == current
puts "Already using Chronicler repository '#{name}'."
exit!
end
name ||= begin
no_repository_available! if repositories.empty?
postfix = " (on #{current})" if repositories.include?(current)
repositories[Ask.list("Which repository do you want to use?#{postfix}", repositories)]
end
unless repositories.include?(name)
puts "fatal: Chronicler repository '#{name}' does not exist."
exit!
end
config_use(name)
end
|