From 48d63a8f3a7812f363f5631209f8b244691b5b0d Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 3 Oct 2023 16:57:27 -0700 Subject: [PATCH 1/2] Add `--input` and `--input-file` to `dsc` --- dsc/Cargo.toml | 2 +- dsc/src/args.rs | 4 +++ dsc/src/main.rs | 19 +++++++++++--- dsc/tests/dsc_args.tests.ps1 | 51 ++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/dsc/Cargo.toml b/dsc/Cargo.toml index 5e9498c2..610ff4be 100644 --- a/dsc/Cargo.toml +++ b/dsc/Cargo.toml @@ -12,7 +12,7 @@ lto = true [dependencies] atty = { version = "0.2" } -clap = { version = "4.1", features = ["derive"] } +clap = { version = "4.4", features = ["derive"] } clap_complete = { version = "4.4" } crossterm = { version = "0.27" } ctrlc = { version = "3.4.0" } diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 4aa9d5ae..87e500da 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -20,6 +20,10 @@ pub struct Args { /// The output format to use #[clap(short = 'f', long)] pub format: Option, + #[clap(short = 'i', long, help = "The input to pass to the configuration or resource", conflicts_with = "input_file")] + pub input: Option, + #[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource")] + pub input_file: Option, } #[derive(Debug, PartialEq, Eq, Subcommand)] diff --git a/dsc/src/main.rs b/dsc/src/main.rs index a3301beb..14570e8c 100644 --- a/dsc/src/main.rs +++ b/dsc/src/main.rs @@ -37,9 +37,22 @@ fn main() { let args = Args::parse(); - let stdin: Option = if atty::is(Stream::Stdin) { + let input = if args.input.is_some() { + args.input + } else if args.input_file.is_some() { + info!("Reading input from file {}", args.input_file.as_ref().unwrap()); + let input_file = args.input_file.unwrap(); + match std::fs::read_to_string(input_file) { + Ok(input) => Some(input), + Err(err) => { + error!("Error: Failed to read input file: {err}"); + exit(util::EXIT_INVALID_INPUT); + } + } + } else if atty::is(Stream::Stdin) { None } else { + info!("Reading input from STDIN"); let mut buffer: Vec = Vec::new(); io::stdin().read_to_end(&mut buffer).unwrap(); let input = match String::from_utf8(buffer) { @@ -59,10 +72,10 @@ fn main() { generate(shell, &mut cmd, "dsc", &mut io::stdout()); }, SubCommand::Config { subcommand } => { - subcommand::config(&subcommand, &args.format, &stdin); + subcommand::config(&subcommand, &args.format, &input); }, SubCommand::Resource { subcommand } => { - subcommand::resource(&subcommand, &args.format, &stdin); + subcommand::resource(&subcommand, &args.format, &input); }, SubCommand::Schema { dsc_type } => { let schema = util::get_schema(dsc_type); diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index 2faffd1d..f81cdaad 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -90,6 +90,7 @@ actualState: $out.Trim() | Should -BeExactly $expected } +<<<<<<< HEAD It 'can generate PowerShell completer' { $out = dsc completer powershell | Out-String Invoke-Expression $out @@ -98,4 +99,54 @@ actualState: $completions.CompletionMatches[0].CompletionText | Should -Be 'completer' $completions.CompletionMatches[1].CompletionText | Should -Be 'config' } +======= + It 'input can be passed using ' -TestCases @( + @{ parameter = '-i' } + @{ parameter = '--input' } + ) { + param($parameter) + + $yaml = @' +$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json +resources: +- name: os + type: Microsoft/OSInfo + properties: + family: Windows +'@ + + $out = dsc $parameter "$yaml" config get | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $out.results[0].type | Should -BeExactly 'Microsoft/OSInfo' + } + + It 'input can be passed using ' -TestCases @( + @{ parameter = '-p' } + @{ parameter = '--input-file' } + ) { + param($parameter) + + $yaml = @' +$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json +resources: +- name: os + type: Microsoft/OSInfo + properties: + family: Windows +'@ + + Set-Content -Path $TestDrive/foo.yaml -Value $yaml + $out = dsc $parameter "$TestDrive/foo.yaml" config get | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $out.results[0].type | Should -BeExactly 'Microsoft/OSInfo' + } + + It '--input and --input-file cannot be used together' { + dsc --input 1 --input-file foo.json config get 2> $TestDrive/error.txt + $err = Get-Content $testdrive/error.txt -Raw + $err.Length | Should -Not -Be 0 + $LASTEXITCODE | Should -Be 2 + } + +>>>>>>> 2332df9 (Add `--input` and `--input-file` to `dsc`) } From 179135862b8647bade77d0ee941d0c0f0fb5b464 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 4 Oct 2023 15:40:29 -0700 Subject: [PATCH 2/2] fix leftover merge conflict --- dsc/tests/dsc_args.tests.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index f81cdaad..586f7da3 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -90,7 +90,6 @@ actualState: $out.Trim() | Should -BeExactly $expected } -<<<<<<< HEAD It 'can generate PowerShell completer' { $out = dsc completer powershell | Out-String Invoke-Expression $out @@ -99,7 +98,7 @@ actualState: $completions.CompletionMatches[0].CompletionText | Should -Be 'completer' $completions.CompletionMatches[1].CompletionText | Should -Be 'config' } -======= + It 'input can be passed using ' -TestCases @( @{ parameter = '-i' } @{ parameter = '--input' } @@ -148,5 +147,4 @@ resources: $LASTEXITCODE | Should -Be 2 } ->>>>>>> 2332df9 (Add `--input` and `--input-file` to `dsc`) }