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

Split Program.cs to separate focused files #16

Closed
wants to merge 1 commit into from
Closed
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
93 changes: 93 additions & 0 deletions Seatbelt/ArgumentsParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Seatbelt.IO;
using Seatbelt.ProbePresets;


namespace Seatbelt
{
public static class ArgumentsParser
{

public static List<Action> Parse(string[] args, IOutputSink output, AvailableProbes probes)
{

// Get the arguments and lowercase them all so we can ignore the case going forward
var arguments = args.Select(a => a.ToLowerInvariant()).Distinct().ToList();

// the actions the user wants to execute
var actionsRequested = new List<Action>();

// look for the full, user, system presets and full - if present
ParseForPresets(output, probes, arguments, actionsRequested);

// look through the remaining arguments and add to the actions required
foreach (var argument in arguments)
actionsRequested.Add(() => output.Write(probes.RunProbe(argument)));

return actionsRequested;

}

private static void ParseForPresets(IOutputSink output, AvailableProbes probes, List<string> arguments, List<Action> actionsRequested)
{
if (arguments.Contains("full"))
{
FilterResults.Filter = false;
actionsRequested.Add(() => FullPreset.Run(output, probes));
arguments.Remove("full");
}

if (arguments.Contains("all"))
{
actionsRequested.Add(() => AllPreset.Run(output, probes));
arguments.Remove("all");
}


if (arguments.Contains("system"))
{
actionsRequested.Add(() => SystemPreset.Run(output, probes));
arguments.Remove("system");
}


if (arguments.Contains("user"))
{
actionsRequested.Add(() => UserPreset.Run(output, probes));
arguments.Remove("user");
}
}


/// <summary>
/// Look for the output choice (file or none (Console))
/// Expected command line format is: ToFile "file name.txt"
/// i.e. the filename is the next argument after the 'ToFile' keyword
/// </summary>
public static IOutputSink GetOutputTarget(string[] args)
{

for (int i = 0; i < args.Length; i++)
{
if (args[i].ToLowerInvariant() == "tofile")
{
// grab the file name
var filename = args[i + 1];

// empty out the parts of the array in case the filename conflicts somehow with the name of a probe
args[i] = string.Empty;
args[i + 1] = string.Empty;

return new FileOutput(filename);
}
}

// if we get here then the file output wasn't selected to default to console
return new ConsoleOutput();
}
}


}
75 changes: 75 additions & 0 deletions Seatbelt/AvailableProbes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Seatbelt.Probes;

namespace Seatbelt
{
/// <summary>
/// Uses reflection to get all the classes implementing IProbe
/// To add a new Probe just create a class the implements IProbe and has a static property "ProbeName"
/// </summary>
public class AvailableProbes
{
private readonly Dictionary<string, Func<string>> _availableProbes = new Dictionary<string, Func<string>>(55);
private readonly List<string> _probesDone = new List<string>(55);

public AvailableProbes()
{
// Get all the IProbe Classes (except for the IProbe interface type its self!)
var iprobeTypes = System.Reflection.Assembly
.GetEntryAssembly()
.GetTypes()
.Where(type => typeof(IProbe).IsAssignableFrom(type) && type.Name != typeof(IProbe).Name);


foreach (var probe in iprobeTypes)
{
// the name comes from the static ProbeName property
var probeName = probe.GetProperty("ProbeName").GetValue(probe, null).ToString().ToLowerInvariant();

// the action, when invoked, will create an instance of the class and then call the List() method
var probeAction = new Func<string>(() => (Activator.CreateInstance(probe) as IProbe).List());

_availableProbes.Add(probeName, probeAction);
}
}


/// <summary>
/// Runs a probe and returns the results
/// NOTE: Will not run a probe more than once!
/// (this is to prevent multiple calls to a probe when that probe is part of a preset and called with the all flag)
/// </summary>
public string RunProbe(string name)
{
// Ignore the case
var nameLowerCase = name.ToLowerInvariant();

// Don't re-run a probe if it's already been run
if (string.IsNullOrEmpty(name) || _probesDone.Contains(nameLowerCase))
return string.Empty;

if (_availableProbes.TryGetValue(nameLowerCase, out var probe))
{
_probesDone.Add(nameLowerCase);
return probe.Invoke();
}
else
return $"[X] Check \"{name}\" not found!";
}

/// <summary>
/// Return all the available probe names
/// </summary>
public IEnumerable<string> GetAll()
=> _availableProbes.Keys;

/// <summary>
/// Intended for debugging - return the list of probes done
/// </summary>
internal IEnumerable<string> GetProbesDone()
=> _probesDone;

}
}
9 changes: 9 additions & 0 deletions Seatbelt/FilterResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Seatbelt
{

// used to signal whether filtering should be done on results
public static class FilterResults
{
public static bool Filter = true;
}
}
Loading