Skip to content

community: add execCommunicate function to libUtils.js

Diego Dario requested to merge dario/gjs-guide:main into main

This commit adds a new function called execCommunicate to the libUtils.js file in the src/extensions/community directory. The execCommunicate function allows for the asynchronous execution of a command and returns the output from stdout on success or throws an error with the output from stderr on failure.

The function takes three parameters: argv, input, and cancellable. The argv parameter is a list of string arguments, input is optional and represents the input to write to stdin, and cancellable is an optional cancellable object that can be used to stop the process before it finishes.

The function initializes a new Gio.Subprocess object with the provided arguments and flags. If an input is provided, the Gio.SubprocessFlags.STDIN_PIPE flag is added. The function then communicates with the subprocess asynchronously using the communicate_utf8_async method. On completion, the function checks the exit status of the process. If the status is not 0, an error is thrown with the appropriate error code and message. Otherwise, the output from stdout is resolved and returned.

In addition, the function handles cancellation by connecting to the cancellable object and disconnecting when the process finishes.

This new function will be useful for executing commands asynchronously and retrieving the output in various scenarios within the GNOME Extensions Community project.

Merge request reports