diff --git a/src/PostIt/Directory.Packages.props b/src/PostIt/Directory.Packages.props index 05fe0e228..42d09cd55 100644 --- a/src/PostIt/Directory.Packages.props +++ b/src/PostIt/Directory.Packages.props @@ -22,6 +22,7 @@ + @@ -30,4 +31,4 @@ - + \ No newline at end of file diff --git a/src/PostIt/PostIt.Desktop/Program.cs b/src/PostIt/PostIt.Desktop/Program.cs index 0de3bd69e..a2b8cd66f 100644 --- a/src/PostIt/PostIt.Desktop/Program.cs +++ b/src/PostIt/PostIt.Desktop/Program.cs @@ -12,6 +12,8 @@ sealed class Program [STAThread] public static void Main(string[] args) { + CLIOptionHandler.HandleArgs(args); + // Short-circuit 2nd-instance launches (OS handing us the // postit://callback URL) BEFORE Avalonia spins up a window. // If we let Avalonia initialise, the new MainWindow flashes diff --git a/src/PostIt/PostIt/App.axaml.cs b/src/PostIt/PostIt/App.axaml.cs index b80351e17..9a04ffd03 100644 --- a/src/PostIt/PostIt/App.axaml.cs +++ b/src/PostIt/PostIt/App.axaml.cs @@ -17,6 +17,7 @@ namespace PostIt; public partial class App : Application { private int _bootStarted; + public string? CliConfigFileSpecification {  get; private set; } /// /// DI container the platform entry points hand to ViewModels so @@ -203,4 +204,9 @@ public partial class App : Application { await View!.NavRoot.PopAsync(); } + + internal void UseConfigFileWhenLoading(string configFile) + { + this.CliConfigFileSpecification = configFile; + } } diff --git a/src/PostIt/PostIt/CLIOptions/CLIOptions.cs b/src/PostIt/PostIt/CLIOptions/CLIOptions.cs new file mode 100644 index 000000000..c0bea85ff --- /dev/null +++ b/src/PostIt/PostIt/CLIOptions/CLIOptions.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using Mono.Options; +using PostIt; +using PostIt.ViewModels; + +public static class CLIOptionHandler +{ + public static void HandleArgs(string[] args) + { + bool showHelp = false; + string configFile = null; + + var options = new OptionSet { + { "c|configuration=", "Configuration file to load", (string v) => configFile = v }, + { "h|help", "Show this message and exit", v => showHelp = true } + }; + + List extra = options.Parse(args); + + if (showHelp) + { + Console.WriteLine("Usage: app [OPTIONS]+"); + options.WriteOptionDescriptions(Console.Out); + return; + } + if (configFile != null) + { + var app = App.Current as App; + app.UseConfigFileWhenLoading(configFile); + } + } +} diff --git a/src/PostIt/PostIt/PostIt.csproj b/src/PostIt/PostIt/PostIt.csproj index 3216cebb2..8d49a9a3e 100644 --- a/src/PostIt/PostIt/PostIt.csproj +++ b/src/PostIt/PostIt/PostIt.csproj @@ -36,6 +36,7 @@ + diff --git a/src/PostIt/PostIt/ViewModels/Settings/Settings.cs b/src/PostIt/PostIt/ViewModels/Settings/Settings.cs index 1c2802e33..5576f01cd 100644 --- a/src/PostIt/PostIt/ViewModels/Settings/Settings.cs +++ b/src/PostIt/PostIt/ViewModels/Settings/Settings.cs @@ -259,25 +259,13 @@ public partial class Settings : ViewModelBase return; } } - if (Environment.GetEnvironmentVariable("POSTIT_SETTINGS_JSON") is string envJson - && !string.IsNullOrWhiteSpace(envJson)) + + var app = App.Current as App; + if (app!=null) + if (app.CliConfigFileSpecification!=null) { - Console.WriteLine("🔎 Loading settings from POSTIT_SETTINGS_JSON environment variable."); - FileInfo configByEnvFileInfo = new FileInfo(envJson); - if (!configByEnvFileInfo.Exists) - { - throw new Exception($"🩎 Settings file not found at {configByEnvFileInfo.FullName}"); - } - string json = File.ReadAllText(configByEnvFileInfo.FullName); - ApplyJson(json, "POSTIT_SETTINGS_JSON"); - SettingsFileFullName = configByEnvFileInfo.FullName; - Loaded = true; - return; + SettingsFileFullName = app.CliConfigFileSpecification; } - string configDir = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - "PostIt" - ); if (SettingsFileFullName is not null) { @@ -287,10 +275,18 @@ public partial class Settings : ViewModelBase else if (Environment.GetEnvironmentVariable("POSTIT_SETTINGS_JSON") is string envPath && !string.IsNullOrWhiteSpace(envPath)) { - SettingsFileFullName = envPath; + FileInfo configByEnvFileInfo = new FileInfo(envPath); + if (!configByEnvFileInfo.Exists) + { + throw new Exception($"🩎 Settings file not found at {configByEnvFileInfo.FullName}"); + } + SettingsFileFullName = configByEnvFileInfo.FullName; } else { + string configDir = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "PostIt"); SettingsFileFullName = Path.Combine(configDir, "postit-settings.json"); } @@ -536,6 +532,7 @@ public partial class Settings : ViewModelBase /// partial void OnIsDirtyChanged(bool value) => SaveCommand.NotifyCanExecuteChanged(); + public override bool CanNavigateNext { get => false; protected set => throw new System.NotImplementedException(); } public override bool CanNavigatePrevious { get => true; protected set => throw new System.NotImplementedException(); } }