Module: Browserctl::Commands::Workflow

Extended by:
CliOutput
Defined in:
lib/browserctl/commands/workflow.rb

Constant Summary collapse

USAGE =
"Usage: browserctl workflow <run|list|describe> [args]"

Class Method Summary collapse

Methods included from CliOutput

print_result

Class Method Details

.run(runner, args) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/browserctl/commands/workflow.rb', line 12

def self.run(runner, args)
  sub = args.shift or abort USAGE
  case sub
  when "run"      then run_workflow(runner, args)
  when "list"     then run_list(runner)
  when "describe" then run_describe(runner, args)
  else abort "unknown workflow subcommand '#{sub}'\n#{USAGE}"
  end
end

.run_describe(runner, args) ⇒ Object



58
59
60
61
# File 'lib/browserctl/commands/workflow.rb', line 58

def self.run_describe(runner, args)
  name = args.shift or abort "usage: browserctl workflow describe <name>"
  puts JSON.pretty_generate(runner.describe_workflow(name))
end

.run_list(runner) ⇒ Object



53
54
55
56
# File 'lib/browserctl/commands/workflow.rb', line 53

def self.run_list(runner)
  list = runner.list_workflows
  list.each { |w| puts "#{w[:name].ljust(24)} #{w[:desc]}" }
end

.run_workflow(runner, args) ⇒ Object



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
# File 'lib/browserctl/commands/workflow.rb', line 22

def self.run_workflow(runner, args)
  name = args.shift or abort "usage: browserctl workflow run <name|file> [--params file] [--key value ...]"
  if File.exist?(name)
    before = Browserctl.registry_snapshot.keys
    load File.expand_path(name)
    name = (Browserctl.registry_snapshot.keys - before).first || File.basename(name, ".rb")
  end

  params_file_idx = args.index("--params")
  file_params = {}
  if params_file_idx
    params_path = args.delete_at(params_file_idx + 1)
    args.delete_at(params_file_idx)
    begin
      file_params = Browserctl::Runner.load_params_file(params_path)
    rescue StandardError => e
      abort "Error loading params file: #{e.message}"
    end
  end

  cli_params = {}
  args.each_slice(2) do |flag, val|
    key = flag.sub(/\A--/, "").to_sym
    cli_params[key] = val
  end

  params = file_params.merge(cli_params)
  success = runner.run_workflow(name, **params)
  exit(success ? 0 : 1)
end