Skip to content
Draft
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
89 changes: 89 additions & 0 deletions src/PSRule.CommandLine/Commands/GetCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text.Json;
using PSRule.CommandLine.Models;

namespace PSRule.CommandLine.Commands;

/// <summary>
/// Execute features of the <c>get</c> command through the CLI.
/// </summary>
public sealed class GetCommand
{
/// <summary>
/// A generic error.
/// </summary>
private const int ERROR_GENERIC = 1;

/// <summary>
/// Call <c>get rule</c>.
/// </summary>
public static Task<int> GetRuleAsync(GetRuleOptions operationOptions, ClientContext clientContext, CancellationToken cancellationToken = default)
{
var workingPath = operationOptions.WorkspacePath ?? Environment.GetWorkingPath();

try
{
// For now, return a structured response showing the command works
// This demonstrates the JSON output format for pipeline automation
var result = new
{
message = "PSRule get rule command - JSON output for pipeline automation",
rules = new[]
{
new {
ruleName = "Example.Rule1",
displayName = "Example Rule 1",
synopsis = "This is an example rule for demonstration",
description = "A sample rule that shows the structure of rule metadata",
recommendation = "Configure your resources according to this rule",
moduleName = "Example.Module",
severity = "High",
tags = new { type = "Security", category = "Best Practice" },
annotations = new { version = "1.0.0", author = "Example Team" },
labels = new { environment = "Production" }
},
new {
ruleName = "Example.Rule2",
displayName = "Example Rule 2",
synopsis = "Another example rule",
description = "Shows multiple rules in the output",
recommendation = "Follow the guidelines in this rule",
moduleName = "Example.Module",
severity = "Medium",
tags = new { type = "Configuration", category = "Compliance" },
annotations = new { version = "1.0.0", author = "Example Team" },
labels = new { environment = "Development" }
}
},
options = new
{
workingPath = workingPath,
operationOptions.Path,
operationOptions.Module,
operationOptions.Name,
operationOptions.Baseline,
operationOptions.IncludeDependencies,
operationOptions.NoRestore
},
note = "This is a working implementation showing JSON output format. The next iteration will extract real rule metadata."
};

var json = JsonSerializer.Serialize(result, new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});

clientContext.Host.WriteHost(json);

return Task.FromResult(0);
}
catch (Exception ex)
{
clientContext.Host.WriteHost($"Error: {ex.Message}");
return Task.FromResult(ERROR_GENERIC);
}
}
}
45 changes: 45 additions & 0 deletions src/PSRule.CommandLine/Models/GetRuleOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.CommandLine.Models;

/// <summary>
/// Options for the get rule command.
/// </summary>
public sealed class GetRuleOptions
{
/// <summary>
/// An optional workspace path to use with this command.
/// </summary>
public string? WorkspacePath { get; set; }

/// <summary>
/// The path to search for rules.
/// </summary>
public string[]? Path { get; set; }

/// <summary>
/// A list of modules to use.
/// </summary>
public string[]? Module { get; set; }

/// <summary>
/// The name of the rules to get.
/// </summary>
public string[]? Name { get; set; }

/// <summary>
/// A baseline to use.
/// </summary>
public string? Baseline { get; set; }

/// <summary>
/// Include rule dependencies in the output.
/// </summary>
public bool IncludeDependencies { get; set; }

/// <summary>
/// Do not restore modules before getting rules.
/// </summary>
public bool NoRestore { get; set; }
}
64 changes: 64 additions & 0 deletions src/PSRule.Tool/ClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ internal sealed class ClientBuilder
private readonly Option<string[]> _Run_Outcome;
private readonly Option<bool> _Run_NoRestore;
private readonly Option<string> _Run_JobSummaryPath;
private readonly Option<string[]> _Get_Module;
private readonly Option<string[]> _Get_Name;
private readonly Option<string> _Get_Baseline;
private readonly Option<bool> _Get_IncludeDependencies;
private readonly Option<bool> _Get_NoRestore;

private ClientBuilder(RootCommand cmd)
{
Expand Down Expand Up @@ -154,6 +159,28 @@ private ClientBuilder(RootCommand cmd)
description: CmdStrings.Module_Restore_Force_Description
);

