Skip to content

Commit

Permalink
Added functionality for reading key from file
Browse files Browse the repository at this point in the history
  • Loading branch information
flostadler committed Aug 1, 2018
1 parent 6300a3b commit 2e7fa12
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions Licensing.Cli/Service/CliParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using Fclp;
using Licensing.Cli.Utility;
Expand All @@ -11,6 +12,8 @@ public class CliParser : ICliParser
private readonly FluentCommandLineParser _parser;
private readonly Product _product;
private readonly License _license;
private bool _keySet;
private string _keyPath = null;

public CliParser()
{
Expand All @@ -26,8 +29,14 @@ public CliParser()
.Callback(text => Console.WriteLine(text));

_parser.Setup<string>('k', "key")
.Callback(privatKey => _product.PrivateKey = privatKey)
.Required();
.Callback(privatKey =>
{
_product.PrivateKey = privatKey;
_keySet = true;
});

_parser.Setup<string>('p', "key-path")
.Callback(path => _keyPath = path);

_parser.Setup<string>('n', "name")
.Callback(name => _license.OwnerName = name)
Expand Down Expand Up @@ -89,13 +98,47 @@ public bool Parse(string[] args, out Product product, out License license)
}
else
{
if (!_keySet && String.IsNullOrEmpty(_keyPath))
{
Console.WriteLine("No key provided, please add a key!");
return false;
}
if (_keySet && !String.IsNullOrEmpty(_keyPath))
{
Console.WriteLine("Key and path to key was set, please provide only one of these options!");
return false;
}
if (!_keySet)
{
if (!File.Exists(_keyPath))
{
Console.WriteLine("Key path doesn't point to an existing file");
return false;
}

try
{
_product.PrivateKey = File.ReadAllText(_keyPath);
}
catch (Exception e)
{
Console.WriteLine("An error happened while reading the key file\n" +
"Detailed error message:\n" +
e);
return false;
}
}

product = _product;
license = _license;
return true;
}
}
catch (Exception e)
{
Console.WriteLine("An unknown error happed!\n" +
"Detailed error message:\n" +
e);
return false;
}
}
Expand Down

0 comments on commit 2e7fa12

Please sign in to comment.