using System.CommandLine; using System.CommandLine.Binding; namespace Duplicati.CommandLine.ServerUtil; /// /// Binds settings from command line options. /// public class SettingsBinder : BinderBase { /// /// The password option. /// public static readonly Option passwordOption = new Option("--password", description: "The password to use for connecting to the server", getDefaultValue: () => null); /// /// The host URL option. /// public static readonly Option hostUrlOption = new Option("--hosturl", description: "The host URL to use", getDefaultValue: () => new Uri("http://localhost:8200")); /// /// The server datafolder option. /// public static readonly Option serverDatafolderOption = new Option("--server-datafolder", description: "The server datafolder to use for locating the database", getDefaultValue: () => null); /// /// The settings file option. /// public static readonly Option settingsFileOption = new Option("--settings-file", description: "The settings file to use", getDefaultValue: () => null); /// /// The settings file option. /// public static readonly Option insecureOption = new Option("--insecure", description: "Accepts any TLS/SSL certificate (dangerous)", getDefaultValue: () => false); /// /// Adds global options to the root command. /// /// The root command to add the options to. /// The root command with the options added. public static RootCommand AddGlobalOptions(RootCommand rootCommand) { rootCommand.AddGlobalOption(passwordOption); rootCommand.AddGlobalOption(hostUrlOption); rootCommand.AddGlobalOption(serverDatafolderOption); rootCommand.AddGlobalOption(settingsFileOption); rootCommand.AddGlobalOption(insecureOption); return rootCommand; } /// /// Gets the settings instance from the binding context. /// /// The binding context to get the settings from. /// The settings instance. public static Settings GetSettings(BindingContext bindingContext) => Settings.Load( bindingContext.ParseResult.GetValueForOption(passwordOption), bindingContext.ParseResult.GetValueForOption(hostUrlOption), bindingContext.ParseResult.GetValueForOption(serverDatafolderOption)?.FullName, bindingContext.ParseResult.GetValueForOption(settingsFileOption)?.FullName ?? "settings.json", bindingContext.ParseResult.GetValueForOption(insecureOption) ); /// /// Gets the bound value. /// /// The binding context to get the value from. /// The bound value. protected override Settings GetBoundValue(BindingContext bindingContext) => GetSettings(bindingContext); }