Class CommandBundle
Describes a collection of files to be uploaded to a Linux server along with the command to be executed after the files have been unpacked.
Implements
Namespace: Neon.SSH
Assembly: Neon.SSH.dll
Syntax
public class CommandBundle : List<CommandFile>, IBashCommandFormatter
Remarks
This class is intended for use with the RunCommand(CommandBundle, RunOptions) and SudoCommand(CommandBundle, RunOptions) methods for situations where one or more files need to be uploaded to a cluster node and be used when a command is executed.
To use this class, construct an instance passing the command and arguments to be executed. The command be an absolute reference to an executable in folders such as /bin or /usr/local/bin, an executable somewhere on the current PATH, or relative to the files unpacked from the bundle. The current working directory will be set to the folder where the bundle was unpacked, so you can reference local executables like ./MyExecutable.
Once a bundle is constructed, you will add CommandFile instances specifying the file data you want to include. These include the relative path to the file to be uploaded as well as its text or binary data. You may also indicate whether each file is to be marked as executable.
Constructors
CommandBundle(string, params object[])
Constructor.
Declaration
public CommandBundle(string command, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
string | command | The command. |
object[] | args | The command arguments or |
Remarks
The args
parameter optionally specifies an array of
command argument objects. With a few exceptions, these arguments will
be passed to the command by rendering the object into a string
by calling its ToString() method. null
and empty string arguments will be ignored and IEnumerable<T>
arguments will be expanded.
bool
and double
arguments get special treatment. bool
values will be rendered as true
or false
and double
arguments will be rendered using double.ToString("#.0")
. If you
need something different, you can convert your arguments to strings first.
Fields
ArgBreak
This is a meta command line argument that can be added to a command to indicate that the following non-command line option is not to be considered to be the value for the previous command line option.
This is entirely optional but can make ToBash(string) formatting a bit nicer.
Declaration
public const string ArgBreak = "-!arg-break!-"
Field Value
Type | Description |
---|---|
string |
Properties
Args
Returns the command arguments.
Declaration
public object[] Args { get; }
Property Value
Type | Description |
---|---|
object[] |
Command
Returns the command to be executed after the bundle has been unpacked.
Declaration
public string Command { get; }
Property Value
Type | Description |
---|---|
string |
Methods
AddFile(string, byte[], bool)
Adds a binary file to be uploaded before executing the command.
Declaration
public CommandBundle AddFile(string path, byte[] data, bool isExecutable = false)
Parameters
Type | Name | Description |
---|---|---|
string | path | The file path relative to the directory where the command will be executed. |
byte[] | data | The file data. |
bool | isExecutable | Optionally specifies that the file is to be marked as executable. |
Returns
Type | Description |
---|---|
CommandBundle |
Remarks
TheCommandBundle for fluent style programming.
AddFile(string, string, bool, bool)
Adds a text file to be uploaded before executing the command.
Declaration
public CommandBundle AddFile(string path, string text, bool isExecutable = false, bool linuxCompatible = true)
Parameters
Type | Name | Description |
---|---|---|
string | path | The file path relative to the directory where the command will be executed. |
string | text | The file text. |
bool | isExecutable | Optionally specifies that the file is to be marked as executable. |
bool | linuxCompatible | Optionally controls whether the text is made Linux compatible by removing carriage returns
and expanding TABs into spaces. This defaults to |
Returns
Type | Description |
---|---|
CommandBundle |
Remarks
The CommandBundle for fluent style programming.
AddZip(string, string)
Creates a ZIP file, recursively adding all of the files in a local source folder and then adds the ZIP file to the bundle.
Declaration
public CommandBundle AddZip(string path, string sourceFolder)
Parameters
Type | Name | Description |
---|---|---|
string | path | The file path relative to the directory where the command will be executed. |
string | sourceFolder | Path to the local source folder containing the files to be zipped. |
Returns
Type | Description |
---|---|
CommandBundle |
Remarks
The CommandBundle for fluent style programming.
FromScript(string)
Creates a bundle that simply uploads and runs a (string
) script.
Declaration
public static CommandBundle FromScript(string script)
Parameters
Type | Name | Description |
---|---|---|
string | script | The script text. |
Returns
Type | Description |
---|---|
CommandBundle | The CommandBundle. |
FromScript(StringBuilder)
Creates a bundle that simply uploads and runs a (StringBuilder) script.
Declaration
public static CommandBundle FromScript(StringBuilder script)
Parameters
Type | Name | Description |
---|---|---|
StringBuilder | script | The script text. |
Returns
Type | Description |
---|---|
CommandBundle | The CommandBundle. |
NormalizeArgs(IEnumerable<object>, bool)
Normalizes the bundle command arguments into a single list of strings by expanding any arguments that can enumerate strings, normalizing common value types like booleans, and adding surrounding quotes if necessary.
Declaration
public static List<string> NormalizeArgs(IEnumerable<object> args, bool keepArgBreaks = false)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<object> | args | The arguments to be normalized. |
bool | keepArgBreaks | Optionally specifies that any ArgBreak arguments are included in the output. |
Returns
Type | Description |
---|---|
List<string> | List of normalized arguments. |
ToBash(string)
Formats the command such that it could be added to a Bash script.
note
This doesn't work if the command has attached files.
Declaration
public string ToBash(string comment = null)
Parameters
Type | Name | Description |
---|---|---|
string | comment | Optional comment text (without a leading #). |
Returns
Type | Description |
---|---|
string | The command formatted for Bash. |
Remarks
This can be useful for making copies of cluster configuration commands on the server as scripts for situations where system operators need to manually tweak things.
Exceptions
Type | Condition |
---|---|
NotSupportedException | Thrown because ToBash(string) does not support commands with attached files. |
ToString()
Renders the command and arguments as a Bash compatible command line.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | The command line. |
Overrides
Validate()
Verifies that the bundle is valid.
Declaration
public void Validate()
Exceptions
Type | Condition |
---|---|
InvalidOperationException | Thrown when the bundle is not valid. |