e2f433e849
Every CLI option that can carry a secret gets a `--<name>-file <path|->`
sibling ('-' reads stdin), so the value stays out of the process list and
shell history: --password-file (global), --primary-config-file /
--backup-config-file (data-connection create|update), --auth-config-file
(external-system create|update), --connection-string-file (db-connection
create|update), --credentials-file (notification smtp create|update) and
--auth-token-file (notification sms create|update). Follows the existing
--config-file convention; `@path` is avoided because System.CommandLine
expands @-tokens as response files.
The value is read at parse time; a missing/unreadable/empty source, or '-'
on a terminal stdin, is a parse error naming option + path only, never the
content. Inline and file forms are mutually exclusive, at most one option
may read stdin, and one trailing line break is stripped. Inline forms keep
working.
docker/seed-sites.sh and docker-env2/seed-sites.sh now write the MxGateway
config (windev gateway key) to a mode-600 temp file removed by an EXIT trap
and pass --primary-config-file; seed-flow-outbound-demo.sh pipes its rig
connection string via --connection-string-file -. CLI README (new "Secrets
from a file or stdin" section + per-command tables), Component-CLI.md and
both docker READMEs updated.
Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
157 lines
9.1 KiB
C#
157 lines
9.1 KiB
C#
using System.CommandLine;
|
|
using System.CommandLine.Parsing;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
|
|
public static class DataConnectionCommands
|
|
{
|
|
/// <summary>
|
|
/// Builds the <c>data-connection</c> command group and all its subcommands.
|
|
/// </summary>
|
|
/// <param name="urlOption">Global management URL option.</param>
|
|
/// <param name="formatOption">Global output format option.</param>
|
|
/// <param name="usernameOption">Global username option.</param>
|
|
/// <param name="passwordOption">Global password option.</param>
|
|
/// <returns>The configured <c>data-connection</c> <see cref="Command"/> with all subcommands registered.</returns>
|
|
public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var command = new Command("data-connection") { Description = "Manage data connections" };
|
|
command.Add(BuildMove(urlOption, formatOption, usernameOption, passwordOption));
|
|
|
|
command.Add(BuildList(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildGet(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildCreate(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildUpdate(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildDelete(urlOption, formatOption, usernameOption, passwordOption));
|
|
|
|
return command;
|
|
}
|
|
|
|
private static Command BuildMove(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Data connection ID", Required = true };
|
|
var siteOption = new Option<int>("--site-id") { Description = "Destination site ID", Required = true };
|
|
var cmd = new Command("move") { Description = "Move a data connection to another site" };
|
|
cmd.Add(idOption);
|
|
cmd.Add(siteOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new MoveDataConnectionCommand(result.GetValue(idOption), result.GetValue(siteOption)));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Data connection ID", Required = true };
|
|
var cmd = new Command("get") { Description = "Get a data connection by ID" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new GetDataConnectionCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildUpdate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Data connection ID", Required = true };
|
|
var nameOption = new Option<string>("--name") { Description = "Connection name", Required = true };
|
|
var protocolOption = new Option<string>("--protocol") { Description = "Protocol", Required = true };
|
|
var configOption = new Option<string?>("--primary-config", "--configuration") { Description = "Primary configuration JSON" };
|
|
var backupConfigOption = new Option<string?>("--backup-config") { Description = "Backup configuration JSON" };
|
|
var configFileOption = ValueSourceOptions.CreateFileOption(configOption, "the primary configuration JSON");
|
|
var backupConfigFileOption = ValueSourceOptions.CreateFileOption(backupConfigOption, "the backup configuration JSON");
|
|
var failoverRetryOption = new Option<int>("--failover-retry-count") { Description = "Number of retries before failover to backup", DefaultValueFactory = _ => 3 };
|
|
|
|
var cmd = new Command("update") { Description = "Update a data connection" };
|
|
cmd.Add(idOption);
|
|
cmd.Add(nameOption);
|
|
cmd.Add(protocolOption);
|
|
cmd.Add(configOption);
|
|
cmd.Add(backupConfigOption);
|
|
cmd.Add(failoverRetryOption);
|
|
ValueSourceOptions.AddFileOptions(cmd, (configOption, configFileOption, false), (backupConfigOption, backupConfigFileOption, false));
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
var name = result.GetValue(nameOption)!;
|
|
var protocol = result.GetValue(protocolOption)!;
|
|
var config = ValueSourceOptions.GetValue(result, configOption, configFileOption);
|
|
var backupConfig = ValueSourceOptions.GetValue(result, backupConfigOption, backupConfigFileOption);
|
|
var failoverRetryCount = result.GetValue(failoverRetryOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new UpdateDataConnectionCommand(id, name, protocol, config, backupConfig, failoverRetryCount));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var siteIdOption = new Option<int?>("--site-id") { Description = "Filter by site ID" };
|
|
var cmd = new Command("list") { Description = "List data connections" };
|
|
cmd.Add(siteIdOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var siteId = result.GetValue(siteIdOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new ListDataConnectionsCommand(siteId));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildCreate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var siteIdOption = new Option<int>("--site-id") { Description = "Site ID", Required = true };
|
|
var nameOption = new Option<string>("--name") { Description = "Connection name", Required = true };
|
|
var protocolOption = new Option<string>("--protocol") { Description = "Protocol (e.g. OpcUa)", Required = true };
|
|
var configOption = new Option<string?>("--primary-config", "--configuration") { Description = "Primary configuration JSON" };
|
|
var backupConfigOption = new Option<string?>("--backup-config") { Description = "Backup configuration JSON" };
|
|
var configFileOption = ValueSourceOptions.CreateFileOption(configOption, "the primary configuration JSON");
|
|
var backupConfigFileOption = ValueSourceOptions.CreateFileOption(backupConfigOption, "the backup configuration JSON");
|
|
var failoverRetryOption = new Option<int>("--failover-retry-count") { Description = "Number of retries before failover to backup", DefaultValueFactory = _ => 3 };
|
|
|
|
var cmd = new Command("create") { Description = "Create a new data connection" };
|
|
cmd.Add(siteIdOption);
|
|
cmd.Add(nameOption);
|
|
cmd.Add(protocolOption);
|
|
cmd.Add(configOption);
|
|
cmd.Add(backupConfigOption);
|
|
cmd.Add(failoverRetryOption);
|
|
ValueSourceOptions.AddFileOptions(cmd, (configOption, configFileOption, false), (backupConfigOption, backupConfigFileOption, false));
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var siteId = result.GetValue(siteIdOption);
|
|
var name = result.GetValue(nameOption)!;
|
|
var protocol = result.GetValue(protocolOption)!;
|
|
var config = ValueSourceOptions.GetValue(result, configOption, configFileOption);
|
|
var backupConfig = ValueSourceOptions.GetValue(result, backupConfigOption, backupConfigFileOption);
|
|
var failoverRetryCount = result.GetValue(failoverRetryOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new CreateDataConnectionCommand(siteId, name, protocol, config, backupConfig, failoverRetryCount));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildDelete(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "Data connection ID", Required = true };
|
|
var cmd = new Command("delete") { Description = "Delete a data connection" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new DeleteDataConnectionCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
}
|