Skip to content

Commit 1080ca7

Browse files
authored
Merge pull request #680 from SteveL-MSFT/export-input
Add args to enable passing input to resource export
2 parents c2a24c4 + a948640 commit 1080ca7

File tree

8 files changed

+87
-5
lines changed

8 files changed

+87
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub enum Schemas {
99
Exist,
1010
ExitCode,
1111
InDesiredState,
12+
Export,
1213
Sleep,
1314
Trace,
1415
WhatIf,
@@ -47,6 +48,11 @@ pub enum SubCommand {
4748
#[clap(name = "input", short, long, help = "The input to the in desired state command as JSON")]
4849
input: String,
4950
},
51+
#[clap(name = "export", about = "Export instances")]
52+
Export {
53+
#[clap(name = "input", short, long, help = "The input to the export command as JSON")]
54+
input: String,
55+
},
5056

5157
#[clap(name = "schema", about = "Get the JSON schema for a subcommand")]
5258
Schema {

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
@@ -6,6 +6,7 @@ mod delete;
66
mod exist;
77
mod exit_code;
88
mod in_desired_state;
9+
mod export;
910
mod sleep;
1011
mod trace;
1112
mod whatif;
@@ -17,6 +18,7 @@ use crate::delete::Delete;
1718
use crate::exist::{Exist, State};
1819
use crate::exit_code::ExitCode;
1920
use crate::in_desired_state::InDesiredState;
21+
use crate::export::Export;
2022
use crate::sleep::Sleep;
2123
use crate::trace::Trace;
2224
use crate::whatif::WhatIf;
@@ -79,6 +81,22 @@ fn main() {
7981
in_desired_state.value_two = 2;
8082
serde_json::to_string(&in_desired_state).unwrap()
8183
},
84+
SubCommand::Export { input } => {
85+
let export = match serde_json::from_str::<Export>(&input) {
86+
Ok(export) => export,
87+
Err(err) => {
88+
eprintln!("Error JSON does not match schema: {err}");
89+
std::process::exit(1);
90+
}
91+
};
92+
for i in 0..export.count {
93+
let instance = Export {
94+
count: i
95+
};
96+
println!("{}", serde_json::to_string(&instance).unwrap());
97+
}
98+
String::new()
99+
},
82100
SubCommand::Schema { subcommand } => {
83101
let schema = match subcommand {
84102
Schemas::Delete => {
@@ -93,6 +111,9 @@ fn main() {
93111
Schemas::InDesiredState => {
94112
schema_for!(InDesiredState)
95113
},
114+
Schemas::Export => {
115+
schema_for!(Export)
116+
},
96117
Schemas::Sleep => {
97118
schema_for!(Sleep)
98119
},
@@ -137,5 +158,7 @@ fn main() {
137158
},
138159
};
139160

140-
println!("{json}");
161+
if !json.is_empty() {
162+
println!("{json}");
163+
}
141164
}

0 commit comments

Comments
 (0)