metafacture-tutorial

Lesson 7: Using Metafacture as Command Line Tool

Get Metafacture Runner as CLI Tool

While we had fun with our Metafacture Playground another way to use Metafacture is the command line. For running a Metafacture flux process we need a terminal and installed JAVA 11 ore higher. For creating and editing Flux and Fix files we need an texteditor like Codium/VS Code or others.

For this lesson basic knowledge of the commandline is recommended.

Check if Java 11 or higher is installed with java -version in your terminal. If not, install JAVA 11 or higher.

To use Metafacture on the commandline we can download the latest runner of Metafacture Fix:

https://github.com/metafacture/metafacture-fix/releases

Unzip the downloaded metafix-runner distribution to your choosen folder

How to run Metafacture via CLI

You can run your workflows:

Unix:

./bin/metafix-runner path/to/your.flux

or Windows:

./bin/metafix-runner.bat path/to/your.flux

(Hint: You need to know the path to your file to run the function.)

To get quick started let’s revisit a Flux we toyed around with in the playground. The playground has a nice feature to export and import Metafacture Workflows.

So lets go to the Playground.

Export the workflow with the Export Button and lets run the flux.

Shows Export Button in Playground.

Linux:

./bin/metafix-runner downloads/playground.flux

or Windows:

./bin/metafix-runner.bat downloads/playground.flux

The result of running the Flux-Script via CLI should be the same as with the Playground.

The Metafacture CLI Tool expects a flux file for every workflow. Our runned workflow only has the following flux and no additional files since it i querring data from the web and it has no fix transformations.

"https://weather-proxy.freecodecamp.rocks/api/current?lat=50.93414&lon=6.93147"
| open-http
| as-lines
| decode-json
| encode-yaml
| print
;

Use local files for transformation

If you want to load a local file instead of fetching data from the web we need to change the flux a little bit with an texteditor. Download the following file 11942150X.json and adjust the path to your file.

Adjust your downloads/playground.flux script:

"path/to/your/file/11942150X.json" // Adjust your path!
| open-file
| as-lines
| decode-json
| encode-yaml
| print
;

Run it again as shown above.

If we want to use fix we need to refrence the fix file that in the playground we only refrenced via |fix

"path/to/your/file/11942150X.json"
| open-file
| as-lines
| decode-json
| fix("path/to/your/fixFile.fix")
| encode-yaml
| print
;

Create a new file with a fixFile.fix, files with fix scripts should have a .fix file suffix.

Add the follwoing line as content:

retain("preferredName","id","type[]")

Save it in the same folder as the flux file. (Hint: It does not always have to be in the same folder.)

Use variables

Hint: You can use the varliable FLUX_DIR to shorten the file path if the file is in the same folder as the flux-file.

FLUX_DIR + "file.json"
| open-file
| as-lines
| decode-json
| fix(FLUX_DIR + "fixFile.fix")
| encode-yaml
| print
;

If you are using variables, that are not defined in the flux, you can pass them on with the CLI:

e.g.

FILE
| open-file
| as-lines
| decode-json
| fix(FLUX_DIR + "fixFile.json")
| encode-yaml
| print
;

You could use:

./bin/metafix-runner path/to/your.flux FILE="path/to/your/file.json"

TODO: Give homework: - Provide a file or a file-folder. - Give a homework. - Give the solution.

Next lesson: 07 Processing MARC