Skip to content

Commit a05736a

Browse files
committed
Add args to enable passing input to resource export
1 parent 416c770 commit a05736a

File tree

8 files changed

+88
-5
lines changed

8 files changed

+88
-5
lines changed

dsc/src/args.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ pub enum ResourceSubCommand {
213213
Export {
214214
#[clap(short, long, help = t!("args.resource").to_string())]
215215
resource: String,
216+
#[clap(short, long, help = t!("args.input").to_string(), conflicts_with = "file")]
217+
input: Option<String>,
218+
#[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")]
219+
file: Option<String>,
216220
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
217221
output_format: Option<OutputFormat>,
218222
},

dsc/src/resource_command.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputForma
259259
}
260260
}
261261

262-
pub fn export(dsc: &mut DscManager, resource_type: &str, format: Option<&OutputFormat>) {
263-
let mut input = String::new();
262+
pub fn export(dsc: &mut DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
264263
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
265264
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
266265
exit(EXIT_DSC_RESOURCE_NOT_FOUND);

dsc/src/subcommand.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,10 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
545545
dsc.find_resources(&[resource.to_string()], progress_format);
546546
resource_command::schema(&dsc, resource, output_format.as_ref());
547547
},
548-
ResourceSubCommand::Export { resource, output_format } => {
548+
ResourceSubCommand::Export { resource, input, file, output_format } => {
549549
dsc.find_resources(&[resource.to_string()], progress_format);
550-
resource_command::export(&mut dsc, resource, output_format.as_ref());
550+
let parsed_input = get_input(input.as_ref(), file.as_ref());
551+
resource_command::export(&mut dsc, resource, parsed_input, output_format.as_ref());
551552
},
552553
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
553554
dsc.find_resources(&[resource.to_string()], progress_format);

dsc/tests/dsc_export.tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ Describe 'resource export tests' {
8888
$config_with_process_list.resources.count | Should -BeGreaterThan 1
8989
}
9090

91+
It 'Export can be called on resource with input' {
92+
$out = '{"count":3}' | dsc resource export -r Test/Export -f - | ConvertFrom-Json
93+
$LASTEXITCODE | Should -Be 0
94+
$out.resources.count | Should -Be 3
95+
$out.resources[0].type | Should -BeExactly 'Test/Export'
96+
$out.resources[0].properties.count | Should -Be 0
97+
$out.resources[1].type | Should -BeExactly 'Test/Export'
98+
$out.resources[1].properties.count | Should -Be 1
99+
$out.resources[2].type | Should -BeExactly 'Test/Export'
100+
$out.resources[2].properties.count | Should -Be 2
101+
}
102+
91103
It 'Export can be called on a configuration with the use of --output-format as a subcommand' {
92104

93105
$yaml = @'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3+
"type": "Test/Export",
4+
"version": "0.1.0",
5+
"export": {
6+
"executable": "dsctest",
7+
"args": [
8+
"export",
9+
{
10+
"jsonInputArg": "--input",
11+
"mandatory": true
12+
}
13+
]
14+
},
15+
"schema": {
16+
"command": {
17+
"executable": "dsctest",
18+
"args": [
19+
"schema",
20+
"-s",
21+
"export"
22+
]
23+
}
24+
}
25+
}

tools/dsctest/src/args.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub enum Schemas {
88
Delete,
99
Exist,
1010
ExitCode,
11+
Export,
1112
Sleep,
1213
Trace,
1314
WhatIf,
@@ -41,6 +42,12 @@ pub enum SubCommand {
4142
input: String,
4243
},
4344

45+
#[clap(name = "export", about = "Export instances")]
46+
Export {
47+
#[clap(name = "input", short, long, help = "The input to the export command as JSON")]
48+
input: String,
49+
},
50+
4451
#[clap(name = "schema", about = "Get the JSON schema for a subcommand")]
4552
Schema {
4653
#[clap(name = "subcommand", short, long, help = "The subcommand to get the schema for")]

tools/dsctest/src/export.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
use schemars::JsonSchema;
5+
use serde::{Deserialize, Serialize};
6+
7+
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
8+
#[serde(deny_unknown_fields)]
9+
pub struct Export {
10+
/// Number of instances to return
11+
pub count: u64,
12+
}

tools/dsctest/src/main.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod args;
55
mod delete;
66
mod exist;
77
mod exit_code;
8+
mod export;
89
mod sleep;
910
mod trace;
1011
mod whatif;
@@ -15,6 +16,7 @@ use schemars::schema_for;
1516
use crate::delete::Delete;
1617
use crate::exist::{Exist, State};
1718
use crate::exit_code::ExitCode;
19+
use crate::export::Export;
1820
use crate::sleep::Sleep;
1921
use crate::trace::Trace;
2022
use crate::whatif::WhatIf;
@@ -65,6 +67,22 @@ fn main() {
6567
}
6668
input
6769
},
70+
SubCommand::Export { input } => {
71+
let export = match serde_json::from_str::<Export>(&input) {
72+
Ok(export) => export,
73+
Err(err) => {
74+
eprintln!("Error JSON does not match schema: {err}");
75+
std::process::exit(1);
76+
}
77+
};
78+
for i in 0..export.count {
79+
let instance = Export {
80+
count: i
81+
};
82+
println!("{}", serde_json::to_string(&instance).unwrap());
83+
}
84+
String::new()
85+
},
6886
SubCommand::Schema { subcommand } => {
6987
let schema = match subcommand {
7088
Schemas::Delete => {
@@ -76,6 +94,9 @@ fn main() {
7694
Schemas::ExitCode => {
7795
schema_for!(ExitCode)
7896
},
97+
Schemas::Export => {
98+
schema_for!(Export)
99+
},
79100
Schemas::Sleep => {
80101
schema_for!(Sleep)
81102
},
@@ -120,5 +141,7 @@ fn main() {
120141
},
121142
};
122143

123-
println!("{json}");
144+
if !json.is_empty() {
145+
println!("{json}");
146+
}
124147
}

0 commit comments

Comments
 (0)