Class: SSHKit::Command

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

Overview

Author:

  • Lee Hambley

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Command

Initialize a new Command object

command name, with optional variadaric args nothing in stdin or stdout

Parameters:

  • A (Array)

    list of arguments, the first is considered to be the

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
# File 'lib/sshkit/command.rb', line 45

def initialize(*args)
  raise ArgumentError, "May not pass no arguments to Command.new" if args.empty?
  @options = args.extract_options!
  @command = args.shift.to_s.strip.to_sym
  @args    = args
  @options.symbolize_keys!
  sanitize_command!
  @stdout, @stderr = String.new, String.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



34
35
36
# File 'lib/sshkit/command.rb', line 34

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



34
35
36
# File 'lib/sshkit/command.rb', line 34

def command
  @command
end

#exit_statusObject

Returns the value of attribute exit_status.



34
35
36
# File 'lib/sshkit/command.rb', line 34

def exit_status
  @exit_status
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/sshkit/command.rb', line 34

def options
  @options
end

#startedObject

Returns the value of attribute started.



34
35
36
# File 'lib/sshkit/command.rb', line 34

def started
  @started
end

#started_atObject (readonly)

Returns the value of attribute started_at.



34
35
36
# File 'lib/sshkit/command.rb', line 34

def started_at
  @started_at
end

#stderrObject

Returns the value of attribute stderr.



36
37
38
# File 'lib/sshkit/command.rb', line 36

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



36
37
38
# File 'lib/sshkit/command.rb', line 36

def stdout
  @stdout
end

Instance Method Details

#complete?Boolean Also known as: finished?

Returns:

  • (Boolean)


55
56
57
# File 'lib/sshkit/command.rb', line 55

def complete?
  !exit_status.nil?
end

#failure?Boolean Also known as: failed?

Returns:

  • (Boolean)


78
79
80
# File 'lib/sshkit/command.rb', line 78

def failure?
  exit_status.to_i > 0
end

#hostObject



104
105
106
# File 'lib/sshkit/command.rb', line 104

def host
  options[:host]
end

#runtimeObject



88
89
90
91
# File 'lib/sshkit/command.rb', line 88

def runtime
  return nil unless complete?
  @finished_at - @started_at
end

#started?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sshkit/command.rb', line 60

def started?
  started
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


73
74
75
# File 'lib/sshkit/command.rb', line 73

def success?
  exit_status.nil? ? false : exit_status.to_i == 0
end

#to_hashObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/sshkit/command.rb', line 93

def to_hash
  {
    command:     command,
    args:        args,
    options:     options,
    exit_status: exit_status,
    stdout:      stdout,
    stderr:      stderr
  }
end

#to_sObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sshkit/command.rb', line 108

def to_s
  return command.to_s if command.match /\s/
  String.new.tap do |cs|
    if options[:in]
      cs << sprintf("cd %s && ", options[:in])
    end
    if options[:env]
      cs << '( '
      options[:env].each do |k,v|
        cs << k.to_s.upcase
        cs << "="
        cs << v.to_s.shellescape
      end
      cs << ' '
    end
    if options[:user]
      cs << "sudo su #{options[:user]} -c "
    end
    cs << SSHKit.config.command_map[command.to_sym]
    if args.any?
      cs << ' '
      cs << args.join(' ')
    end
    if options[:env]
      cs << ' )'
    end
  end
end

#uuidObject



69
70
71
# File 'lib/sshkit/command.rb', line 69

def uuid
  @uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7]
end