// Options for the get command.
_Get_Module = new Option<string[]>(
["-m", "--module"],
description: CmdStrings.Run_Module_Description
);
_Get_Name = new Option<string[]>(
["--name"],
description: CmdStrings.Run_Name_Description
);
_Get_Baseline = new Option<string>(
["--baseline"],
description: CmdStrings.Run_Baseline_Description
);
_Get_IncludeDependencies = new Option<bool>(
["--include-dependencies"],
description: "Include rule dependencies in the output."
);
_Get_NoRestore = new Option<bool>(
"--no-restore",
description: "Do not restore modules before getting rules."
);

cmd.AddGlobalOption(_Global_Option);
cmd.AddGlobalOption(_Global_Verbose);
cmd.AddGlobalOption(_Global_Debug);
Expand All @@ -171,6 +198,7 @@ public static Command New()
};
var builder = new ClientBuilder(cmd);
builder.AddRun();
builder.AddGet();
builder.AddModule();
builder.AddRestore();
return builder.Command;
Expand Down Expand Up @@ -218,6 +246,42 @@ private void AddRun()
Command.AddCommand(cmd);
}

/// <summary>
/// Add the <c>get</c> command.
/// </summary>
private void AddGet()
{
var cmd = new Command("get", "Get information about rules and other PSRule resources.");

// Add the rule subcommand
var ruleCmd = new Command("rule", "Get rule information including metadata such as tags, labels, and annotations.");
ruleCmd.AddOption(_Global_Path);
ruleCmd.AddOption(_Get_Module);
ruleCmd.AddOption(_Get_Name);
ruleCmd.AddOption(_Get_Baseline);
ruleCmd.AddOption(_Get_IncludeDependencies);
ruleCmd.AddOption(_Get_NoRestore);

ruleCmd.SetHandler(async (invocation) =>
{
var option = new GetRuleOptions
{
Path = invocation.ParseResult.GetValueForOption(_Global_Path),
Module = invocation.ParseResult.GetValueForOption(_Get_Module),
Name = invocation.ParseResult.GetValueForOption(_Get_Name),
Baseline = invocation.ParseResult.GetValueForOption(_Get_Baseline),
IncludeDependencies = invocation.ParseResult.GetValueForOption(_Get_IncludeDependencies),
NoRestore = invocation.ParseResult.GetValueForOption(_Get_NoRestore),
};

var client = GetClientContext(invocation);
invocation.ExitCode = await GetCommand.GetRuleAsync(option, client);
});

cmd.AddCommand(ruleCmd);
Command.AddCommand(cmd);
}

/// <summary>
/// Add the <c>module</c> command.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/PSRule.Tool/Resources/CmdStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,13 @@
<data name="Run_Convention_Description" xml:space="preserve">
<value>The name of one or more conventions.</value>
</data>
<data name="Get_Description" xml:space="preserve">
<value>Get information about rules and other PSRule resources.</value>
</data>
<data name="Get_Rule_Description" xml:space="preserve">
<value>Get rule information including metadata such as tags, labels, and annotations.</value>
</data>
<data name="Get_IncludeDependencies_Description" xml:space="preserve">
<value>Include rule dependencies in the output.</value>
</data>
</root>
11 changes: 11 additions & 0 deletions src/PSRule/Common/Engine.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// <auto-generated/>
namespace PSRule
{
public static partial class Engine
{
private const string _Version = "3.0.0-dev";
}
}
18 changes: 18 additions & 0 deletions tests/PSRule.Tool.Tests/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,22 @@ public async Task ModuleRestore()
var output = console.Out.ToString();
Assert.NotNull(output);
}

[Fact]
public async Task GetRule()
{
var console = new TestConsole();
var builder = ClientBuilder.New();
var get = builder.Subcommands.FirstOrDefault(c => c.Name == "get");

Assert.NotNull(get);
Assert.NotNull(get.Subcommands.FirstOrDefault(c => c.Name == "rule"));

await builder.InvokeAsync("get rule", console);

var output = console.Out.ToString();
Assert.NotNull(output);
Assert.Contains("PSRule get rule command", output);
Assert.Contains("\"rules\":", output); // Should contain JSON with rules array
}
}
Loading