Module: Dkit::Intercept
- Defined in:
- lib/dkit/intercept.rb
Class Method Summary collapse
- .add(project_root, cmd) ⇒ Object
- .file_path(project_root) ⇒ Object
- .list(project_root) ⇒ Object
- .remove(project_root, cmd) ⇒ Object
- .verbose_enabled?(project_root) ⇒ Boolean
Class Method Details
.add(project_root, cmd) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/dkit/intercept.rb', line 20 def add(project_root, cmd) current = list(project_root) if current.include?(cmd) puts "dkit: '#{cmd}' is already in the intercept list" return end File.open(file_path(project_root), "a") { |f| f.puts cmd } puts "dkit: added '#{cmd}' — reload shell to activate (exec zsh)" end |
.file_path(project_root) ⇒ Object
7 8 9 |
# File 'lib/dkit/intercept.rb', line 7 def file_path(project_root) File.join(project_root, DC_INTERCEPT) end |
.list(project_root) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/dkit/intercept.rb', line 11 def list(project_root) f = file_path(project_root) return [] unless File.exist?(f) File.readlines(f, chomp: true) .reject { |l| l.strip.empty? || l.strip.start_with?("#") } .map(&:strip) .uniq end |
.remove(project_root, cmd) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dkit/intercept.rb', line 30 def remove(project_root, cmd) f = file_path(project_root) unless list(project_root).include?(cmd) puts "dkit: '#{cmd}' is not in the intercept list" return end lines = File.readlines(f).reject { |l| l.strip == cmd } File.write(f, lines.join) puts "dkit: removed '#{cmd}' — reload shell to deactivate (exec zsh)" end |
.verbose_enabled?(project_root) ⇒ Boolean
41 42 43 44 45 46 |
# File 'lib/dkit/intercept.rb', line 41 def verbose_enabled?(project_root) return false if ENV["DKIT_VERBOSE"] == "0" f = file_path(project_root) return true unless File.exist?(f) !File.readlines(f, chomp: true).any? { |l| l.strip == "verbose: false" } end |