Module: Wisco::Commands::Exec

Defined in:
lib/wisco/commands/exec.rb

Class Method Summary collapse

Class Method Details

.execute_one(section, key, input_file, fixtures_dir, connector_full_path, connection, debug: false) ⇒ Object



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
122
123
# File 'lib/wisco/commands/exec.rb', line 90

def execute_one(section, key, input_file, fixtures_dir, connector_full_path, connection, debug: false)
  stem        = File.basename(input_file, '.*')
  output_file = File.join(fixtures_dir, "output_#{stem}.json")
  error_file  = File.join(fixtures_dir, "error_#{stem}.txt")

  options = { connector: connector_full_path, input: input_file, output: output_file }
  options[:connection] = connection if connection

  if debug
    warn "[exec] path:       #{section}.#{key}.execute"
    warn "[exec] connector:  #{connector_full_path}"
    warn "[exec] connection: #{connection.inspect}"
    warn "[exec] input:      #{input_file}"
    warn "[exec] output:     #{output_file}"
  end

  begin
    cmd = Workato::CLI::ExecCommand.new(path: "#{section}.#{key}.execute", options: options)
    cmd.call
  rescue StandardError => e
    File.write(error_file, "#{e.class}: #{e.message}\n\n#{e.backtrace.join("\n")}\n")
    warn "Error executing #{section}.#{key} with #{File.basename(input_file)}: #{e.message}"
    warn "  Details written to: #{error_file}"
    return
  end

  FileUtils.rm_f(error_file)

  return unless File.exist?(output_file)

  pretty = JSON.pretty_generate(JSON.parse(File.read(output_file)))
  File.write(output_file, pretty + "\n")
  puts "  Written: #{output_file}"
end

.file_has_sentinel?(path) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'lib/wisco/commands/exec.rb', line 81

def file_has_sentinel?(path)
  first_line = begin
    File.open(path, &:readline).chomp
  rescue StandardError
    ''
  end
  first_line == Wisco::Commands::Fixtures::SENTINEL
end

.resolve_input_files(input, fixtures_dir) ⇒ Object

Resolve the list of input files to execute. If an explicit input filename/path is given, use that (relative to fixtures_dir). Otherwise glob execute_* in fixtures_dir and exclude files still containing the sentinel.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wisco/commands/exec.rb', line 66

def resolve_input_files(input, fixtures_dir)
  if input
    path = File.absolute_path?(input) ? input : File.join(fixtures_dir, input)
    unless File.exist?(path)
      warn "Error: Input file not found: #{path}"
      exit 1
    end
    [path]
  else
    Dir.glob(File.join(fixtures_dir, 'execute_*')).select do |f|
      File.file?(f) && !file_has_sentinel?(f)
    end
  end
end

.run(path_arg, target_dir, input: nil, debug: false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wisco/commands/exec.rb', line 13

def run(path_arg, target_dir, input: nil, debug: false)
  target_dir = File.expand_path(target_dir)
  config_path = Wisco.config_path(target_dir)

  unless File.exist?(config_path)
    warn "Error: No #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} found in #{target_dir}."
    warn "       Run '#{Wisco::CLI_NAME} init' first."
    exit 1
  end

  config = Wisco::Config.load_config(config_path)
  connector_path = config.dig('connector', 'path')
  connector_file = config.dig('connector', 'file')

  if connector_path.nil? || connector_file.nil?
    warn "Error: #{Wisco::WISCO_DIR}/#{Wisco::CONFIG_FILENAME} is missing connector path/file. Run '#{Wisco::CLI_NAME} init' again."
    exit 1
  end

  connector_full_path = File.join(connector_path, connector_file)
  connection = config['connection']

  connector = Wisco::Connector.load_connector_from_config(target_dir)
  pairs = Wisco::PathUtils.parse_path(path_arg, connector)

  pairs.each do |section, key|
    puts "Executing #{section}.#{key}"
    fixtures_dir = File.join(target_dir, 'fixtures', section, key)
    fixture_dir_output = fixtures_dir.sub(connector_path, '.')

    unless File.directory?(fixtures_dir)
      warn "Error: fixtures directory not found: #{fixture_dir_output}"
      warn "       Run '#{Wisco::CLI_NAME} fixtures #{section}.#{key}' first."
      next
    end

    input_files = resolve_input_files(input, fixtures_dir)

    if input_files.empty?
      warn "\tWarning: No ready input files found in #{fixture_dir_output}"
      next
    end

    input_files.each do |input_file|
      execute_one(section, key, input_file, fixtures_dir,
                  connector_full_path, connection, debug: debug)
    end
  end
end