We have a CLI

This commit is contained in:
Timothy Baldridge 2020-01-31 15:38:56 -07:00
parent 8ce5906a72
commit aa85c0dc8c
7 changed files with 117 additions and 4 deletions

View File

@ -0,0 +1,13 @@
using System;
using Wabbajack.CLI.Verbs;
namespace Wabbajack.CLI
{
public class OptionsDefinition
{
public static Type[] AllOptions = new[]
{
typeof(OptionsDefinition), typeof(Encrypt), typeof(Decrypt)
};
}
}

18
Wabbajack.CLI/Program.cs Normal file
View File

@ -0,0 +1,18 @@
using System;
using CommandLine;
using Wabbajack.CLI.Verbs;
namespace Wabbajack.CLI
{
class Program
{
static int Main(string[] args)
{
return CommandLine.Parser.Default.ParseArguments(args, OptionsDefinition.AllOptions)
.MapResult(
(Encrypt opts) => Encrypt.Run(opts),
(Decrypt opts) => Decrypt.Run(opts),
errs => 1);
}
}
}

View File

@ -0,0 +1,23 @@
using Alphaleonis.Win32.Filesystem;
using CommandLine;
using Wabbajack.Common;
namespace Wabbajack.CLI.Verbs
{
[Verb("decrypt", HelpText = @"Decrypt data from AppData\Local\Wabbajack and store it locally", Hidden = true)]
public class Decrypt
{
[Option('n', "name", Required = true, HelpText = @"Credential to encrypt and store in AppData\Local\Wabbajack")]
public string Name { get; set; }
[Option('o', "output", Required = true, HelpText = @"Output file for the decrypted data")]
public string Output { get; set; }
public static int Run(Decrypt opts)
{
File.WriteAllBytes(opts.Output, Utils.FromEncryptedData(opts.Name));
return 0;
}
}
}

View File

@ -0,0 +1,22 @@
using Alphaleonis.Win32.Filesystem;
using CommandLine;
using Wabbajack.Common;
namespace Wabbajack.CLI.Verbs
{
[Verb("encrypt", HelpText = @"Encrypt local data and store it in AppData\Local\Wabbajack", Hidden = true)]
public class Encrypt
{
[Option('n', "name", Required = true, HelpText = @"Credential to encrypt and store in AppData\Local\Wabbajack")]
public string Name { get; set; }
[Option('i', "input", Required = true, HelpText = @"Source data file name")]
public string Input { get; set; }
public static int Run(Encrypt opts)
{
File.ReadAllBytes(opts.Input).ToEcryptedData(opts.Name);
return 0;
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.7.82" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Wabbajack.Common\Wabbajack.Common.csproj" />
<ProjectReference Include="..\Wabbajack.Lib\Wabbajack.Lib.csproj" />
</ItemGroup>
</Project>

View File

@ -1082,6 +1082,18 @@ namespace Wabbajack.Common
public static void ToEcryptedJson<T>(this T data, string key) public static void ToEcryptedJson<T>(this T data, string key)
{ {
var bytes = Encoding.UTF8.GetBytes(data.ToJSON()); var bytes = Encoding.UTF8.GetBytes(data.ToJSON());
bytes.ToEcryptedData(key);
}
public static T FromEncryptedJson<T>(string key)
{
var decoded = FromEncryptedData(key);
return Encoding.UTF8.GetString(decoded).FromJSONString<T>();
}
public static void ToEcryptedData(this byte[] bytes, string key)
{
var encoded = ProtectedData.Protect(bytes, Encoding.UTF8.GetBytes(key), DataProtectionScope.LocalMachine); var encoded = ProtectedData.Protect(bytes, Encoding.UTF8.GetBytes(key), DataProtectionScope.LocalMachine);
if (!Directory.Exists(Consts.LocalAppDataPath)) if (!Directory.Exists(Consts.LocalAppDataPath))
@ -1090,13 +1102,11 @@ namespace Wabbajack.Common
var path = Path.Combine(Consts.LocalAppDataPath, key); var path = Path.Combine(Consts.LocalAppDataPath, key);
File.WriteAllBytes(path, encoded); File.WriteAllBytes(path, encoded);
} }
public static byte[] FromEncryptedData(string key)
public static T FromEncryptedJson<T>(string key)
{ {
var path = Path.Combine(Consts.LocalAppDataPath, key); var path = Path.Combine(Consts.LocalAppDataPath, key);
var bytes = File.ReadAllBytes(path); var bytes = File.ReadAllBytes(path);
var decoded = ProtectedData.Unprotect(bytes, Encoding.UTF8.GetBytes(key), DataProtectionScope.LocalMachine); return ProtectedData.Unprotect(bytes, Encoding.UTF8.GetBytes(key), DataProtectionScope.LocalMachine);
return Encoding.UTF8.GetString(decoded).FromJSONString<T>();
} }
public static bool HaveEncryptedJson(string key) public static bool HaveEncryptedJson(string key)

View File

@ -36,6 +36,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wabbajack.VirtualFileSystem
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wabbajack.Test", "Wabbajack.Test\Wabbajack.Test.csproj", "{81F87B8B-D5CE-4807-9005-38DE95BD7840}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wabbajack.Test", "Wabbajack.Test\Wabbajack.Test.csproj", "{81F87B8B-D5CE-4807-9005-38DE95BD7840}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wabbajack.CLI", "Wabbajack.CLI\Wabbajack.CLI.csproj", "{685D8BB1-D178-4D2C-85C7-C54A36FB7454}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -132,6 +134,14 @@ Global
{81F87B8B-D5CE-4807-9005-38DE95BD7840}.Release|Any CPU.Build.0 = Release|Any CPU {81F87B8B-D5CE-4807-9005-38DE95BD7840}.Release|Any CPU.Build.0 = Release|Any CPU
{81F87B8B-D5CE-4807-9005-38DE95BD7840}.Release|x64.ActiveCfg = Release|x64 {81F87B8B-D5CE-4807-9005-38DE95BD7840}.Release|x64.ActiveCfg = Release|x64
{81F87B8B-D5CE-4807-9005-38DE95BD7840}.Release|x64.Build.0 = Release|x64 {81F87B8B-D5CE-4807-9005-38DE95BD7840}.Release|x64.Build.0 = Release|x64
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Debug|Any CPU.Build.0 = Debug|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Debug|x64.ActiveCfg = Debug|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Debug|x64.Build.0 = Debug|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Release|Any CPU.ActiveCfg = Release|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Release|Any CPU.Build.0 = Release|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Release|x64.ActiveCfg = Release|Any CPU
{685D8BB1-D178-4D2C-85C7-C54A36FB7454}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE