Skip to content

Commit f05a4e8

Browse files
committed
finish internationalization
1 parent 71a9011 commit f05a4e8

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

runcommandonset/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ lto = true
1212

1313
[dependencies]
1414
clap = { version = "4.4", features = ["derive"] }
15-
rust-i18n = { version = "3" }
15+
rust-i18n = { version = "3.1" }
1616
serde = { version = "1.0", features = ["derive"] }
1717
serde_json = { version = "1.0", features = ["preserve_order"] }
1818
tracing = { version = "0.1" }

runcommandonset/locales/en-us.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@ readStdin = "Reading input from STDIN"
99

1010
[runcommand]
1111
invalidJson = "Failed to serialize to JSON"
12+
13+
[utils]
14+
executableRequired = "Executable is required when input is not provided via stdin"
15+
failedToExecute = "Failed to execute"
16+
failedOpenStderr = "Failed to open stderr for"
17+
failedOpenStdout = "Failed to open stdout for"
18+
failedReadStderr = "Failed to read stderr for"
19+
failedReadStdout = "Failed to read stdout for"
20+
failedWait = "Failed to wait for"
21+
invalidInput = "Input is not valid"
22+
unableToTrace = "Unable to set global default tracing subscriber. Tracing is diabled."

runcommandonset/src/main.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
// Licensed under the MIT License.
33

44
use clap::Parser;
5+
use rust_i18n::{i18n, t};
56
use std::{io::{self, Read, IsTerminal}, process::exit};
6-
use tracing::{error, warn, debug};
7+
use tracing::{error, warn, debug, trace};
78

89
use args::{Arguments, SubCommand, TraceLevel};
910
use runcommand::RunCommand;
10-
//use rust_i18n::t;
1111
use utils::{enable_tracing, invoke_command, parse_input, EXIT_INVALID_ARGS};
1212

13-
#[macro_use]
14-
extern crate rust_i18n;
15-
i18n!("locales", fallback = "en");
16-
1713
pub mod args;
1814
pub mod runcommand;
1915
pub mod utils;
2016

