Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/piped-input #31

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/BusinessLogic/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

namespace Passenger
{
public class Worker(string[] args)
public class Worker(string[] args, string piped)
{
private readonly Authorization authorization = new(EnDeCoder.JSWSecret);
private readonly string[] arguments = args.Skip(1).ToArray();
private readonly string piped = piped;

private void RoutineAuthControl(string verbName, int requiredArgs)
{
if (arguments.Length != requiredArgs) Error.ArgumentCount(verbName, requiredArgs);
if (!authorization.ValidateToken(arguments[0])) Error.InvalidToken();
}

private void RequirePipedInput()
{
if (piped == null) Error.PipedInputRequired();
}

/*
* Authorization
*/
Expand Down Expand Up @@ -91,11 +97,9 @@ public void Update()
RoutineAuthControl("update", 3);
ReadWritableDatabaseEntry entry = Validate.JsonAsDatabaseEntry(arguments[2]);
Validate.Entry(entry);
Console.WriteLine(
JsonSerializer.Serialize(
Database.Update(arguments[1], entry)
)
);
Console.WriteLine(JsonSerializer.Serialize(
Database.Update(arguments[1], entry)
));
}

public void Delete()
Expand Down
4 changes: 2 additions & 2 deletions src/Presentation/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Passenger
{
public class CommandLine(string[] args)
public class CommandLine(string[] args, string piped)
{
private readonly string command = args.Length > 0
? args[0]
Expand All @@ -9,7 +9,7 @@ public class CommandLine(string[] args)
public void Parse()
{
if (command is null) Error.MissingCommand();
Worker worker = new(args);
Worker worker = new(args, piped);
switch (command)
{
case "login" or "-l": worker.Login(); break;
Expand Down
10 changes: 9 additions & 1 deletion src/Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ public static class Program
{
public static void Main(string[] args)
{
CommandLine commandLine = new(args);
string piped = null;
// Get piped input if exists
if (Console.IsInputRedirected)
{
using Stream inputStream = Console.OpenStandardInput();
using StreamReader streamReader = new(inputStream);
piped = streamReader.ReadToEnd().Trim();
}
CommandLine commandLine = new(args, piped);
commandLine.Parse();
}
}
Expand Down