Module: Browserctl::Commands::Workflow

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

Constant Summary collapse

USAGE =
"Usage: browserctl workflow <run|list|describe|generate|promote> [args]"
EXIT_CODE =
{ clean: 0, drift: 2, fail: 1 }.freeze

Constants included from CliOutput

CliOutput::AUTH_REQUIRED_EXIT_CODE

Class Method Summary collapse

Methods included from CliOutput

exit_code_for, print_result

Class Method Details

.run(runner, args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/browserctl/commands/workflow.rb', line 16

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)
  when "generate" then run_generate(args)
  when "promote"  then run_promote(args)
  else abort "unknown workflow subcommand '#{sub}'\n#{USAGE}"
  end
end

.run_describe(runner, args) ⇒ Object



117
118
119
120
# File 'lib/browserctl/commands/workflow.rb', line 117

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_generate(args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/browserctl/commands/workflow.rb', line 60

def self.run_generate(args)
  name = args.shift or abort \
    "usage: browserctl workflow generate <recording> [--out PATH]"
  out_idx = args.index("--out")
  out = if out_idx
          args.delete_at(out_idx + 1).tap { args.delete_at(out_idx) }
        else
          File.join(".browserctl/workflows", "#{name}.rb")
        end
  FileUtils.mkdir_p(File.dirname(out))
  Browserctl::Recording.generate_workflow(name, output_path: out, keep_log: true)
  puts JSON.generate({ ok: true, name: name, path: out })
rescue StandardError => e
  abort "Error generating workflow: #{e.message}"
end

.run_list(runner) ⇒ Object



112
113
114
115
# File 'lib/browserctl/commands/workflow.rb', line 112

def self.run_list(runner)
  list = runner.list_workflows
  puts JSON.generate({ workflows: list.map { |w| { name: w[:name], desc: w[:desc] } } })
end

.run_promote(args) ⇒ Object



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

def self.run_promote(args)
  name = args.shift or abort \
    "usage: browserctl workflow promote <name> [--force] [--threshold N] [--as-flow]"

  force   = !args.delete("--force").nil?
  as_flow = !args.delete("--as-flow").nil?

  threshold_idx = args.index("--threshold")
  threshold = if threshold_idx
                val = args.delete_at(threshold_idx + 1)
                args.delete_at(threshold_idx)
                Integer(val)
              else
                Browserctl::Workflow::PromotionLedger::DEFAULT_THRESHOLD
              end

  result = Browserctl::Workflow::Promoter.promote(
    workflow: name, force: force, threshold: threshold, as_flow: as_flow
  )
  puts JSON.generate(ok: true, **result)
rescue Browserctl::Workflow::Promoter::IneligibleError => e
  puts JSON.generate(
    ok: false, error: "ineligible",
    message: e.message, streak: e.streak, threshold: e.threshold
  )
  exit 1
rescue Browserctl::Workflow::Promoter::NotFoundError => e
  abort "Error: #{e.message}"
rescue ArgumentError => e
  abort "Error: invalid --threshold value: #{e.message}"
end

.run_workflow(runner, args) ⇒ Object



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

def self.run_workflow(runner, args)
  name = args.shift or abort \
    "usage: browserctl workflow run <name|file> [--check] [--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

  check = !args.delete("--check").nil?

  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)
  verdict = runner.run_workflow(name, check: check, **params)
  exit(EXIT_CODE.fetch(verdict, 1))
end