How I can to get custom options in the `init` function of the Tarantool Cartridge role?
Asked Answered
R

1

6

A Tarantool Cartridge role file has a function init.

I want to get my custom options from the instance.yml file. But the opts variable doesn't have it.

How I can do it?

Ramires answered 27/5, 2020 at 10:25 Comment(0)
M
9

Cartridge has a built-in module called "argparse". It parses a few sources of configuration and combines them together:

  • instances.yml or files in /etc/tarantool/conf.d
  • command line arguments
  • environment variables starting with TARANTOOL_

Cartridge uses this module to get various pieces of configuration like the port numbers or maximum allowed memory usage. But it doesn't stop you from putting anything you like in those files, as long as it doesn't clash with built-in parameters.

Here's what you chould have in your init():

local argparse = require('cartridge.argparse')

-- ...

local function init()
    local args = argparse.parse()

    log.info("My parameter: %s", args.my_parameter) -- use anything you want in place of my_parameter
end
Minor answered 27/5, 2020 at 10:33 Comment(1)
I would also mention that it supports simple type checking abilities: local my_parameter, err = argparse.get_opts({my_parameter = 'number'}). It'll try to convert value to number, and return an error if it fails.Russi

© 2022 - 2024 — McMap. All rights reserved.