ballerina/os Ballerina library
Overview
This module provides APIs to retrieve information about the environment variables and the current users of the Operating System.
The APIs are platform-independent, i.e., uniform across all operating systems.
For information on the operations, which you can perform with the os
module, see the below Functions.
Functions
exec
function exec(Command command, *EnvProperties envProperties) returns Process|Error
Executes an operating system command as a subprocess of the current process.
os:Process|os:Error result = os:exec({value: "bal", arguments: ["run", filepath]}, BAL_CONFIG_FILE = "/abc/Config.toml");
Parameters
- command Command - The command to be executed
- envProperties *EnvProperties - The environment properties
getEnv
Returns the environment variable value associated with the provided name.
string port = os:getEnv("HTTP_PORT");
Parameters
- name string - Name of the environment variable
Return Type
- string - Environment variable value if it exists or else, an empty string
getUserHome
function getUserHome() returns string
Returns the current user's home directory path.
string userHome = os:getUserHome();
Return Type
- string - Current user's home directory if it can be determined or else, an empty string
getUsername
function getUsername() returns string
Returns the current user's name.
string username = os:getUsername();
Return Type
- string - Current user's name if it can be determined or else, an empty string
listEnv
Returns a map of environment variables.
map<string> envs = os:listEnv();
setEnv
Sets the value of the environment variable named by the key. Note that the parameter key cannot be an empty string or "==" sign.
os:Error? err = os:setEnv("BALCONFIGFILE", "/path/to/Config.toml");
Parameters
- key string - Key of the environment variable
- value string - Value of the environment variable
Return Type
- Error? - An
os:Error
if setting the environment variable fails, () otherwise
unsetEnv
Removes a single environment variable from the system if it exists. Note that the parameter key cannot be an empty string.
os:Error? err = os:unsetEnv("BALCONFIGFILE");
Parameters
- key string - Key of the environment variable
Return Type
- Error? - An
os:Error
if unsetting the environment variable fails, () otherwise
Classes
os: Process
This object contains information on a process being created from Ballerina.
This is returned from the exec
function in the os
module.
waitForExit
Waits for the process to finish its work and exit. This will return 0 if successful, or a different value during failure depending on the operating system.
int|os:Error exitCode = process.waitForExit();
output
function output(FileOutputStream fileOutputStream) returns byte[]|Error
Returns the standard output as default. Option provided to return standard error by providing file descriptor. If the process was not finished and exited explicitly by running process.waitForExit(), then process.output() will finish the work and exit and return the output.
byte[]|os:Error err = process.output(io:stderr);
Parameters
- fileOutputStream FileOutputStream (default io:stdout) - The output stream (
io:stdout
orio:stderr
) content needs to be returned
Return Type
- byte[]|Error - The
byte[]
, which represents the process's 'standard error', or the 'standard out', or an Error
exit
function exit()
Terminates the process.
process.exit();
Records
os: Command
Fields
- value string -
- arguments string[](default []) -
os: EnvProperties
Fields
- command never? -
- anydata... - Rest field
Errors
os: Error
Represents OS module related errors.
os: ProcessExecError
Process Execution error that returns when the os:Exec function fails.
Import
import ballerina/os;
Metadata
Released date: about 1 year ago
Version: 1.8.0
License: Apache-2.0
Compatibility
Platform: java17
Ballerina version: 2201.8.0
GraalVM compatible: Yes
Pull count
Total: 391654
Current verison: 32925
Weekly downloads
Keywords
environment
Contributors
Dependencies