Skip to content
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ Usage: spdx-tool [options] <command> [arguments]
Options:
-h, --help Show this help message and exit
-v, --version Show version information and exit
-l, --log <log-file> Log output to file
-s, --silent Silence console output

Commands:
help <command> Display extended help about a command
add-package Add package to SPDX document (workflow only).
add-relationship <spdx.json> <args> Add relationship between elements.
copy-package <spdx.json> <args> Copy package between SPDX documents (workflow only).
diagram <spdx.json> <mermaid.txt> Generate mermaid diagram.
diagram <spdx.json> <mermaid.txt> [tools] Generate mermaid diagram.
find-package <spdx.json> <criteria> Find package ID in SPDX document
get-version <spdx.json> <criteria> Get the version of an SPDX package.
hash <operation> <algorithm> <file> Generate or verify hashes of files
Expand Down
4 changes: 2 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/AddPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ private AddPackage()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
throw new CommandUsageException("'add-package' command is only valid in a workflow");
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
4 changes: 2 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/AddRelationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private AddRelationship()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is less than 4
if (args.Length < 4)
Expand All @@ -102,7 +102,7 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
6 changes: 4 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ public abstract class Command
/// <summary>
/// Run the command
/// </summary>
/// <param name="context">Program context</param>
/// <param name="args">Command arguments</param>
public abstract void Run(string[] args);
public abstract void Run(Context context, string[] args);

/// <summary>
/// Run the command
/// </summary>
/// <param name="context">Program context</param>
/// <param name="step">Command step</param>
/// <param name="variables">Workflow variables</param>
public abstract void Run(YamlMappingNode step, Dictionary<string, string> variables);
public abstract void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables);

/// <summary>
/// Expand variables in text
Expand Down
4 changes: 2 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/CopyPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private CopyPackage()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is less than 3
if (args.Length < 3)
Expand Down Expand Up @@ -122,7 +122,7 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
4 changes: 2 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/Diagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private Diagram()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is less than 2
if (args.Length < 2)
Expand All @@ -110,7 +110,7 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
6 changes: 3 additions & 3 deletions src/DemaConsulting.SpdxTool/Commands/FindPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private FindPackage()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if insufficient arguments
if (args.Length < 2)
Expand All @@ -97,11 +97,11 @@ public override void Run(string[] args)
var packageId = FindPackageByCriteria(spdxFile, criteria).Id;

// Write the package ID to the console
Console.WriteLine(packageId);
context.WriteLine(packageId);
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
6 changes: 3 additions & 3 deletions src/DemaConsulting.SpdxTool/Commands/GetVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private GetVersion()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if insufficient arguments
if (args.Length < 2)
Expand All @@ -94,11 +94,11 @@ public override void Run(string[] args)
var packageVersion = FindPackage.FindPackageByCriteria(spdxFile, criteria).Version;

// Print the version
Console.WriteLine(packageVersion);
context.WriteLine(packageVersion ?? "");
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
20 changes: 11 additions & 9 deletions src/DemaConsulting.SpdxTool/Commands/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private Hash()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is not 3
if (args.Length != 3)
Expand All @@ -79,11 +79,11 @@ public override void Run(string[] args)
var operation = args[0];
var algorithm = args[1];
var file = args[2];
DoHashOperation(operation, algorithm, file);
DoHashOperation(context, operation, algorithm, file);
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand All @@ -101,17 +101,18 @@ public override void Run(YamlMappingNode step, Dictionary<string, string> variab
throw new YamlException(step.Start, step.End, "'hash' command missing 'file' input");

// Do the hash operation
DoHashOperation(operation, algorithm, file);
DoHashOperation(context, operation, algorithm, file);
}

