Class: Chronicler::CLI

Inherits:
Thor
  • Object
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 (private)

Raises:

  • (Error)


268
269
270
# File 'lib/chronicler/cli.rb', line 268

def method_missing(method, *args)
  raise Error, "Unrecognized command \"#{method}\". Please consult `crn help`."
end

Instance Method Details

#__print_versionObject



207
208
209
# File 'lib/chronicler/cli.rb', line 207

def __print_version
  puts "Chronicler #{Chronicler::VERSION}"
end

#branchObject



73
74
75
76
# File 'lib/chronicler/cli.rb', line 73

def branch
  puts "On repository #{repository.name}"
  repository.git("branch", :system)
end

#checkout(identifier = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/chronicler/cli.rb', line 173

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

#commitObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chronicler/cli.rb', line 144

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

#destroyObject



195
196
197
198
199
200
201
202
203
# File 'lib/chronicler/cli.rb', line 195

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

#listObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chronicler/cli.rb', line 53

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

#loadObject



166
167
168
169
170
# File 'lib/chronicler/cli.rb', line 166

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



100
101
102
103
104
105
106
107
108
# File 'lib/chronicler/cli.rb', line 100

def log(interface = nil)
  repository # check available repository
  interface ||= begin
    ensure_interface
    Chronicler.config[:interface]
  end
  command = (interface == "git") ? "git log" : interface
  repository.run(command, :system)
end

#new(branch = nil) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/chronicler/cli.rb', line 117

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

#openObject



48
49
50
# File 'lib/chronicler/cli.rb', line 48

def open
  repository.run "open ."
end

#reset(commit) ⇒ Object



188
189
190
191
192
# File 'lib/chronicler/cli.rb', line 188

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

#selectObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/chronicler/cli.rb', line 126

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

#statusObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chronicler/cli.rb', line 79

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

#switch(name = nil) ⇒ Object



42
43
44
45
# File 'lib/chronicler/cli.rb', line 42

def switch(name = nil)
  use(name)
  load
end

#treeObject



111
112
113
114
# File 'lib/chronicler/cli.rb', line 111

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 Chronicler.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