17+
i18n!("locales", fallback = "en");
18+
2119
fn main() {
2220
let args = Arguments::parse();
2321
let trace_level = match args.trace_level {
@@ -32,6 +30,7 @@ fn main() {
3230
"debug" => TraceLevel::Debug,
3331
"trace" => TraceLevel::Trace,
3432
_ => {
33+
//warn!("{}: {trace_level}", t!("main.invalidTraceLevel"));
3534
warn!("{}: {trace_level}", t!("main.invalidTraceLevel"));
3635
TraceLevel::Info
3736
}
@@ -43,12 +42,12 @@ fn main() {
4342
}
4443
};
4544
enable_tracing(&trace_level, &args.trace_format);
46-
warn!(t!("main.notIdempotent"));
45+
warn!("{}", t!("main.notIdempotent"));
4746

4847
let stdin = if std::io::stdin().is_terminal() {
4948
None
5049
} else {
51-
debug!(t!("main.readStdin"));
50+
debug!("{}", t!("main.readStdin"));
5251
let mut buffer: Vec<u8> = Vec::new();
5352
io::stdin().read_to_end(&mut buffer).unwrap();
5453
let stdin = match String::from_utf8(buffer) {
@@ -60,7 +59,7 @@ fn main() {
6059
};
6160
// parse_input expects at most 1 input, so wrapping Some(empty input) would throw it off
6261
if stdin.is_empty() {
63-
debug!(t!("main.emptyStdin"));
62+
debug!("{}", t!("main.emptyStdin"));
6463
None
6564
}
6665
else {
@@ -77,9 +76,8 @@ fn main() {
7776
SubCommand::Set { arguments, executable, exit_code } => {
7877
command = parse_input(arguments, executable, exit_code, stdin);
7978
let (exit_code, stdout, stderr) = invoke_command(command.executable.as_ref(), command.arguments.clone());
80-
// TODO: convert this to tracing json once other PR is merged to handle tracing from resources
81-
eprintln!("Stdout: {stdout}");
82-
eprintln!("Stderr: {stderr}");
79+
trace!("Stdout: {stdout}");
80+
trace!("Stderr: {stderr}");
8381
command.exit_code = exit_code;
8482
}
8583
}

runcommandonset/src/utils.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
use rust_i18n::t;
45
use std::{io::Read, process::{Command, exit, Stdio}};
56
use tracing::{Level, error, debug, trace};
67
use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, Layer};
@@ -33,7 +34,7 @@ pub fn parse_input(arguments: Option<Vec<String>>, executable: Option<String>, e
3334
command = match serde_json::from_str(&input) {
3435
Ok(json) => json,
3536
Err(err) => {
36-
error!("Error: Input is not valid: {err}");
37+
error!("{}: {err}", t!("utils.invalidInput"));
3738
exit(EXIT_INVALID_INPUT);
3839
}
3940
}
@@ -45,7 +46,7 @@ pub fn parse_input(arguments: Option<Vec<String>>, executable: Option<String>, e
4546
};
4647
}
4748
else {
48-
error!("Error: Executable is required when input is not provided via stdin");
49+
error!("{}", t!("utils.executableRequired"));
4950
exit(EXIT_INVALID_INPUT);
5051
}
5152
command
@@ -72,7 +73,7 @@ pub fn enable_tracing(trace_level: &TraceLevel, trace_format: &TraceFormat) {
7273
};
7374

7475
let filter = EnvFilter::try_from_default_env()
75-
.or_else(|_| EnvFilter::try_new("warning"))
76+
.or_else(|_| EnvFilter::try_new("warn"))
7677
.unwrap_or_default()
7778
.add_directive(tracing_level.into());
7879
let layer = tracing_subscriber::fmt::Layer::default().with_writer(std::io::stderr);
@@ -104,7 +105,7 @@ pub fn enable_tracing(trace_level: &TraceLevel, trace_format: &TraceFormat) {
104105
let subscriber = tracing_subscriber::Registry::default().with(fmt).with(filter);
105106

106107
if tracing::subscriber::set_global_default(subscriber).is_err() {
107-
eprintln!("Unable to set global default tracing subscriber. Tracing is diabled.");
108+
eprintln!("{}", t!("utils.unableToTrace"));
108109
}
109110
}
110111

@@ -132,41 +133,41 @@ pub fn invoke_command(executable: &str, args: Option<Vec<String>>) -> (i32, Stri
132133
let mut child = match command.spawn() {
133134
Ok(child) => child,
134135
Err(e) => {
135-
error!("Failed to execute {}: {e}", executable);
136+
error!("{} '{executable}': {e}", t!("utils.failedToExecute"));
136137
exit(EXIT_DSC_ERROR);
137138
}
138139
};
139140

140141
let Some(mut child_stdout) = child.stdout.take() else {
141-
error!("Failed to open stdout for {}", executable);
142+
error!("{} {executable}", t!("utils.failedOpenStdout"));
142143
exit(EXIT_DSC_ERROR);
143144
};
144145
let mut stdout_buf = Vec::new();
145146
match child_stdout.read_to_end(&mut stdout_buf) {
146147
Ok(_) => (),
147148
Err(e) => {
148-
error!("Failed to read stdout for {}: {e}", executable);
149+
error!("{} '{executable}': {e}", t!("utils.failedReadStdout"));
149150
exit(EXIT_DSC_ERROR);
150151
}
151152
}
152153

153154
let Some(mut child_stderr) = child.stderr.take() else {
154-
error!("Failed to open stderr for {}", executable);
155+
error!("{} {executable}", t!("utils.failedOpenStderr"));
155156
exit(EXIT_DSC_ERROR);
156157
};
157158
let mut stderr_buf = Vec::new();
158159
match child_stderr.read_to_end(&mut stderr_buf) {
159160
Ok(_) => (),
160161
Err(e) => {
161-
error!("Failed to read stderr for {}: {e}", executable);
162+
error!("{} '{executable}': {e}", t!("utils.failedReadStderr"));
162163
exit(EXIT_DSC_ERROR);
163164
}
164165
}
165166

166167
let exit_status = match child.wait() {
167168
Ok(exit_status) => exit_status,
168169
Err(e) => {
169-
error!("Failed to wait for {}: {e}", executable);
170+
error!("{} '{executable}': {e}", t!("utils.failedWait"));
170171
exit(EXIT_DSC_ERROR);
171172
}
172173
};

0 commit comments

Comments
 (0)