Skip to content

Commit cf45e05

Browse files
author
Andrew
committed
Added progress-format arg
1 parent e5ff855 commit cf45e05

File tree

12 files changed

+217
-62
lines changed

12 files changed

+217
-62
lines changed

dsc/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ _version = 1
44
about = "Apply configuration or invoke specific DSC resources"
55
traceFormat = "Trace format to use"
66
traceLevel = "Trace level to use"
7+
progressFormat = "Progress format to use"
78
completer = "Generate a shell completion script"
89
configAbout = "Apply a configuration document"
910
parameters = "Parameters to pass to the configuration as JSON or YAML"

dsc/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use clap::{Parser, Subcommand, ValueEnum};
55
use clap_complete::Shell;
66
use dsc_lib::dscresources::command_resource::TraceLevel;
7+
use dsc_lib::util::ProgressFormat;
78
use rust_i18n::t;
89
use serde::Deserialize;
910

@@ -33,6 +34,8 @@ pub struct Args {
3334
pub trace_level: Option<TraceLevel>,
3435
#[clap(short = 't', long, help = t!("args.traceFormat").to_string(), value_enum)]
3536
pub trace_format: Option<TraceFormat>,
37+
#[clap(short = 'p', long, help = t!("args.progressFormat").to_string(), value_enum)]
38+
pub progress_format: Option<ProgressFormat>,
3639
}
3740

3841
#[derive(Debug, PartialEq, Eq, Subcommand)]

dsc/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rust_i18n::{i18n, t};
88
use std::{io, process::exit};
99
use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind};
1010
use tracing::{error, info, warn, debug};
11+
use dsc_lib::util::ProgressFormat;
1112

1213
#[cfg(debug_assertions)]
1314
use crossterm::event;
@@ -40,6 +41,8 @@ fn main() {
4041

4142
debug!("{}: {}", t!("main.usingDscVersion"), env!("CARGO_PKG_VERSION"));
4243

44+
let progress_format = args.progress_format.unwrap_or_else(|| { ProgressFormat::Default });
45+
4346
match args.subcommand {
4447
SubCommand::Completer { shell } => {
4548
info!("{} {:?}", t!("main.generatingCompleter"), shell);
@@ -50,19 +53,19 @@ fn main() {
5053
if let Some(file_name) = parameters_file {
5154
info!("{}: {file_name}", t!("main.readingParametersFile"));
5255
match std::fs::read_to_string(&file_name) {
53-
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), system_root.as_ref(), &as_group, &as_include),
56+
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), system_root.as_ref(), &as_group, &as_include, progress_format),
5457
Err(err) => {
5558
error!("{} '{file_name}': {err}", t!("main.failedToReadParametersFile"));
5659
exit(util::EXIT_INVALID_INPUT);
5760
}
5861
}
5962
}
6063
else {
61-
subcommand::config(&subcommand, &parameters, system_root.as_ref(), &as_group, &as_include);
64+
subcommand::config(&subcommand, &parameters, system_root.as_ref(), &as_group, &as_include, progress_format);
6265
}
6366
},
6467
SubCommand::Resource { subcommand } => {
65-
subcommand::resource(&subcommand);
68+
subcommand::resource(&subcommand, progress_format);
6669
},
6770
SubCommand::Schema { dsc_type , output_format } => {
6871
let schema = util::get_schema(dsc_type);

dsc/src/subcommand.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use dsc_lib::{
2525
},
2626
dscresources::dscresource::{Capability, ImplementedAs, Invoke},
2727
dscresources::resource_manifest::{import_manifest, ResourceManifest},
28+
util::ProgressFormat,
2829
};
2930
use rust_i18n::t;
3031
use std::{
@@ -251,7 +252,7 @@ fn initialize_config_root(path: Option<&String>) -> Option<String> {
251252
}
252253

253254
#[allow(clippy::too_many_lines)]
254-
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounted_path: Option<&String>, as_group: &bool, as_include: &bool) {
255+
pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounted_path: Option<&String>, as_group: &bool, as_include: &bool, progress_format: ProgressFormat) {
255256
let (new_parameters, json_string) = match subcommand {
256257
ConfigSubCommand::Get { input, file, .. } |
257258
ConfigSubCommand::Set { input, file, .. } |
@@ -295,6 +296,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
295296
}
296297
};
297298

