Skip to content

Commit e7683ad

Browse files
committed
Change how to pass STDIN input via --file, rename other args
1 parent b52b5cb commit e7683ad

32 files changed

+241
-275
lines changed

dsc/src/args.rs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,32 @@ pub enum SubCommand {
7676
pub enum ConfigSubCommand {
7777
#[clap(name = "get", about = "Retrieve the current configuration")]
7878
Get {
79-
#[clap(short = 'd', long, help = "The document to pass to the configuration or resource", conflicts_with = "path")]
80-
document: Option<String>,
81-
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource", conflicts_with = "document")]
82-
path: Option<String>,
83-
#[clap(short = 'f', long, help = "The output format to use")]
84-
format: Option<OutputFormat>,
79+
#[clap(short = 'i', long, help = "The input document as JSON or YAML to pass to the configuration or resource", conflicts_with = "file")]
80+
input: Option<String>,
81+
#[clap(short = 'f', long, help = "The path to a file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
82+
file: Option<String>,
83+
#[clap(short = 'o', long, help = "The output format to use")]
84+
output_format: Option<OutputFormat>,
8585
},
8686
#[clap(name = "set", about = "Set the current configuration")]
8787
Set {
88-
#[clap(short = 'd', long, help = "The document to pass to the configuration or resource", conflicts_with = "path")]
89-
document: Option<String>,
90-
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource", conflicts_with = "document")]
91-
path: Option<String>,
92-
#[clap(short = 'f', long, help = "The output format to use")]
93-
format: Option<OutputFormat>,
88+
#[clap(short = 'i', long, help = "The input document as JSON or YAML to pass to the configuration or resource", conflicts_with = "file")]
89+
input: Option<String>,
90+
#[clap(short = 'f', long, help = "The path to a file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
91+
file: Option<String>,
92+
#[clap(short = 'o', long, help = "The output format to use")]
93+
output_format: Option<OutputFormat>,
9494
#[clap(short = 'w', long, help = "Run as a what-if operation instead of executing the configuration or resource")]
9595
what_if: bool,
9696
},
9797
#[clap(name = "test", about = "Test the current configuration")]
9898
Test {
99-
#[clap(short = 'd', long, help = "The document to pass to the configuration or resource", conflicts_with = "path")]
100-
document: Option<String>,
101-
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource", conflicts_with = "document")]
102-
path: Option<String>,
103-
#[clap(short = 'f', long, help = "The output format to use")]
104-
format: Option<OutputFormat>,
99+
#[clap(short = 'i', long, help = "The input document as JSON or YAML to pass to the configuration or resource", conflicts_with = "file")]
100+
input: Option<String>,
101+
#[clap(short = 'f', long, help = "The path to a file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
102+
file: Option<String>,
103+
#[clap(short = 'o', long, help = "The output format to use")]
104+
output_format: Option<OutputFormat>,
105105
// Used by Assertion resource to return `test` result as a `get` result
106106
#[clap(long, hide = true)]
107107
as_get: bool,
@@ -111,30 +111,30 @@ pub enum ConfigSubCommand {
111111
},
112112
#[clap(name = "validate", about = "Validate the current configuration", hide = true)]
113113
Validate {
114-
#[clap(short = 'd', long, help = "The document to pass to the configuration or resource", conflicts_with = "path")]
115-
document: Option<String>,
116-
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource", conflicts_with = "document")]
117-
path: Option<String>,
118-
#[clap(short = 'f', long, help = "The output format to use")]
119-
format: Option<OutputFormat>,
114+
#[clap(short = 'i', long, help = "The document to pass to the configuration or resource", conflicts_with = "file")]
115+
input: Option<String>,
116+
#[clap(short = 'f', long, help = "The path to a file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
117+
file: Option<String>,
118+
#[clap(short = 'o', long, help = "The output format to use")]
119+
output_format: Option<OutputFormat>,
120120
},
121121
#[clap(name = "export", about = "Export the current configuration")]
122122
Export {
123-
#[clap(short = 'd', long, help = "The document to pass to the configuration or resource", conflicts_with = "path")]
124-
document: Option<String>,
125-
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource", conflicts_with = "document")]
126-
path: Option<String>,
127-
#[clap(short = 'f', long, help = "The output format to use")]
128-
format: Option<OutputFormat>,
123+
#[clap(short = 'i', long, help = "The document to pass to the configuration or resource", conflicts_with = "file")]
124+
input: Option<String>,
125+
#[clap(short = 'f', long, help = "The path to a file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
126+
file: Option<String>,
127+
#[clap(short = 'o', long, help = "The output format to use")]
128+
output_format: Option<OutputFormat>,
129129
},
130130
#[clap(name = "resolve", about = "Resolve the current configuration", hide = true)]
131131
Resolve {
132-
#[clap(short = 'd', long, help = "The document to pass to the configuration or resource", conflicts_with = "path")]
133-
document: Option<String>,
134-
#[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource", conflicts_with = "document")]
135-
path: Option<String>,
136-
#[clap(short = 'f', long, help = "The output format to use")]
137-
format: Option<OutputFormat>,
132+
#[clap(short = 'i', long, help = "The document to pass to the configuration or resource", conflicts_with = "file")]
133+
input: Option<String>,
134+
#[clap(short = 'f', long, help = "The path to a file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
135+
file: Option<String>,
136+
#[clap(short = 'o', long, help = "The output format to use")]
137+
output_format: Option<OutputFormat>,
138138
}
139139
}
140140

@@ -151,66 +151,66 @@ pub enum ResourceSubCommand {
151151
description: Option<String>,
152152
#[clap(short, long, help = "Tag to search for in the resource tags")]
153153
tags: Option<Vec<String>>,
154-
#[clap(short = 'f', long, help = "The output format to use")]
155-
format: Option<OutputFormat>,
154+
#[clap(short = 'o', long, help = "The output format to use")]
155+
output_format: Option<OutputFormat>,
156156
},
157157
#[clap(name = "get", about = "Invoke the get operation to a resource", arg_required_else_help = true)]
158158
Get {
159159
#[clap(short, long, help = "Get all instances of the resource")]
160160
all: bool,
161161
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `get` on")]
162162
resource: String,
163-
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "path")]
163+
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "file")]
164164
input: Option<String>,
165-
#[clap(short = 'p', long, help = "The path to a JSON or YAML file used as input to the configuration or resource", conflicts_with = "input")]
166-
path: Option<String>,
167-
#[clap(short = 'f', long, help = "The output format to use")]
168-
format: Option<OutputFormat>,
165+
#[clap(short = 'f', long, help = "The path to a JSON or YAML file used as input to the configuration or resource. Use '-' as the file to read from STDIN.", conflicts_with = "input")]
166+
file: Option<String>,
167+
#[clap(short = 'o', long, help = "The output format to use")]
168+
output_format: Option<OutputFormat>,
169169
},
170170
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
171171
Set {
172172
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `set` on")]
173173
resource: String,
174-
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "path")]
174+
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "file")]
175175
input: Option<String>,
176-
#[clap(short = 'p', long, help = "The path to a JSON or YAML file used as input to the configuration or resource", conflicts_with = "input")]
177-
path: Option<String>,
178-
#[clap(short = 'f', long, help = "The output format to use")]
179-
format: Option<OutputFormat>,
176+
#[clap(short = 'f', long, help = "The path to a JSON or YAML file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
177+
file: Option<String>,
178+
#[clap(short = 'o', long, help = "The output format to use")]
179+
output_format: Option<OutputFormat>,
180180
},
181181
#[clap(name = "test", about = "Invoke the test operation to a resource", arg_required_else_help = true)]
182182
Test {
183183
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `test` on")]
184184
resource: String,
185-
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "path")]
185+
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "file")]
186186
input: Option<String>,
187-
#[clap(short = 'p', long, help = "The path to a JSON or YAML file used as input to the configuration or resource", conflicts_with = "input")]
188-
path: Option<String>,
187+
#[clap(short = 'p', long, help = "The path to a JSON or YAML file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
188+
file: Option<String>,
189189
#[clap(short = 'f', long, help = "The output format to use")]
190-
format: Option<OutputFormat>,
190+
output_format: Option<OutputFormat>,
191191
},
192192
#[clap(name = "delete", about = "Invoke the delete operation to a resource", arg_required_else_help = true)]
193193
Delete {
194194
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `delete` on")]
195195
resource: String,
196-
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "path")]
196+
#[clap(short, long, help = "The input to pass to the resource as JSON or YAML", conflicts_with = "file")]
197197
input: Option<String>,
198-
#[clap(short = 'p', long, help = "The path to a JSON or YAML file used as input to the configuration or resource", conflicts_with = "input")]
199-
path: Option<String>,
198+
#[clap(short = 'p', long, help = "The path to a JSON or YAML file used as input to the configuration or resource. Use '-' for the file to read from STDIN.", conflicts_with = "input")]
199+
file: Option<String>,
200200
},
201201
#[clap(name = "schema", about = "Get the JSON schema for a resource", arg_required_else_help = true)]
202202
Schema {
203203
#[clap(short, long, help = "The name of the resource to get the JSON schema")]
204204
resource: String,
205-
#[clap(short = 'f', long, help = "The output format to use")]
206-
format: Option<OutputFormat>,
205+
#[clap(short = 'o', long, help = "The output format to use")]
206+
output_format: Option<OutputFormat>,
207207
},
208208
#[clap(name = "export", about = "Retrieve all resource instances", arg_required_else_help = true)]
209209
Export {
210210
#[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `export` on")]
211211
resource: String,
212-
#[clap(short = 'f', long, help = "The output format to use")]
213-
format: Option<OutputFormat>,
212+
#[clap(short = 'o', long, help = "The output format to use")]
213+
output_format: Option<OutputFormat>,
214214
},
215215
}
216216

dsc/src/main.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
use args::{Args, SubCommand};
55
use clap::{CommandFactory, Parser};
66
use clap_complete::generate;
7-
use std::io::{self, IsTerminal, Read};
8-
use std::process::exit;
7+
use std::{io, process::exit};
98
use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind};
109
use tracing::{error, info, warn, debug};
1110

@@ -38,30 +37,6 @@ fn main() {
3837

3938
debug!("Running dsc {}", env!("CARGO_PKG_VERSION"));
4039

41-
let input = if io::stdin().is_terminal() {
42-
None
43-
} else {
44-
info!("Reading input from STDIN");
45-
let mut buffer: Vec<u8> = Vec::new();
46-
io::stdin().read_to_end(&mut buffer).unwrap();
47-
let input = match String::from_utf8(buffer) {
48-
Ok(input) => input,
49-
Err(e) => {
50-
error!("Invalid UTF-8 sequence: {e}");
51-
exit(util::EXIT_INVALID_ARGS);
52-
},
53-
};
54-
// get_input call expects at most 1 input, so wrapping Some(empty input) would throw it off
55-
// have only seen this happen with dsc_args.test.ps1 running on the CI pipeline
56-
if input.is_empty() {
57-
debug!("Input from STDIN is empty");
58-
None
59-
}
60-
else {
61-
Some(input)
62-
}
63-
};
64-
6540
match args.subcommand {
6641
SubCommand::Completer { shell } => {
6742
info!("Generating completion script for {:?}", shell);
@@ -72,19 +47,19 @@ fn main() {
7247
if let Some(file_name) = parameters_file {
7348
info!("Reading parameters from file {file_name}");
7449
match std::fs::read_to_string(&file_name) {
75-
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &system_root, &input, &as_group, &as_include),
50+
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &system_root, &as_group, &as_include),
7651
Err(err) => {
7752
error!("Error: Failed to read parameters file '{file_name}': {err}");
7853
exit(util::EXIT_INVALID_INPUT);
7954
}
8055
}
8156
}
8257
else {
83-
subcommand::config(&subcommand, &parameters, &system_root, &input, &as_group, &as_include);
58+
subcommand::config(&subcommand, &parameters, &system_root, &as_group, &as_include);
8459
}
8560
},
8661
SubCommand::Resource { subcommand } => {
87-
subcommand::resource(&subcommand, &input);
62+
subcommand::resource(&subcommand);
8863
},
8964
SubCommand::Schema { dsc_type , format } => {
9065
let schema = util::get_schema(dsc_type);

0 commit comments

Comments
 (0)