Class: Exec::ExecutableCommand Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/exec/executable_command.rb

Overview

This class is abstract.

Subclass and override exec and set_options to implement

Basic class of an executable command. It contains every attributes and methods all executable command needs.

Defined Under Namespace

Classes: CustomCommandOption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin, stdout, stderr, command_name, command_option_class = CustomCommandOption) ⇒ ExecutableCommand (protected)

Default constructor of the class.

Parameters:

  • argv

    The arguments of the command.

  • stdin (IO)

    The input stream of the command.

  • stdout (IO)

    The output stream of the command.

  • stderr (IO)

    The error stream of the command.

  • command_name (String)

    The name of the executed command.

  • command_option_class (CommandOption) (defaults to: CustomCommandOption)

    The CommandOption object.



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/exec/executable_command.rb', line 101

def initialize(argv, stdin, stdout, stderr, command_name, command_option_class = CustomCommandOption)
  @command_name = command_name
  @argv = argv
  @stdin = stdin
  @stdout = stdout
  @stderr = stderr
  @cloudbox_home = ENV['CLOUDBOX_HOME']

  @options = command_option_class.new(@command_name, @argv)
  @options.add_option("h", "help", "Display help.", false)
end

Instance Attribute Details

#argvObject (protected)

The passed arguments when calling the command.



25
26
27
# File 'lib/exec/executable_command.rb', line 25

def argv
  @argv
end

#command_nameObject (protected)

The name of the command.



23
24
25
# File 'lib/exec/executable_command.rb', line 23

def command_name
  @command_name
end

#loggerObject (protected)

The cloudbox logger object, used for logging.



21
22
23
# File 'lib/exec/executable_command.rb', line 21

def logger
  @logger
end

#optionsObject (protected)

The CommandOption object which describe option behavior



33
34
35
# File 'lib/exec/executable_command.rb', line 33

def options
  @options
end

#stderrObject (protected)

The error stream of the command.



31
32
33
# File 'lib/exec/executable_command.rb', line 31

def stderr
  @stderr
end

#stdinObject (protected)

The input stream of the command.



27
28
29
# File 'lib/exec/executable_command.rb', line 27

def stdin
  @stdin
end

#stdoutObject (protected)

The output stream of the command.



29
30
31
# File 'lib/exec/executable_command.rb', line 29

def stdout
  @stdout
end

#valuesObject (protected)

The values Hash obtained with option passed to the command



35
36
37
# File 'lib/exec/executable_command.rb', line 35

def values
  @values
end

Instance Method Details

#check_parametersObject (protected)

This method just call setOptions method and assumes print infos into logger and stdout.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/exec/executable_command.rb', line 150

def check_parameters
  @logger.begin_main_step("parameters checking")
  Color::print_log("NONE", "checking parameters...", @stdout)

  begin
    set_options()

    @values = @options.verify()
  rescue CloudboxError => e
    Color::echo_fail(@stdout)
    raise e
  end

  @logger.end_main_step("parameters checking")
  Color::echo_ok(@stdout)
end

#create_loggerObject (protected)

Create a logger which logs into the default log path/command_name. If an error occurred, it will create a mocked logger.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/exec/executable_command.rb', line 135

def create_logger
  begin
    Color::print_log("NONE", "Loading logger...", @stdout)
    @logger = Common::CloudboxLogger.new(@command_name)
    Color::echo_ok(@stdout)
  rescue => e
    Color::echo_fail
    @logger = Common::CloudboxLoggerMock.new
    Color::color_red("Init CloudboxLogger error: #{e.message}", @stdout)
  end
end

#execObject (protected)

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/exec/executable_command.rb', line 127

def exec
  raise NotImplementedError
end

#runObject

This function is the common execution of an executable command. It loads the librairies, creater the logger, calls the setOptions function and finally calls the exec function (the effective execution of the command).



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/exec/executable_command.rb', line 43

def run
  begin
    begin

      create_logger()
      #check_environment_variables()
      check_parameters()

      @logger.begin_execution(self.argv)

      if @values["help"]
        @stdout.puts(@options.get_help())
      else
        exec()
      end

    rescue StandardError => e
      @logger.error(e.message)
      @logger.error(e.backtrace.join("\n"))
      @stdout.puts(Color::color_red("Execution failed"))
      @stdout.puts(e.message)
      raise e
    end
  rescue LoadError
    @logger.error(e.message)
    @logger.error(e.backtrace.join("\n"))
    exit 254
  rescue Common::ParameterError => e
    @stdout.puts e.usage
    @logger.error(e.message)
    @logger.error(e.backtrace.join("\n"))
    exit 1
  rescue Common::CloudboxError
    @logger.error(e.message)
    @logger.error(e.backtrace.join("\n"))
    exit 2
  rescue StandardError => e
    @stdout.puts e.class
    @logger.error(e.message)
    @logger.error(e.backtrace.join("\n"))
    exit 254
  else
    @logger.end_execution()
  end

  @stdout.puts(Color::color_green("Execution success"))

end

#set_optionsObject (protected)

Note:

This method has to be overrided by the inheriting class to add some options.

Declare specific options needed by the command.

Raises:

  • (NotImplementedError)

    When the method is not overrided by the inheriting class.



118
119
120
# File 'lib/exec/executable_command.rb', line 118

def set_options
  raise NotImplementedError
end