Class: Kanrisuru::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
13
# File 'lib/kanrisuru/command.rb', line 8

def initialize(command)
  @valid_exit_codes = [0]

  @raw_command = command
  @raw_result = []
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



5
6
7
# File 'lib/kanrisuru/command.rb', line 5

def exit_status
  @exit_status
end

#programObject (readonly)

Returns the value of attribute program.



5
6
7
# File 'lib/kanrisuru/command.rb', line 5

def program
  @program
end

#raw_resultObject (readonly)

Returns the value of attribute raw_result.



5
6
7
# File 'lib/kanrisuru/command.rb', line 5

def raw_result
  @raw_result
end

#remote_env=(value) ⇒ Object (writeonly)

Sets the attribute remote_env

Parameters:

  • value

    the value to set the attribute remote_env to.



6
7
8
# File 'lib/kanrisuru/command.rb', line 6

def remote_env=(value)
  @remote_env = value
end

#remote_path=(value) ⇒ Object (writeonly)

Sets the attribute remote_path

Parameters:

  • value

    the value to set the attribute remote_path to.



6
7
8
# File 'lib/kanrisuru/command.rb', line 6

def remote_path=(value)
  @remote_path = value
end

#remote_shell=(value) ⇒ Object (writeonly)

Sets the attribute remote_shell

Parameters:

  • value

    the value to set the attribute remote_shell to.



6
7
8
# File 'lib/kanrisuru/command.rb', line 6

def remote_shell=(value)
  @remote_shell = value
end

#remote_user=(value) ⇒ Object (writeonly)

Sets the attribute remote_user

Parameters:

  • value

    the value to set the attribute remote_user to.



6
7
8
# File 'lib/kanrisuru/command.rb', line 6

def remote_user=(value)
  @remote_user = value
end

Instance Method Details

#+(other) ⇒ Object



76
77
78
# File 'lib/kanrisuru/command.rb', line 76

def +(other)
  append_value(other)
end

#<<(value) ⇒ Object



80
81
82
# File 'lib/kanrisuru/command.rb', line 80

def <<(value)
  append_value(value)
end

#append_arg(arg, string) ⇒ Object



102
103
104
# File 'lib/kanrisuru/command.rb', line 102

def append_arg(arg, string)
  @raw_command = Kanrisuru::Util.present?(string) ? "#{@raw_command} #{arg} #{string}" : @raw_command
end

#append_array(value, separator = ' ') ⇒ Object



92
93
94
95
96
# File 'lib/kanrisuru/command.rb', line 92

def append_array(value, separator = ' ')
  return unless Kanrisuru::Util.present?(value)

  append_value(Kanrisuru::Util.array_join_string(value, separator))
end

#append_flag(arg, boolean = 'true') ⇒ Object



106
107
108
# File 'lib/kanrisuru/command.rb', line 106

def append_flag(arg, boolean = 'true')
  @raw_command = Kanrisuru::Util.present?(boolean) ? "#{@raw_command} #{arg}" : @raw_command
end

#append_flag_no(arg, boolean = 'true') ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/kanrisuru/command.rb', line 110

def append_flag_no(arg, boolean = 'true')
  return if boolean.nil?

  if boolean
    append_flag(arg, true)
  else
    append_flag("no#{arg}", true)
  end
end

#append_valid_exit_code(code) ⇒ Object



120
121
122
123
# File 'lib/kanrisuru/command.rb', line 120

def append_valid_exit_code(code)
  @valid_exit_codes << code if code.instance_of?(Integer)
  @valid_exit_codes.concat(code) if code.instance_of?(Array)
end

#append_value(value) ⇒ Object



98
99
100
# File 'lib/kanrisuru/command.rb', line 98

def append_value(value)
  @raw_command = "#{@raw_command} #{value}"
end

#failure?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/kanrisuru/command.rb', line 19

def failure?
  !success?
end

#handle_data(data) ⇒ Object



68
69
70
# File 'lib/kanrisuru/command.rb', line 68

def handle_data(data)
  @raw_result.push(data)
end

#handle_signal(signal) ⇒ Object



72
73
74
# File 'lib/kanrisuru/command.rb', line 72

def handle_signal(signal)
  @signal = signal
end

#handle_status(status) ⇒ Object



64
65
66
# File 'lib/kanrisuru/command.rb', line 64

def handle_status(status)
  @exit_status = status
end

#pipe(value) ⇒ Object



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

def pipe(value)
  append_value("| #{value}")
end

#prepared_commandObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kanrisuru/command.rb', line 44

def prepared_command
  if !@remote_user.nil? && !@remote_shell.nil?
    evaluate = ''
    evaluate += if Kanrisuru::Util.present?(@remote_path)
                  "cd #{@remote_path} && #{@raw_command}"
                else
                  @raw_command.to_s
                end

    env = @remote_env && !@remote_env.empty? ? "#{@remote_env} " : ''
    "#{env}sudo -u #{@remote_user} #{@remote_shell} -c \"#{evaluate}\""
  else
    @raw_command
  end
end

#raw_commandObject



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

def raw_command
  @raw_command.to_s
end

#success?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/kanrisuru/command.rb', line 15

def success?
  @valid_exit_codes.include?(@exit_status)
end

#to_aObject



39
40
41
42
# File 'lib/kanrisuru/command.rb', line 39

def to_a
  string = @raw_result.join
  string.lines.map(&:strip)
end

#to_fObject



27
28
29
# File 'lib/kanrisuru/command.rb', line 27

def to_f
  to_a.join.to_f
end

#to_iObject



23
24
25
# File 'lib/kanrisuru/command.rb', line 23

def to_i
  to_a.join.to_i
end

#to_json(*_args) ⇒ Object



35
36
37
# File 'lib/kanrisuru/command.rb', line 35

def to_json(*_args)
  JSON.parse(to_s)
end

#to_sObject



31
32
33
# File 'lib/kanrisuru/command.rb', line 31

def to_s
  to_a.join(' ')
end

#|(other) ⇒ Object



84
85
86
# File 'lib/kanrisuru/command.rb', line 84

def |(other)
  pipe(other)
end