-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Walker
committed
May 14, 2020
1 parent
35b03dc
commit b7639a3
Showing
7 changed files
with
196 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DroneFlightLog.Data\DroneFlightLog.Data.csproj" /> | ||
<ProjectReference Include="..\DroneFlightLog.Data.Sqlite\DroneFlightLog.Data.Sqlite.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Remove="appsettings.json" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace DroneFlightLog.Manager.Entities | ||
{ | ||
public class Operation | ||
{ | ||
public bool Valid { get; set; } | ||
public OperationType Type { get; set; } | ||
public string UserName { get; set; } | ||
public string Password { get; set; } | ||
public string FileName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace DroneFlightLog.Manager.Entities | ||
{ | ||
public enum OperationType | ||
{ | ||
add = 0, | ||
setpassword = 1, | ||
delete = 2, | ||
export = 3, | ||
import = 4, | ||
update = 5 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using DroneFlightLog.Manager.Entities; | ||
|
||
namespace DroneFlightLog.Manager.Logic | ||
{ | ||
public class CommandParser | ||
{ | ||
// The index into this array is one of the values from the OperationType | ||
// enumeration, mapping the operation to the required argument count | ||
private readonly int[] _requiredArgumentCount = { 3, 3, 2, 2, 2, 1 }; | ||
|
||
/// <summary> | ||
/// Parse the command line, extracting the operation to be performed | ||
/// and its parameters | ||
/// </summary> | ||
/// <param name="args"></param> | ||
/// <returns></returns> | ||
public Operation ParseCommandLine(string[] args) | ||
{ | ||
Operation op = new Operation(); | ||
|
||
if (args.Length > 0) | ||
{ | ||
// Attempt to parse out the operation type from the first argument | ||
if (Enum.TryParse<OperationType>(args[0], out OperationType type)) | ||
{ | ||
// Check there are sufficient arguments for this operation | ||
op.Type = type; | ||
int requiredArgumentCount = _requiredArgumentCount[(int)type]; | ||
|
||
// All is OK at this point if the argument count is correct | ||
op.Valid = (args.Length == requiredArgumentCount); | ||
if (op.Valid) | ||
{ | ||
// Extract the arguments | ||
AssignOperationParameters(op, args); | ||
} | ||
} | ||
} | ||
|
||
return op; | ||
} | ||
|
||
/// <summary> | ||
/// For those operations that require it, assign the operation parameters | ||
/// from the command line, based on the operation type | ||
/// </summary> | ||
/// <param name="op"></param> | ||
/// <param name="args"></param> | ||
private void AssignOperationParameters(Operation op, string[] args) | ||
{ | ||
switch (op.Type) | ||
{ | ||
case OperationType.add: | ||
op.UserName = args[1]; | ||
op.Password = args[2]; | ||
break; | ||
case OperationType.delete: | ||
op.UserName = args[1]; | ||
break; | ||
case OperationType.setpassword: | ||
op.UserName = args[1]; | ||
op.Password = args[2]; | ||
break; | ||
case OperationType.import: | ||
op.FileName = args[1]; | ||
break; | ||
case OperationType.export: | ||
op.FileName = args[1]; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using DroneFlightLog.Data.Factory; | ||
using DroneFlightLog.Data.Sqlite; | ||
using DroneFlightLog.Manager.Entities; | ||
using DroneFlightLog.Manager.Logic; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DroneFlightLog.Manager | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
string version = typeof(Program).Assembly.GetName().Version.ToString(); | ||
Console.WriteLine($"Drone Flight Log Database Management {version}"); | ||
|
||
Operation op = new CommandParser().ParseCommandLine(args); | ||
if (op.Valid) | ||
{ | ||
DroneFlightLogDbContext context = new DroneFlightLogDbContextFactory().CreateDbContext(null); | ||
DroneFlightLogFactory<DroneFlightLogDbContext> factory = new DroneFlightLogFactory<DroneFlightLogDbContext>(context); | ||
|
||
try | ||
{ | ||
switch (op.Type) | ||
{ | ||
case OperationType.add: | ||
factory.Users.AddUser(op.UserName, op.Password); | ||
Console.WriteLine($"Added user {op.UserName}"); | ||
break; | ||
case OperationType.setpassword: | ||
factory.Users.SetPassword(op.UserName, op.Password); | ||
Console.WriteLine($"Set password for user {op.UserName}"); | ||
break; | ||
case OperationType.delete: | ||
factory.Users.DeleteUser(op.UserName); | ||
Console.WriteLine($"Deleted user {op.UserName}"); | ||
break; | ||
case OperationType.update: | ||
context.Database.Migrate(); | ||
Console.WriteLine($"Applied the latest database migrations"); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error : {ex.Message}"); | ||
} | ||
} | ||
else | ||
{ | ||
string executable = AppDomain.CurrentDomain.FriendlyName; | ||
Console.WriteLine("Usage:"); | ||
Console.WriteLine($"[1] {executable} add username password"); | ||
Console.WriteLine($"[2] {executable} setpassword username password"); | ||
Console.WriteLine($"[3] {executable} delete username"); | ||
Console.WriteLine($"[4] {executable} update"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DroneLogDb": "Data Source=droneflightlog.db" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters