Skip to content

Commit

Permalink
Merge pull request PowerShell#217 from SteveL-MSFT/dsc-input
Browse files Browse the repository at this point in the history
Add `--input` and `--input-file` to `dsc`
  • Loading branch information
SteveL-MSFT authored Oct 4, 2023
2 parents 5b92b58 + 1791358 commit 67ade07
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dsc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub struct Args {
/// The output format to use
#[clap(short = 'f', long)]
pub format: Option<OutputFormat>,
#[clap(short = 'i', long, help = "The input to pass to the configuration or resource", conflicts_with = "input_file")]
pub input: Option<String>,
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource")]
pub input_file: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Subcommand)]
Expand Down
19 changes: 16 additions & 3 deletions dsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,22 @@ fn main() {

let args = Args::parse();

let stdin: Option<String> = 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<u8> = Vec::new();
io::stdin().read_to_end(&mut buffer).unwrap();
let input = match String::from_utf8(buffer) {
Expand All @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,53 @@ actualState:
$completions.CompletionMatches[0].CompletionText | Should -Be 'completer'
$completions.CompletionMatches[1].CompletionText | Should -Be 'config'
}

It 'input can be passed using <parameter>' -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 <parameter>' -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
}

}

0 comments on commit 67ade07

Please sign in to comment.