diff --git a/src/licenseGen/Program.cs b/src/licenseGen/Program.cs index 0040e69..fb35fe8 100644 --- a/src/licenseGen/Program.cs +++ b/src/licenseGen/Program.cs @@ -13,9 +13,24 @@ namespace bitwardenSelfLicensor static int Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); - var cert = app.Option("-c | --cert", "cert file", CommandOptionType.SingleValue); + var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue); var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue); + bool certExists() { + return File.Exists(cert.Value()); + } + + bool coreExists() { + return File.Exists(coreDll.Value()); + } + + bool verifyTopOptions() + { + return !string.IsNullOrWhiteSpace(cert.Value()) && + !string.IsNullOrWhiteSpace(coreDll.Value()) && + certExists() && coreExists(); + } + app.Command("user", config => { var name = config.Argument("Name", "your name"); @@ -25,13 +40,22 @@ namespace bitwardenSelfLicensor config.OnExecute(() => { - if (!cert.HasValue() || !coreDll.HasValue()) + if (!verifyTopOptions()) { - app.ShowHelp(); + if(!coreExists()) + { + config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); + } + if (!certExists()) { + config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); + } + + config.ShowHelp(); return 1; } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value)) { + config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.ShowHelp("user"); return 1; } @@ -51,16 +75,24 @@ namespace bitwardenSelfLicensor config.OnExecute(() => { - if (!cert.HasValue() || !coreDll.HasValue()) + if (!verifyTopOptions()) { - app.ShowHelp(); + if(!coreExists()) + { + config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); + } + if (!certExists()) { + config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); + } + + config.ShowHelp(); return 1; } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value) || string.IsNullOrWhiteSpace(installId.Value)) { - config.Error.WriteLine("Missing arguments"); + config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'"); config.ShowHelp("org"); return 1; } @@ -68,7 +100,9 @@ namespace bitwardenSelfLicensor if (!Guid.TryParse(installId.Value, out Guid installationId)) { config.Error.WriteLine("Unable to parse your installation id as a GUID"); + config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}"); config.ShowHelp("org"); + return 1; } GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId, key.Value); @@ -125,7 +159,7 @@ namespace bitwardenSelfLicensor set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0]))); set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert }))); - Console.WriteLine(JsonConvert.SerializeObject(license)); + Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented)); } static void GenerateOrgLicense(X509Certificate2 cert, string corePath, @@ -167,7 +201,7 @@ namespace bitwardenSelfLicensor set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0]))); set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert }))); - Console.WriteLine(JsonConvert.SerializeObject(license)); + Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented)); } } } diff --git a/src/licenseGen/build.sh b/src/licenseGen/build.sh index 46bc85b..c52062e 100755 --- a/src/licenseGen/build.sh +++ b/src/licenseGen/build.sh @@ -7,5 +7,5 @@ cd $script_dir dotnet restore dotnet publish -docker build . -t bitbetter/licenseGen # --squash +docker build . -t bitbetter/licensegen # --squash diff --git a/src/licenseGen/run.sh b/src/licenseGen/run.sh index d1fd856..3174ade 100755 --- a/src/licenseGen/run.sh +++ b/src/licenseGen/run.sh @@ -2,10 +2,11 @@ script_dir=`cd $(dirname $0); pwd` -if [ "$#" -ne "1" ]; then - echo "USAGE: $0 " +if [ "$#" -lt "1" ]; then + echo "USAGE: $0 [License Gen args...]" exit 1 fi - -docker run -it -v "$1:/cert.pfx" bitbetter/licensegen +cert_path=$1 +shift +docker run -it -v "$cert_path:/cert.pfx" bitbetter/licensegen "$@"