RT 6.0.2 Documentation

RT::Interface::CLI

NAME

RT::Interface::CLI - helper functions for creating a commandline RT interface

SYNOPSIS

  use lib "/opt/rt6/local/lib", "/opt/rt6/lib";

  use RT::Interface::CLI  qw(GetCurrentUser Init loc);

  # Create a hash to hold parsed values
  my %OPT = (
      "id" => 1,
  );

  # Process command-line arguments, load the configuration, and connect
  # to the database. See below for options provided by default.
  Init(
      \%OPT,
      "id=i",  # Getopt::Long options
  );

  print "Got an id: " . $OPT{'id'};

  # Get the current user all loaded
  my $CurrentUser = GetCurrentUser();

  print loc('Hello!'); # Synonym of $CurrentUser->loc('Hello!');

DESCRIPTION

This library provides shared functions for bootstrapping RT CLI programs that provide you with a fully functional environment for running code against the RT Perl API. When run, a database connection to the RT database is automatically set up.

Memory Usage

The CLI interface loads a full RT environment, which is convenient when creating command-line utilities because you can just start coding. However, there are parts of RT that are typically not needed in a CLI context and they contribute to the size of the running CLI process. You can reduce the size of these processes by disabling some of these features.

The most effective way to reduce memory usage is to limit the languages loaded by RT. By default, RT loads all available translation lexicons, which can consume 40-50MB of memory. If your CLI script only needs English, you can use:

    --config LexiconLanguages=en

METHODS

GetCurrentUser

Figures out the uid of the current user and returns an RT::CurrentUser object loaded with that user. if the current user isn't found, returns a copy of RT::Nobody.

loc

  Synonym of $CurrentUser->loc().

Init

A shim for "GetOptions" in Getopt::Long which automatically adds a --help option if it is not supplied. It then calls "LoadConfig" in RT and "Init" in RT.

It sets the LogToSTDERR setting to warning, to ensure that the user sees all relevant warnings. It also adds --quiet and --verbose options, which adjust the LogToSTDERR value to error or debug, respectively.

If debug is provided as a parameter, it added as an alias for --verbose.

statement-log provides a command-line version of the $StatementLog option in the main RT config file. This allows users to log SQL for queries run in a CLI script in the same manner as the web UI. It accepts log levels like $StatementLog:

    --statement-log=debug

config provides a generic way to override any RT configuration option for the duration of the CLI script. It accepts key=value pairs where the key is the name of any RT configuration option. You can specify this option multiple times to set different configuration values.

    --config LogLevel=debug
    --config DisableGraphViz=1
    --config LexiconLanguages=en,fr

For array-valued options, use comma-separated values. For scalar options, provide the value directly. Hash-valued options are not fully supported yet.

← Back to index