Update winsw to work with both XML and YAML config

Add logic to select configuration type
Remove redundant codes for config file finding
pull/641/head
Buddhika Chathuranga 2020-08-08 23:16:55 +05:30
parent 3a5db8a61d
commit 588a5ed455
5 changed files with 58 additions and 58 deletions

View File

@ -21,6 +21,7 @@ using log4net.Layout;
using WinSW.Configuration; using WinSW.Configuration;
using WinSW.Logging; using WinSW.Logging;
using WinSW.Native; using WinSW.Native;
using WinSW.Util;
using WMI; using WMI;
using ServiceType = WMI.ServiceType; using ServiceType = WMI.ServiceType;
@ -63,8 +64,20 @@ namespace WinSW
{ {
bool inConsoleMode = argsArray.Length > 0; bool inConsoleMode = argsArray.Length > 0;
string baseName;
DirectoryInfo d;
var configType = GetConfigType(out baseName, out d);
// If descriptor is not specified, initialize the new one (and load configs from there) // If descriptor is not specified, initialize the new one (and load configs from there)
descriptor ??= new ServiceDescriptor(); if (configType == ConfigType.XML)
{
descriptor ??= new ServiceDescriptor(baseName, d);
}
else
{
descriptor ??= new ServiceDescriptorYaml(baseName, d).Configurations;
}
// Configure the wrapper-internal logging. // Configure the wrapper-internal logging.
// STDOUT and STDERR of the child process will be handled independently. // STDOUT and STDERR of the child process will be handled independently.
@ -658,6 +671,39 @@ namespace WinSW
} }
} }
private static ConfigType GetConfigType(out string baseName, out DirectoryInfo d)
{
var executablePath = new DefaultWinSWSettings().ExecutablePath;
baseName = Path.GetFileNameWithoutExtension(executablePath);
if (baseName.EndsWith(".vshost"))
{
baseName = baseName.Substring(0, baseName.Length - 7);
}
d = new DirectoryInfo(Path.GetDirectoryName(executablePath));
while (true)
{
if (File.Exists(Path.Combine(d.FullName, baseName + ".xml")))
{
return ConfigType.XML;
}
if (File.Exists(Path.Combine(d.FullName, baseName + ".yml")))
{
return ConfigType.YAML;
}
if (d.Parent is null)
{
throw new FileNotFoundException("Unable to locate " + baseName + ".xml " + "or " + baseName + ".yml " + "file within executable directory or any parents");
}
d = d.Parent;
}
}
private static void PrintHelp() private static void PrintHelp()
{ {
Console.WriteLine("A wrapper binary that can be used to host executables as Windows services"); Console.WriteLine("A wrapper binary that can be used to host executables as Windows services");

View File

@ -73,11 +73,6 @@ namespace WinSW
eventLogProvider.Service = this; eventLogProvider.Service = this;
} }
public WrapperService()
: this(new ServiceDescriptor())
{
}
/// <summary> /// <summary>
/// Process the file copy instructions, so that we can replace files that are always in use while /// Process the file copy instructions, so that we can replace files that are always in use while
/// the service runs. /// the service runs.

View File

@ -41,34 +41,8 @@ namespace WinSW
// Currently there is no opportunity to alter the executable path // Currently there is no opportunity to alter the executable path
public virtual string ExecutablePath => Defaults.ExecutablePath; public virtual string ExecutablePath => Defaults.ExecutablePath;
public ServiceDescriptor() public ServiceDescriptor(string baseName, DirectoryInfo d)
{ {
// find co-located configuration xml. We search up to the ancestor directories to simplify debugging,
// as well as trimming off ".vshost" suffix (which is used during debugging)
// Get the first parent to go into the recursive loop
string p = this.ExecutablePath;
string baseName = Path.GetFileNameWithoutExtension(p);
if (baseName.EndsWith(".vshost"))
{
baseName = baseName.Substring(0, baseName.Length - 7);
}
DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p));
while (true)
{
if (File.Exists(Path.Combine(d.FullName, baseName + ".xml")))
{
break;
}
if (d.Parent is null)
{
throw new FileNotFoundException("Unable to locate " + baseName + ".xml file within executable directory or any parents");
}
d = d.Parent;
}
this.BaseName = baseName; this.BaseName = baseName;
this.BasePath = Path.Combine(d.FullName, this.BaseName); this.BasePath = Path.Combine(d.FullName, this.BaseName);

View File

@ -7,35 +7,12 @@ namespace WinSW
{ {
public class ServiceDescriptorYaml public class ServiceDescriptorYaml
{ {
public readonly YamlConfiguration Configurations = new YamlConfiguration(); public readonly YamlConfiguration Configurations;
public static DefaultWinSWSettings Defaults { get; } = new DefaultWinSWSettings(); public static DefaultWinSWSettings Defaults { get; } = new DefaultWinSWSettings();
public ServiceDescriptorYaml() public ServiceDescriptorYaml(string baseName, DirectoryInfo d)
{ {
string p = Defaults.ExecutablePath;
string baseName = Path.GetFileNameWithoutExtension(p);
if (baseName.EndsWith(".vshost"))
{
baseName = baseName.Substring(0, baseName.Length - 7);
}
DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p));
while (true)
{
if (File.Exists(Path.Combine(d.FullName, baseName + ".yml")))
{
break;
}
if (d.Parent is null)
{
throw new FileNotFoundException("Unable to locate " + baseName + ".yml file within executable directory or any parents");
}
d = d.Parent;
}
var basepath = Path.Combine(d.FullName, baseName); var basepath = Path.Combine(d.FullName, baseName);
using (var reader = new StreamReader(basepath + ".yml")) using (var reader = new StreamReader(basepath + ".yml"))

View File

@ -0,0 +1,8 @@
namespace WinSW.Util
{
public enum ConfigType
{
XML,
YAML
}
}