Class: Aka::Store

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/aka/store.rb

Constant Summary collapse

FORMAT =
'3'

Instance Method Summary collapse

Instance Method Details

#add(options) ⇒ Object



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
# File 'lib/aka/store.rb', line 42

def add(options)
  shortcut = Configuration::Shortcut.new({
    :shortcut => options[:shortcut],
    :command => options[:command],
    :tag => options[:tag],
    :description => options[:description],
    :function => options[:function]
  })

  found = shortcut_manager.find(options)
  if found.length > 0
    unless options[:force]
      abort %{Shortcut "#{options[:shortcut]}" exists. Pass --force to overwrite. Or provide a new --tag.}
    else
      found.each do |row|
        [:shortcut, :command, :description, :function].each do |key|
          row.send(:"#{key}=", options[key])
        end
        row.tag = options[:tag] || []
      end
      save
      puts "Overwrote shortcut."
    end
  else
    shortcut_manager.add(shortcut)
    save
    puts "Created shortcut."
  end
end

#edit(options) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/aka/store.rb', line 151

def edit(options)
  result = nil

  row = shortcut_manager.find(options).first

  unless row
    abort "Shortcut not found."
  end

  if options[:input]
    result = File.read(options[:input])
  else
    file = Tempfile.new('shortcut')
    begin
      file.open
      if row.tag
        tags = %{ #{row.tag.join(', ')}}
      else
        tags = ''
      end
      file.write(<<-EOS.gsub(/^            /, '')
        Shortcut: #{row.shortcut}
        Description:
        #{row.description}
        Function (y/n): #{row.function ? 'y' : 'n'}
        Tags:#{tags}
        Command:
        #{row.command}
      EOS
      )
      file.close
      editor = ENV['EDITOR'] || 'vim'
      system(%[#{editor} #{file.path}])
      debug("Editing exited with code: #{$?.exitstatus}.")
      if $?.exitstatus == 0
        file.open
        file.rewind
        result = file.read.strip
      end
    ensure
      file.unlink
    end
  end

  if result
    parse_row_txt(row, result)
    save
    puts "Saved shortcut."
  else
  end
end

#generate(options) ⇒ Object



114
115
116
117
118
# File 'lib/aka/store.rb', line 114

def generate(options)
  excluded = shortcut_manager.generate(options)

  excluded_output(excluded)
end

#help(command, options) ⇒ Object



12
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
# File 'lib/aka/store.rb', line 12

def help(command, options)
  case command
  when nil       then command = "aka.7"
  when 'ls'      then command = "aka.list.1"
  else command = "aka-#{command}.1"
  end

  manpages = %w(
    aka-add.1
    aka-edit.1
    aka-generate.1
    aka-list.1
    aka-remove.1
    aka-show.1
    aka-link.1
    aka-sync.1
    aka-upgrade.1
    aka.7)

  exit unless manpages.include?(command)

  root = File.expand_path("../man", __FILE__)

  if !ENV['NO_MAN'] && Aka::App.which("man") && root !~ %r{^file:/.+!/META-INF/jruby.home/.+}
    Kernel.exec "man #{root}/#{command}"
  else
    puts File.read("#{root}/#{command}.txt")
  end
end


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/aka/store.rb', line 120

def link(options)
  unless options[:tag] && options[:output]
    abort("Invalid link.")
  end

  new_link = Configuration::Link.new({
    :tag => options[:tag],
    :output => options[:output]
  })

  link_manager.add(new_link)
  save
  puts "Saved link."
end

#list(options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aka/store.rb', line 72

def list(options)
  excluded = shortcut_manager.match_by_tag(options[:tag] || []) do |tag, rows|
    puts %{##{tag}}
    puts ''.ljust(tag.length + 1, '=')
    rows.each do |row|
      list_output(row)
    end
    puts
  end

  if link_manager.any?
    puts "====="
    puts "Links"
    puts "====="
    puts

    index = 1
    link_manager.each do |link|
      puts "[#{index}] #{link.output}: #{link.tag.map { |tag| "##{tag}" }.join(', ')}"
      index += 1
    end
  end

  excluded_output(excluded)
end

#remove(options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aka/store.rb', line 98

def remove(options)
  found = shortcut_manager.find(options)

  if found.length > 0
    found.each do |shortcut|
      shortcut_manager.remove(shortcut)
    end

    save

    puts "Removed shortcut."
  else
    abort %{No shortcut "#{options[:shortcut]}". Aborting.}
  end
end

#saveObject



225
226
227
228
229
# File 'lib/aka/store.rb', line 225

def save
  File.open(aka_db, 'w+') do |f|
    f.write configuration.encode
  end
end

#show(options) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/aka/store.rb', line 203

def show(options)
  row = shortcut_manager.find(options).first

  if row
    puts show_output(row)
  else
    abort "Shortcut not found."
  end
end

#sync(match) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/aka/store.rb', line 141

def sync(match)
  index = 0
  link_manager.each do |config|
    index += 1
    next if match && match != index
    excluded = shortcut_manager.generate({ :tag => config.tag, :output => config.output })
    excluded_output(excluded)
  end
end


135
136
137
138
139
# File 'lib/aka/store.rb', line 135

def unlink(key)
  link_manager.remove(key)
  save
  puts "Deleted link."
end

#upgrade(options) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/aka/store.rb', line 213

def upgrade(options)
  configuration

  if !@version
    Upgrader::FromV0ToV1.run(aka_db)
  elsif @version == '1'
    Upgrader::FromV1ToV2.run(aka_db)
  elsif @version == '2'
    Upgrader::FromV2ToV3.run(aka_db)
  end
end