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
66 lines
2.9 KiB
C#
66 lines
2.9 KiB
C#
using System.CommandLine;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
|
|
/// <summary>
|
|
/// Factory methods for the global CLI options. Centralising option construction keeps
|
|
/// validation rules (e.g. the accepted <c>--format</c> values) in one place and makes
|
|
/// them testable without standing up the whole command tree.
|
|
/// </summary>
|
|
internal static class CliOptions
|
|
{
|
|
/// <summary>
|
|
/// Creates the global <c>--format</c> option. The option deliberately has no
|
|
/// <c>DefaultValueFactory</c> — format precedence (explicit flag → config/env →
|
|
/// <c>"json"</c>) is resolved by <see cref="CommandHelpers.ResolveFormat"/>, which
|
|
/// needs to distinguish an absent flag. The accepted values are constrained so a
|
|
/// typo (e.g. <c>--format tabel</c>) is rejected with a clear parse error rather
|
|
/// than silently falling through to JSON.
|
|
/// </summary>
|
|
/// <returns>The configured <c>--format</c> option constrained to "json" or "table".</returns>
|
|
/// <summary>
|
|
/// The global <c>--password-file</c> option: reads the LDAP password from a file or stdin
|
|
/// (ScadaBridge#137). Shared by every root so <see cref="CommandHelpers.ResolvePassword"/>
|
|
/// can read it without threading another option through each command group.
|
|
/// </summary>
|
|
internal static Option<string?> PasswordFileOption { get; } =
|
|
ValueSourceOptions.CreateFileOption("--password", "the LDAP password", recursive: true);
|
|
|
|
/// <summary>
|
|
/// Adds <see cref="PasswordFileOption"/> to the root and rejects it alongside an inline
|
|
/// <c>--password</c> on every command. Call it after every command group is added.
|
|
/// </summary>
|
|
/// <param name="root">The CLI root command.</param>
|
|
/// <param name="passwordOption">The inline <c>--password</c> option already on the root.</param>
|
|
internal static void AddPasswordFileOption(Command root, Option<string> passwordOption)
|
|
{
|
|
root.Add(PasswordFileOption);
|
|
|
|
// System.CommandLine runs only the innermost command's validators, so the check goes on
|
|
// every command in the tree. Call this after all command groups are registered.
|
|
void AddTo(Command command)
|
|
{
|
|
command.Validators.Add(result =>
|
|
{
|
|
if (result.GetResult(passwordOption) is { Implicit: false } && result.GetResult(PasswordFileOption) is { Implicit: false })
|
|
result.AddError("Use either --password or --password-file, not both.");
|
|
});
|
|
foreach (var sub in command.Subcommands)
|
|
AddTo(sub);
|
|
}
|
|
|
|
AddTo(root);
|
|
}
|
|
|
|
internal static Option<string> CreateFormatOption()
|
|
{
|
|
var formatOption = new Option<string>("--format")
|
|
{
|
|
Description = "Output format (json or table)",
|
|
Recursive = true,
|
|
};
|
|
formatOption.AcceptOnlyFromAmong("json", "table");
|
|
return formatOption;
|
|
}
|
|
}
|