Class: Pdi::Spoon

Inherits:
Object
  • Object
show all
Defined in:
lib/pdi/spoon.rb,
lib/pdi/spoon/parser.rb,
lib/pdi/spoon/result.rb,
lib/pdi/spoon/options.rb,
lib/pdi/spoon/pan_error.rb,
lib/pdi/spoon/options/arg.rb,
lib/pdi/spoon/kitchen_error.rb,
lib/pdi/spoon/options/level.rb,
lib/pdi/spoon/options/param.rb

Overview

This class is the main wrapper for PDI’s pan and kitchen scripts.

Defined Under Namespace

Classes: KitchenError, Options, PanError, Parser, Result

Constant Summary collapse

DEFAULT_KITCHEN =
'kitchen.sh'
DEFAULT_PAN =
'pan.sh'
TYPES_TO_ERRORS =
{
  Options::Type::JOB => KitchenError,
  Options::Type::TRANSFORMATION => PanError
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args: [], dir:, kitchen: DEFAULT_KITCHEN, pan: DEFAULT_PAN, timeout_in_seconds: nil) ⇒ Spoon

Returns a new instance of Spoon.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pdi/spoon.rb', line 29

def initialize(
  args: [],
  dir:,
  kitchen: DEFAULT_KITCHEN,
  pan: DEFAULT_PAN,
  timeout_in_seconds: nil
)
  assert_required(:dir, dir)
  assert_required(:kitchen, kitchen)
  assert_required(:pan, pan)

  @args           = Array(args)
  @dir            = File.expand_path(dir.to_s)
  @kitchen        = kitchen.to_s
  @pan            = pan.to_s
  @executor       = Executor.new(timeout_in_seconds: timeout_in_seconds)
  @parser         = Parser.new

  freeze
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



27
28
29
# File 'lib/pdi/spoon.rb', line 27

def args
  @args
end

#dirObject (readonly)

Returns the value of attribute dir.



27
28
29
# File 'lib/pdi/spoon.rb', line 27

def dir
  @dir
end

#executorObject (readonly)

Returns the value of attribute executor.



27
28
29
# File 'lib/pdi/spoon.rb', line 27

def executor
  @executor
end

#kitchenObject (readonly)

Returns the value of attribute kitchen.



27
28
29
# File 'lib/pdi/spoon.rb', line 27

def kitchen
  @kitchen
end

#panObject (readonly)

Returns the value of attribute pan.



27
28
29
# File 'lib/pdi/spoon.rb', line 27

def pan
  @pan
end

Instance Method Details

#run(options, &streaming_reader) ⇒ Object

Returns a Pdi::Executor::Result instance when PDI returns error code 0 or else raises a PanError (transformation) or KitchenError (job).

An optional block may be passed in so that the output of a run is available in a streaming manner during the run. This block takes one parameter which is the current chunk of output and is called repeatedly throughout the run.



70
71
72
73
74
75
76
77
# File 'lib/pdi/spoon.rb', line 70

def run(options, &streaming_reader)
  options  = Options.make(options)
  all_args = run_args(options)

  executor.run(all_args, &streaming_reader).tap do |result|
    raise(error_constant(options), result) if result.code != 0
  end
end

#versionObject

Returns a Spoon::Result instance when PDI returns error code 0 or else raises a KitchenError since Kitchen was used to run the version command.

Raises:



52
53
54
55
56
57
58
59
60
61
# File 'lib/pdi/spoon.rb', line 52

def version
  final_args = [kitchen_path] + args + [Options::Arg.new(Options::Arg::Key::VERSION)]

  result       = executor.run(final_args)
  version_line = parser.version(result.out)

  raise(KitchenError, result) if result.code != 0

  Result.new(result, version_line)
end