Class: Pasqual::Command

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program, username, password, host, port, name, file = nil) ⇒ Command

Returns a new instance of Command.



14
15
16
17
18
19
20
21
22
# File 'lib/pasqual/command.rb', line 14

def initialize(program, username, password, host, port, name, file = nil)
  self.program = program
  self.username = username
  self.password = password
  self.host = host
  self.port = port
  self.name = name
  self.file = file
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def file
  @file
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def host
  @host
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def name
  @name
end

#outputObject

Returns the value of attribute output.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def output
  @output
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def password
  @password
end

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def port
  @port
end

#programObject

Returns the value of attribute program.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def program
  @program
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def status
  @status
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/pasqual/command.rb', line 11

def username
  @username
end

Class Method Details

.execute(program, username, password, host, port, name, file = nil) ⇒ Object



7
8
9
# File 'lib/pasqual/command.rb', line 7

def self.execute(program, username, password, host, port, name, file = nil)
  new(program, username, password, host, port, name, file).tap { |c| c.execute }
end

Instance Method Details

#executeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pasqual/command.rb', line 24

def execute
  outfile = Tempfile.new("pasqual-#{name}")
  outfile.sync = true

  process = ChildProcess.build program,
    *Arglist.args(username, password, host, port, name)

  process.io.stdout = process.io.stderr = outfile

  process.duplex = true if file

  # TODO: find out why piping to stdin doesn't work :(
  ENV['PGPASSWORD'] = password
  process.start

  if file && File.exist?(file)
    File.open file do |f|
      process.io.stdin.puts f.read
      process.io.stdin.flush
    end
    process.io.stdin.close
  elsif file
    process.io.stdin.puts file
    process.io.stdin.flush
    process.io.stdin.close
  end

  process.poll_for_exit(30)
  ENV['PGPASSWORD'] = nil

  outfile.rewind
  self.output = outfile.read
  self.status = process.exit_code
end

#success?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/pasqual/command.rb', line 59

def success?
  status == 0
end