Browse Source

Merge pull request #187 from nickbabcock/config-arg

Allow specifying config path when ran interactive
Nick Babcock 4 năm trước cách đây
mục cha
commit
8bda1830fb
2 tập tin đã thay đổi với 50 bổ sung7 xóa
  1. 17 0
      OhmGraphite/CustomConfig.cs
  2. 33 7
      OhmGraphite/Program.cs

+ 17 - 0
OhmGraphite/CustomConfig.cs

@@ -0,0 +1,17 @@
+using System.Configuration;
+
+namespace OhmGraphite.Test
+{
+    class CustomConfig : IAppConfig
+    {
+        private readonly Configuration _config;
+
+        public CustomConfig(Configuration config)
+        {
+            _config = config;
+        }
+
+        public string this[string name] => _config.AppSettings.Settings[name]?.Value;
+        public string[] GetKeys() => _config.AppSettings.Settings.AllKeys;
+    }
+}

+ 33 - 7
OhmGraphite/Program.cs

@@ -1,5 +1,9 @@
-using NLog;
+using System;
+using System.Configuration;
+using System.IO;
+using NLog;
 using LibreHardwareMonitor.Hardware;
+using OhmGraphite.Test;
 using Prometheus;
 using Topshelf;
 
@@ -11,6 +15,7 @@ namespace OhmGraphite
 
         private static void Main()
         {
+            string configPath = string.Empty;
             HostFactory.Run(x =>
             {
                 x.Service<IManage>(s =>
@@ -28,15 +33,19 @@ namespace OhmGraphite
                         IsControllerEnabled = true
                     };
 
-                    // We need to know where the graphite server lives and how often
-                    // to poll the hardware
-                    var config = Logger.LogFunction("parse config", () => MetricConfig.ParseAppSettings(new AppConfigManager()));
-                    var collector = new SensorCollector(computer, config);
-                    var metricsManager = CreateManager(config, collector);
-                    s.ConstructUsing(name => metricsManager);
+                    s.ConstructUsing(name =>
+                    {
+                        var configDisplay = string.IsNullOrEmpty(configPath) ? "default" : configPath;
+                        var config = Logger.LogFunction($"parse config {configDisplay}", () => MetricConfig.ParseAppSettings(CreateConfiguration(configPath)));
+                        var collector = new SensorCollector(computer, config);
+                        return CreateManager(config, collector);
+                    });
                     s.WhenStarted(tc => tc.Start());
                     s.WhenStopped(tc => tc.Dispose());
                 });
+
+                // Allow one to specify a command line argument when running interactively
+                x.AddCommandLineDefinition("config", v => configPath = v);
                 x.UseNLog();
                 x.RunAsLocalSystem();
                 x.SetDescription(
@@ -47,6 +56,23 @@ namespace OhmGraphite
             });
         }
 
+        private static IAppConfig CreateConfiguration(string configPath)
+        {
+            if (string.IsNullOrEmpty(configPath))
+            {
+                return new AppConfigManager();
+            }
+
+            if (!File.Exists(configPath))
+            {
+                throw new ApplicationException($"unable to detect config: ${configPath}");
+            }
+
+            var configMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath };
+            var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
+            return new CustomConfig(config);
+        }
+
         private static IManage CreateManager(MetricConfig config, SensorCollector collector)
         {
             var hostname = config.LookupName();