Class: Bashy::Add

Inherits:
Object
  • Object
show all
Defined in:
lib/bashy.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Add

Returns a new instance of Add.

Raises:



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

def initialize(options = {})
  # Check that our directories are in tact.
  Bashy::check_and_load_config_directory
  # If we haven't been passed the name of the bash snippet, raise an exception.
  raise(ArgumentError) unless options.has_key?(:name)
  # We're going to do different things here if we're operating on a path or the user wants to enter a snippet of code directly.
  snippet_path = File.join(SNIPPET_PATH, options[:name])
  if(!options[:path].nil?)
    FileUtils.cp(options[:path], snippet_path)
  else(options.has_key?(:snippet))
    File.open(snippet_path, 'w') do |f|
      f.write("#!/usr/bin/env bash\n") unless options[:snippet] =~ /^#!/
      f.write(options[:snippet])
    end
    FileUtils.chmod(0777, snippet_path)
  end
  Bashy.config[:snippets] << options[:name]
  Bashy.save_config
end