299+
configurator.set_progress_format(progress_format);
300+
298301
if let ConfigSubCommand::Set { what_if , .. } = subcommand {
299302
if *what_if {
300303
configurator.context.execution_type = ExecutionKind::WhatIf;
@@ -382,7 +385,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
382385
}
383386
}
384387
} else {
385-
match validate_config(configurator.get_config()) {
388+
match validate_config(configurator.get_config(), progress_format) {
386389
Ok(()) => {
387390
// valid, so do nothing
388391
},
@@ -450,7 +453,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
450453
/// # Errors
451454
///
452455
/// * `DscError` - The error that occurred.
453-
pub fn validate_config(config: &Configuration) -> Result<(), DscError> {
456+
pub fn validate_config(config: &Configuration, progress_format: ProgressFormat) -> Result<(), DscError> {
454457
// first validate against the config schema
455458
debug!("{}", t!("subcommand.validatingConfiguration"));
456459
let schema = serde_json::to_value(get_schema(DscType::Configuration))?;
@@ -476,7 +479,7 @@ pub fn validate_config(config: &Configuration) -> Result<(), DscError> {
476479

477480
resource_types.push(type_name.to_lowercase().to_string());
478481
}
479-
dsc.find_resources(&resource_types);
482+
dsc.find_resources(&resource_types, progress_format);
480483

481484
for resource_block in resources {
482485
let Some(type_name) = resource_block["type"].as_str() else {
@@ -527,7 +530,7 @@ pub fn validate_config(config: &Configuration) -> Result<(), DscError> {
527530
}
528531

529532
#[allow(clippy::too_many_lines)]
530-
pub fn resource(subcommand: &ResourceSubCommand) {
533+
pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat) {
531534
let mut dsc = match DscManager::new() {
532535
Ok(dsc) => dsc,
533536
Err(err) => {
@@ -538,43 +541,43 @@ pub fn resource(subcommand: &ResourceSubCommand) {
538541

539542
match subcommand {
540543
ResourceSubCommand::List { resource_name, adapter_name, description, tags, output_format } => {
541-
list_resources(&mut dsc, resource_name.as_ref(), adapter_name.as_ref(), description.as_ref(), tags.as_ref(), output_format.as_ref());
544+
list_resources(&mut dsc, resource_name.as_ref(), adapter_name.as_ref(), description.as_ref(), tags.as_ref(), output_format.as_ref(), progress_format);
542545
},
543546
ResourceSubCommand::Schema { resource , output_format } => {
544-
dsc.find_resources(&[resource.to_string()]);
547+
dsc.find_resources(&[resource.to_string()], progress_format);
545548
resource_command::schema(&dsc, resource, output_format.as_ref());
546549
},
547550
ResourceSubCommand::Export { resource, output_format } => {
548-
dsc.find_resources(&[resource.to_string()]);
551+
dsc.find_resources(&[resource.to_string()], progress_format);
549552
resource_command::export(&mut dsc, resource, output_format.as_ref());
550553
},
551554
ResourceSubCommand::Get { resource, input, file: path, all, output_format } => {
552-
dsc.find_resources(&[resource.to_string()]);
555+
dsc.find_resources(&[resource.to_string()], progress_format);
553556
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
554557
else {
555558
let parsed_input = get_input(input.as_ref(), path.as_ref());
556559
resource_command::get(&dsc, resource, parsed_input, output_format.as_ref());
557560
}
558561
},
559562
ResourceSubCommand::Set { resource, input, file: path, output_format } => {
560-
dsc.find_resources(&[resource.to_string()]);
563+
dsc.find_resources(&[resource.to_string()], progress_format);
561564
let parsed_input = get_input(input.as_ref(), path.as_ref());
562565
resource_command::set(&dsc, resource, parsed_input, output_format.as_ref());
563566
},
564567
ResourceSubCommand::Test { resource, input, file: path, output_format } => {
565-
dsc.find_resources(&[resource.to_string()]);
568+
dsc.find_resources(&[resource.to_string()], progress_format);
566569
let parsed_input = get_input(input.as_ref(), path.as_ref());
567570
resource_command::test(&dsc, resource, parsed_input, output_format.as_ref());
568571
},
569572
ResourceSubCommand::Delete { resource, input, file: path } => {
570-
dsc.find_resources(&[resource.to_string()]);
573+
dsc.find_resources(&[resource.to_string()], progress_format);
571574
let parsed_input = get_input(input.as_ref(), path.as_ref());
572575
resource_command::delete(&dsc, resource, parsed_input);
573576
},
574577
}
575578
}
576579

577-
fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: Option<&OutputFormat>) {
580+
fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_name: Option<&String>, description: Option<&String>, tags: Option<&Vec<String>>, format: Option<&OutputFormat>, progress_format: ProgressFormat) {
578581
let mut write_table = false;
579582
let mut table = Table::new(&[
580583
t!("subcommand.tableHeader_type").to_string().as_ref(),
@@ -588,7 +591,7 @@ fn list_resources(dsc: &mut DscManager, resource_name: Option<&String>, adapter_
588591
// write as table if format is not specified and interactive
589592
write_table = true;
590593
}
591-
for resource in dsc.list_available_resources(resource_name.unwrap_or(&String::from("*")), adapter_name.unwrap_or(&String::new())) {
594+
for resource in dsc.list_available_resources(resource_name.unwrap_or(&String::from("*")), adapter_name.unwrap_or(&String::new()), progress_format) {
592595
let mut capabilities = "--------".to_string();
593596
let capability_types = [
594597
(Capability::Get, "g"),

dsc/tests/dsc_config_get.tests.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ Describe 'dsc config get tests' {
5656
$LASTEXITCODE | Should -Be 0
5757
}
5858

59+
It 'json progress for config subcommand' {
60+
$config_yaml = @"
61+
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
62+
resources:
63+
- name: Echo
64+
type: Microsoft.DSC.Debug/Echo
65+
properties:
66+
output: hello
67+
"@
68+
$config_yaml | dsc --progress-format json config get -f - 2> $TestDrive/ErrorStream.txt
69+
$LASTEXITCODE | Should -Be 0
70+
$lines = Get-Content $TestDrive/ErrorStream.txt
71+
$ProgressMessagesFound = $False
72+
foreach ($line in $lines) {
73+
$jp = $line | ConvertFrom-Json
74+
if ($jp.activity) { # if line is a progress message
75+
$jp.percent_complete | Should -BeIn (0..100)
76+
$ProgressMessagesFound = $True
77+
}
78+
}
79+
$ProgressMessagesFound | Should -BeTrue
80+
}
81+
5982
It 'contentVersion is ignored' {
6083
$config_yaml = @"
6184
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json

dsc/tests/dsc_resource_list.tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ Describe 'Tests for listing resources' {
5858
}
5959
}
6060

61+
It 'json progress for resource subcommand' {
62+
dsc --progress-format json resource list -a '*' 2> $TestDrive/ErrorStream.txt
63+
$LASTEXITCODE | Should -Be 0
64+
$lines = Get-Content $TestDrive/ErrorStream.txt
65+
$ProgressMessagesFound = $False
66+
foreach ($line in $lines) {
67+
$jp = $line | ConvertFrom-Json
68+
if ($jp.activity) { # if line is a progress message
69+
$jp.percent_complete | Should -BeIn (0..100)
70+
$ProgressMessagesFound = $True
71+
}
72+
}
73+
$ProgressMessagesFound | Should -BeTrue
74+
}
75+
6176
It 'Capabilities are returned' {
6277
$resource = dsc resource list Microsoft/OSInfo | ConvertFrom-Json
6378
$LASTEXITCODE | Should -Be 0

0 commit comments

Comments
 (0)