A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.cfengine.com/latest/reference-functions-readdata.html below:

readdata - CFEngine 3.26 Docs

Prototype: readdata(filename, filetype)

Return type: data

Description: Parses CSV, JSON, or YAML data from file filename and returns the result as a data variable.

When filetype is auto, the file type is guessed from the extension (ignoring case): .csv means CSV; .json means JSON; .yaml or .yml means YAML. If the file doesn't match any of those names, JSON is used.

When filetype is CSV,JSON,YAML or ENV, this function behaves like readcsv(), readjson(), readyaml() or readenvfile() respectively. These functions have an optional parameter maxbytes (default: inf). maxbytes can not be set using readdata(), if needed use one of the mentioned functions instead.

Arguments:

Example:

Prepare:

code

echo -n 1,2,3 > /tmp/file.csv
echo -n '{ "x": 200 }' > /tmp/file.json
echo '- a' > /tmp/file.yaml
echo '- b' >> /tmp/file.yaml

Run:

code

bundle agent main
{
  vars:

      "csv" data => readdata("/tmp/file.csv", "auto"); # or file type "CSV"
      "json" data => readdata("/tmp/file.json", "auto"); # or file type "JSON"

      "csv_str" string => format("%S", csv);
      "json_str" string => format("%S", json);

    feature_yaml:: # we can only test YAML data if libyaml is compiled in
      "yaml" data => readdata("/tmp/file.yaml", "auto"); # or file type "YAML"
      "yaml_str" string => format("%S", yaml);
  reports:

      "From /tmp/file.csv, got data $(csv_str)";
      "From /tmp/file.json, got data $(json_str)";
    feature_yaml::
      "From /tmp/file.yaml, we would get data $(yaml_str)";
    !feature_yaml:: # show the output anyway
      'From /tmp/file.yaml, we would get data ["a","b"]';

}

Output:

code

R: From /tmp/file.csv, got data [["1","2","3"]]
R: From /tmp/file.json, got data {"x":200}
R: From /tmp/file.yaml, we would get data ["a","b"]

See also: data_expand(), readcsv(), readyaml(), readjson(), readenvfile(), validdata(), data documentation.

History: Was introduced in 3.7.0.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4