Class: SensuCli::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu-cli/editor.rb

Instance Method Summary collapse

Instance Method Details

#create_stash(create_path) ⇒ Object



6
7
8
9
# File 'lib/sensu-cli/editor.rb', line 6

def create_stash(create_path)
  file = temp_file(:path => create_path, :content => { :timestamp => Time.now.to_i })
  edit(file)
end

#edit(file) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/sensu-cli/editor.rb', line 11

def edit(file)
  editor = ENV['EDITOR'] ? ENV['EDITOR'] : 'vi'
  system("#{editor} #{file}")
  begin
    JSON.parse(File.read(file))
  rescue JSON::ParserError
    SensuCli::die(1, 'The stash you created has invalid JSON.')
  end
end

#temp_file(template) ⇒ Object



21
22
23
24
25
26
# File 'lib/sensu-cli/editor.rb', line 21

def temp_file(template)
  file = Tempfile.new('sensu')
  file.write(JSON.pretty_generate(template))
  file.close
  file.path
end