OpenrelikWorkerCommon.TaskUtils (openrelik_worker_common v0.1.0)

Copy Markdown View Source

Helper functions for tasks (openrelik_worker_common.task_utils).

Provides Base64/JSON serialization of task results, input file resolution from upstream piped results (get_input_files/3), and glob-based file compatibility filtering (filter_compatible_files/2).

Summary

Functions

Creates a task result dictionary and encodes it to a Base64 string.

Decodes a Base64-encoded JSON string into an Elixir map.

Encodes a map (dictionary) to a Base64-encoded JSON string.

Filters a list of file dictionaries based on compatibility with filter_dict, supporting glob (fnmatch) matching on "data_types", "mime_types", and "filenames".

Matches a string against a Unix shell-style wildcard pattern (fnmatch.fnmatch).

Fetches a field from a map by string key or atom key.

Functions

create_task_result(opts)

@spec create_task_result(keyword() | map()) :: String.t()

Creates a task result dictionary and encodes it to a Base64 string.

Can be called with a keyword list or map:

create_task_result(
  output_files: output_files,
  workflow_id: workflow_id,
  task_files: [],
  command: command,
  meta: meta,
  file_reports: [],
  task_report: nil
)

or with positional arguments matching Python's signature:

create_task_result(
  output_files,
  workflow_id,
  task_files \\ [],
  command \\ nil,
  meta \\ nil,
  file_reports \\ [],
  task_report \\ nil
)

create_task_result(output_files, workflow_id)

@spec create_task_result(list(), String.t() | integer() | nil) :: String.t()

create_task_result(output_files, workflow_id, opts_or_task_files)

@spec create_task_result(
  list(),
  String.t() | integer() | nil,
  keyword() | map() | list()
) :: String.t()

create_task_result(output_files, workflow_id, task_files, command, meta \\ nil, file_reports \\ [], task_report \\ nil)

@spec create_task_result(
  list(),
  String.t() | integer() | nil,
  list(),
  String.t() | nil,
  map() | nil,
  list(),
  map() | nil
) :: String.t()

decode_base64_to_dict(encoded_result)

@spec decode_base64_to_dict(String.t()) :: map()

Decodes a Base64-encoded JSON string into an Elixir map.

encode_dict_to_base64(dict_to_encode)

@spec encode_dict_to_base64(map()) :: String.t()

Encodes a map (dictionary) to a Base64-encoded JSON string.

filter_compatible_files(input_files, filter_dict)

@spec filter_compatible_files([map()], map()) :: [map()]

Filters a list of file dictionaries based on compatibility with filter_dict, supporting glob (fnmatch) matching on "data_types", "mime_types", and "filenames".

fnmatch?(value, pattern)

@spec fnmatch?(String.t(), String.t()) :: boolean()

Matches a string against a Unix shell-style wildcard pattern (fnmatch.fnmatch).

get_field(map, key)

@spec get_field(map(), String.t()) :: term()

Fetches a field from a map by string key or atom key.

get_input_files(pipe_result, input_files \\ [], filter \\ nil)

@spec get_input_files(String.t() | [String.t()] | nil, list() | nil, map() | nil) ::
  list() | term()

Prepares the input files for a task.

Determines the appropriate input files by checking for results from a previous task (pipe_result as a single Base64-encoded string or a list of Base64-encoded strings) and then applies any specified file filtering (filter).