Class: Jumpstarter::CommandRunner

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

Class Method Summary collapse

Class Method Details

._execute(command: nil, error: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jumpstarter_core/commandRunner.rb', line 69

def _execute(command: nil, error:nil)

    print_all = true
    prefix ||= {}

    output = []

    Writer.write(message: "#{command}")

    begin
        status = Jumpstarter::JumpstarterPty.spawn(command) do |command_stdout, command_stdin, pid|
        command_stdout.each do |l|
            line = l.strip # strip so that \n gets removed
            output << line

            next unless print_all

            # Prefix the current line with a string
            prefix.each do |element|
                line = element[:prefix] + line if element[:block] && element[:block].call(line)
            end
            Writer.write(message: "#{line}")
        end
    end
    rescue => ex
        # FastlanePty adds exit_status on to StandardError so every error will have a status code
        status = ex.exit_status

        # This could happen when the environment is wrong:
        # > invalid byte sequence in US-ASCII (ArgumentError)
        output << ex.to_s
        o = output.join("\n")
        puts(o)
        if error
            error.call(o, nil)
        else
            raise ex
        end
    end

    # Exit status for build command, should be 0 if build succeeded
    if status != 0
        o = output.join("\n")
        puts(o)
        Writer.write(message: ("Exit status: #{status}"))
        if error
            error.call(o, status)
        else
            Writer.write(message: ("Exit status: #{status}"))
        end
    end 
    return output.join("\n")
end

.execute(command: nil, error: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jumpstarter_core/commandRunner.rb', line 57

def execute(command: nil, error:nil)
    output = []
    if command.is_a? String
        output <<  _execute(command: command, error: error) 
    else
        command.command!.each do |item|
            output <<  _execute(command: item, error: error) 
        end
    end
    return output.join("\n")
end

.execute_sudo(command: nil) ⇒ Object



53
54
55
# File 'lib/jumpstarter_core/commandRunner.rb', line 53

def execute_sudo(command: nil)
    system("sudo #{command}")
end