/// <summary>
/// Do the requested Sha256 operation
/// </summary>
/// <param name="context">Program context</param>
/// <param name="operation">Operation to perform (generate or verify)</param>
/// <param name="algorithm">Hash algorithm</param>
/// <param name="file">File to perform operation on</param>
/// <exception cref="CommandUsageException">On usage error</exception>
public static void DoHashOperation(string operation, string algorithm, string file)
public static void DoHashOperation(Context context, string operation, string algorithm, string file)
{
// Check the algorithm
if (algorithm != "sha256")
Expand All @@ -125,7 +126,7 @@ public static void DoHashOperation(string operation, string algorithm, string fi
break;

case "verify":
VerifySha256(file);
VerifySha256(context, file);
break;

default:
Expand All @@ -149,9 +150,10 @@ public static void GenerateSha256(string file)
/// <summary>
/// Verify a Sha256 hash for a file
/// </summary>
/// <param name="file"></param>
/// <param name="context">Program context</param>
/// <param name="file">Name of the file to verify</param>
/// <exception cref="CommandErrorException"></exception>
public static void VerifySha256(string file)
public static void VerifySha256(Context context, string file)
{
// Check the hash file exists
var hashFile = file + ".sha256";
Expand All @@ -169,7 +171,7 @@ public static void VerifySha256(string file)
throw new CommandErrorException($"Sha256 hash mismatch for '{file}'");

// Report the digest is OK
Console.WriteLine($"Sha256 Digest OK for '{file}'");
context.WriteLine($"Sha256 Digest OK for '{file}'");
}

/// <summary>
Expand Down
15 changes: 8 additions & 7 deletions src/DemaConsulting.SpdxTool/Commands/Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ private Help()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is not 1
if (args.Length != 1)
throw new CommandUsageException("'help' command missing arguments");

// Generate the markdown
ShowUsage(args[0]);
ShowUsage(context, args[0]);
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand All @@ -87,22 +87,23 @@ public override void Run(YamlMappingNode step, Dictionary<string, string> variab
throw new YamlException(step.Start, step.End, "'help' command missing 'about' input");

// Generate the markdown
ShowUsage(about);
ShowUsage(context, about);
}

/// <summary>
/// Show the usage for the requested command
/// </summary>
/// <param name="command"></param>
/// <param name="context">Program context</param>
/// <param name="command">Command to get help on</param>
/// <exception cref="CommandUsageException">On error</exception>
public static void ShowUsage(string command)
public static void ShowUsage(Context context, string command)
{
// Get the entry for the command
if (!CommandsRegistry.Commands.TryGetValue(command, out var entry))
throw new CommandUsageException($"Unknown command: '{command}'");

// Display the command entry
foreach (var line in entry.Details)
Console.WriteLine(line);
context.WriteLine(line);
}
}
8 changes: 4 additions & 4 deletions src/DemaConsulting.SpdxTool/Commands/Print.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ private Print()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
foreach (var arg in args)
Console.WriteLine(arg);
context.WriteLine(arg);
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand All @@ -88,7 +88,7 @@ public override void Run(YamlMappingNode step, Dictionary<string, string> variab
for (var i = 0; i < text.Children.Count; i++)
{
var line = GetSequenceString(text, i, variables) ?? string.Empty;
Console.WriteLine(line);
context.WriteLine(line);
}
}
}
8 changes: 4 additions & 4 deletions src/DemaConsulting.SpdxTool/Commands/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private Query()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is not 1
if (args.Length < 2)
Expand All @@ -83,12 +83,12 @@ public override void Run(string[] args)
// Generate the markdown
var found = QueryProgramOutput(args[0], args[1], args.Skip(2).ToArray());

// Write the found value to the console
Console.WriteLine(found);
// Write the found value
context.WriteLine(found);
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
4 changes: 2 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/RenameId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private RenameId()
}

/// <inheritdoc />
public override void Run(string[] args)
public override void Run(Context context, string[] args)
{
// Report an error if the number of arguments is not 3
if (args.Length != 3)
Expand All @@ -81,7 +81,7 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
public override void Run(Context context, YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the step inputs
var inputs = GetMapMap(step, "inputs");
Expand Down
Loading