Skip to content

Commit

Permalink
0.4.1 add and run validation on application startup
Browse files Browse the repository at this point in the history
  • Loading branch information
penCsharpener committed Mar 10, 2020
1 parent 113c0c9 commit b724c24
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NetDeepL.Models;
using NetDeepL.TranslationWorker.Abstractions;
using NetDeepL.TranslationWorker.Models.Config;

Expand Down Expand Up @@ -78,19 +79,49 @@ public async Task<ConfigFileRaw> GetConfig()

public async Task ValidateConfigFile()
{
var conf = await GetConfig();
var rawConf = await GetConfig();
var conf = new ConfigFile(rawConf);
var configFileInvalid = false;
conf.SourceLanguage = conf.SourceLanguage.ToUpper();
conf.LanguagesToTranslate = string.Join(",", conf.LanguagesToTranslate.ToUpper().Split(",").Distinct());

if (conf.SourceLanguage.Length != 2)
if (conf.SourceLanguage == Languages.Undefined)
{
Console.WriteLine("Check your source language. Only one source language is allowed and it must be two letter.");
Console.WriteLine("Check your source language. You selection is not supported.");
configFileInvalid = true;
}

if (conf.LanguagesToTranslate.Any(x => x == Languages.Undefined))
{
Console.WriteLine("Check your target language. One or more are not supported.");
configFileInvalid = true;
}

if (conf.DeepLApiKey == Guid.Empty.ToString())
{
Console.WriteLine("It appears you have not set a valid api key. Please check your config file.");
configFileInvalid = true;
}

if (!conf.InputPath.Exists)
{
Console.WriteLine("Your specified input directory does not exist. Do you want to create it now? y/n");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
Directory.CreateDirectory(conf.InputPath.FullName);
}
}

if (!conf.OutputPath.Exists)
{
Console.WriteLine("Your specified output directory does not exist. Do you want to create it now? y/n");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
Directory.CreateDirectory(conf.OutputPath.FullName);
}
}

if (configFileInvalid)
{
Console.WriteLine("There were errors in your configuration. Please correct them and restart the tool.");
Console.ReadLine();
Environment.Exit(0);
}
Expand Down
16 changes: 14 additions & 2 deletions tools/NetDeepL.TranslationWorker/Models/Config/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public ConfigFile(ConfigFileRaw raw)
{
DeepLApiKey = raw.DeepLApiKey;
LanguagesToTranslate = raw.LanguagesToTranslate.Split(',', System.StringSplitOptions.RemoveEmptyEntries)
.Select(x => Enum.Parse<Languages>(x)).ToArray();
SourceLanguage = Enum.Parse<Languages>(raw.SourceLanguage);
.Select(x => EnumParseWithDefault(x.ToUpper(), Languages.Undefined)).Distinct().ToArray();
SourceLanguage = EnumParseWithDefault(raw.SourceLanguage.ToUpper(), Languages.Undefined);
DelayMilliseconds = int.Parse(raw.DelayMilliseconds);
var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
InputPath = string.IsNullOrWhiteSpace(raw.InputPath) ? currentDir : new DirectoryInfo(raw.InputPath);
Expand All @@ -30,5 +30,17 @@ public ConfigFile(ConfigFileRaw raw)
public int DelayMilliseconds { get; }
public DirectoryInfo InputPath { get; }
public DirectoryInfo OutputPath { get; }

private T EnumParseWithDefault<T>(string enumText, T defaultValue) where T : struct
{
try
{
return Enum.Parse<T>(enumText);
}
catch
{
return defaultValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<FileVersion>0.4.0.0</FileVersion>
<AssemblyVersion>0.4.1.0</AssemblyVersion>
<FileVersion>0.4.1.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tools/NetDeepL.TranslationWorker/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void ConfigureServices(IServiceCollection services)
Environment.Exit(0);
}
var conf = configProvider.GetConfig().Result;
configProvider.ValidateConfigFile().GetAwaiter().GetResult();
return NetDeepL.Implementations.NetDeepL.CreateClient(conf.DeepLApiKey, new NetDeepLOptions());
});
}
Expand Down

0 comments on commit b724c24

Please sign in to comment.