Class: Browserctl::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/browserctl/runner.rb

Constant Summary collapse

SEARCH_PATHS =
[
  "./.browserctl/workflows",
  File.expand_path("~/.browserctl/workflows")
].freeze
SAFE_WORKFLOW_NAME =
/\A[a-zA-Z0-9_-]+\z/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_params_file(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/browserctl/runner.rb', line 43

def self.load_params_file(path)
  raise "params file not found: #{path}" unless File.exist?(path)

  case File.extname(path).downcase
  when ".yml", ".yaml"
    require "yaml"
    YAML.safe_load_file(path, symbolize_names: true)
  when ".json"
    JSON.parse(File.read(path), symbolize_names: true)
  else
    raise "unsupported params file format: #{path} (use .yml, .yaml, or .json)"
  end
rescue Psych::SyntaxError => e
  raise "invalid YAML in #{path}: #{e.message}"
rescue JSON::ParserError => e
  raise "invalid JSON in #{path}: #{e.message}"
end

Instance Method Details

#describe_workflow(name) ⇒ Hash

Returns detailed information about a workflow.

Parameters:

  • name (String)

    workflow name

Returns:

  • (Hash)

    ‘{ name:, desc:, params:, steps: }`



36
37
38
39
# File 'lib/browserctl/runner.rb', line 36

def describe_workflow(name)
  defn = fetch_workflow(name)
  { name: defn.name, desc: defn.description, params: format_params(defn), steps: defn.steps.map(&:label) }
end

#list_workflowsArray<Hash>

Lists all registered workflows from the standard search paths.

Returns:

  • (Array<Hash>)

    array of ‘{ name:, desc: }` hashes



28
29
30
31
# File 'lib/browserctl/runner.rb', line 28

def list_workflows
  load_all_workflows
  Browserctl.registry_snapshot.map { |name, defn| { name: name, desc: defn.description } }
end

#run_workflow(name, **params) ⇒ Boolean

Runs a named workflow with the given parameters.

Parameters:

  • name (String)

    workflow name (must match /A+z/)

  • params (Hash)

    keyword arguments passed to the workflow

Returns:

  • (Boolean)

    true if all steps succeeded

Raises:



19
20
21
22
23
24
# File 'lib/browserctl/runner.rb', line 19

def run_workflow(name, **params)
  defn    = fetch_workflow(name)
  results = defn.call(params, Client.new)
  print_results(results)
  results.all?(&:ok)
end