From 71a901183db8c310734095c15870358a04681696 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 5 Dec 2024 15:36:20 -0800 Subject: [PATCH 1/5] initial change to i18n --- runcommandonset/Cargo.lock | 379 ++++++++++++++++++++++++++++- runcommandonset/Cargo.toml | 1 + runcommandonset/locales/en-us.toml | 11 + runcommandonset/src/main.rs | 15 +- runcommandonset/src/runcommand.rs | 3 +- 5 files changed, 400 insertions(+), 9 deletions(-) create mode 100644 runcommandonset/locales/en-us.toml diff --git a/runcommandonset/Cargo.lock b/runcommandonset/Cargo.lock index 6eeac6f8..0b82a3a9 100644 --- a/runcommandonset/Cargo.lock +++ b/runcommandonset/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -47,7 +47,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -57,7 +57,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + +[[package]] +name = "base62" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48fa474cf7492f9a299ba6019fb99ec673e1739556d48e8a90eabaea282ef0e4" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "bstr" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a68f1f47cdf0ec8ee4b941b2eee2a80cb796db73118c0dd09ac63fbe405be22" +dependencies = [ + "memchr", + "serde", ] [[package]] @@ -112,12 +146,89 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "fastrand" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "486f806e73c5707928240ddc295403b1b93c96a02038563881c4a2fd84b81ac4" + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "globset" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata 0.4.8", + "regex-syntax 0.8.5", +] + +[[package]] +name = "globwalk" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" +dependencies = [ + "bitflags 1.3.2", + "ignore", + "walkdir", +] + [[package]] name = "hashbrown" version = "0.15.0" @@ -130,6 +241,22 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "ignore" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata 0.4.8", + "same-file", + "walkdir", + "winapi-util", +] + [[package]] name = "indexmap" version = "2.6.0" @@ -146,6 +273,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -158,6 +294,24 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "libc" +version = "0.2.167" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" + +[[package]] +name = "libyml" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64804cc6a5042d4f05379909ba25b503ec04e2c082151d62122d5dcaa274b961" + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "log" version = "0.4.22" @@ -179,6 +333,15 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "normpath" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8911957c4b1549ac0dc74e30db9c8b0e66ddcd6d7acc33098f4c63a64a6d7ed" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -274,18 +437,95 @@ name = "runcommandonset" version = "0.1.0" dependencies = [ "clap", + "rust-i18n", "serde", "serde_json", "tracing", "tracing-subscriber", ] +[[package]] +name = "rust-i18n" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "039f57d22229db401af3458ca939300178e99e88b938573cea12b7c2b0f09724" +dependencies = [ + "globwalk", + "once_cell", + "regex", + "rust-i18n-macro", + "rust-i18n-support", + "smallvec", +] + +[[package]] +name = "rust-i18n-macro" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dde5c022360a2e54477882843d56b6f9bcb4bc62f504b651a2f497f0028d174f" +dependencies = [ + "glob", + "once_cell", + "proc-macro2", + "quote", + "rust-i18n-support", + "serde", + "serde_json", + "serde_yml", + "syn", +] + +[[package]] +name = "rust-i18n-support" +version = "3.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75d2844d36f62b5d6b66f9cf8f8cbdbbbdcdb5fd37a473a9cc2fb45fdcf485d2" +dependencies = [ + "arc-swap", + "base62", + "globwalk", + "itertools", + "lazy_static", + "normpath", + "once_cell", + "proc-macro2", + "regex", + "serde", + "serde_json", + "serde_yml", + "siphasher", + "toml", + "triomphe", +] + +[[package]] +name = "rustix" +version = "0.38.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" +dependencies = [ + "bitflags 2.6.0", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "serde" version = "1.0.210" @@ -319,6 +559,32 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_yml" +version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e76bab63c3fd98d27c17f9cbce177f64a91f5e69ac04cafe04e1bb25d1dc3c" +dependencies = [ + "indexmap", + "itoa", + "libyml", + "log", + "memchr", + "ryu", + "serde", + "serde_json", + "tempfile", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -328,12 +594,24 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "siphasher" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" + [[package]] name = "smallvec" version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "strsim" version = "0.11.1" @@ -351,6 +629,19 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c" +dependencies = [ + "cfg-if", + "fastrand", + "once_cell", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -361,6 +652,40 @@ dependencies = [ "once_cell", ] +[[package]] +name = "toml" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79e69d3b627db300ff956027cc6c3798cef26d22526befdfcd12feeb6d2257" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tracing" version = "0.1.40" @@ -435,6 +760,17 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "triomphe" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef8f7726da4807b58ea5c96fdc122f80702030edc33b35aff9190a51148ccc85" +dependencies = [ + "arc-swap", + "serde", + "stable_deref_trait", +] + [[package]] name = "unicode-ident" version = "1.0.13" @@ -453,6 +789,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "winapi" version = "0.3.9" @@ -469,6 +815,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -484,6 +839,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -547,3 +911,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] diff --git a/runcommandonset/Cargo.toml b/runcommandonset/Cargo.toml index 00d4a998..f42017c0 100644 --- a/runcommandonset/Cargo.toml +++ b/runcommandonset/Cargo.toml @@ -12,6 +12,7 @@ lto = true [dependencies] clap = { version = "4.4", features = ["derive"] } +rust-i18n = { version = "3" } serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", features = ["preserve_order"] } tracing = { version = "0.1" } diff --git a/runcommandonset/locales/en-us.toml b/runcommandonset/locales/en-us.toml new file mode 100644 index 00000000..8a09a9f1 --- /dev/null +++ b/runcommandonset/locales/en-us.toml @@ -0,0 +1,11 @@ +_version = 1 + +[main] +emptyStdin = "Input from STDIN is empty" +invalidTraceLevel = "Defaulting to Info, invalid trace level defined in DSC_TRACE_LEVEL env var" +invalidUtf8 = "Invalid UTF-8 sequence" +notIdempotent = "This resource is not idempotent" +readStdin = "Reading input from STDIN" + +[runcommand] +invalidJson = "Failed to serialize to JSON" diff --git a/runcommandonset/src/main.rs b/runcommandonset/src/main.rs index d4557d05..6176f70b 100644 --- a/runcommandonset/src/main.rs +++ b/runcommandonset/src/main.rs @@ -7,8 +7,13 @@ use tracing::{error, warn, debug}; use args::{Arguments, SubCommand, TraceLevel}; use runcommand::RunCommand; +//use rust_i18n::t; use utils::{enable_tracing, invoke_command, parse_input, EXIT_INVALID_ARGS}; +#[macro_use] +extern crate rust_i18n; +i18n!("locales", fallback = "en"); + pub mod args; pub mod runcommand; pub mod utils; @@ -27,7 +32,7 @@ fn main() { "debug" => TraceLevel::Debug, "trace" => TraceLevel::Trace, _ => { - warn!("Invalid trace level: {trace_level}"); + warn!("{}: {trace_level}", t!("main.invalidTraceLevel")); TraceLevel::Info } } @@ -38,24 +43,24 @@ fn main() { } }; enable_tracing(&trace_level, &args.trace_format); - warn!("This resource is not idempotent"); + warn!(t!("main.notIdempotent")); let stdin = if std::io::stdin().is_terminal() { None } else { - debug!("Reading input from STDIN"); + debug!(t!("main.readStdin")); let mut buffer: Vec = Vec::new(); io::stdin().read_to_end(&mut buffer).unwrap(); let stdin = match String::from_utf8(buffer) { Ok(stdin) => stdin, Err(e) => { - error!("Invalid UTF-8 sequence: {e}"); + error!("{}: {e}", t!("main.invalidUtf8")); exit(EXIT_INVALID_ARGS); }, }; // parse_input expects at most 1 input, so wrapping Some(empty input) would throw it off if stdin.is_empty() { - debug!("Input from STDIN is empty"); + debug!(t!("main.emptyStdin")); None } else { diff --git a/runcommandonset/src/runcommand.rs b/runcommandonset/src/runcommand.rs index ae5ce81c..f7a3be6a 100644 --- a/runcommandonset/src/runcommand.rs +++ b/runcommandonset/src/runcommand.rs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +use rust_i18n::t; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Clone, PartialEq, Serialize)] @@ -19,7 +20,7 @@ impl RunCommand { match serde_json::to_string(self) { Ok(json) => json, Err(e) => { - eprintln!("Failed to serialize to JSON: {e}"); + eprintln!("{}: {e}", t!("runcommand.invalidJson")); String::new() } } From f05a4e81943cf40174dfcc7c1ab52b4fcbcb4973 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Dec 2024 17:03:31 -0800 Subject: [PATCH 2/5] finish internationalization --- runcommandonset/Cargo.toml | 2 +- runcommandonset/locales/en-us.toml | 11 +++++++++++ runcommandonset/src/main.rs | 22 ++++++++++------------ runcommandonset/src/utils.rs | 21 +++++++++++---------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/runcommandonset/Cargo.toml b/runcommandonset/Cargo.toml index f42017c0..475523bc 100644 --- a/runcommandonset/Cargo.toml +++ b/runcommandonset/Cargo.toml @@ -12,7 +12,7 @@ lto = true [dependencies] clap = { version = "4.4", features = ["derive"] } -rust-i18n = { version = "3" } +rust-i18n = { version = "3.1" } serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", features = ["preserve_order"] } tracing = { version = "0.1" } diff --git a/runcommandonset/locales/en-us.toml b/runcommandonset/locales/en-us.toml index 8a09a9f1..5c043751 100644 --- a/runcommandonset/locales/en-us.toml +++ b/runcommandonset/locales/en-us.toml @@ -9,3 +9,14 @@ readStdin = "Reading input from STDIN" [runcommand] invalidJson = "Failed to serialize to JSON" + +[utils] +executableRequired = "Executable is required when input is not provided via stdin" +failedToExecute = "Failed to execute" +failedOpenStderr = "Failed to open stderr for" +failedOpenStdout = "Failed to open stdout for" +failedReadStderr = "Failed to read stderr for" +failedReadStdout = "Failed to read stdout for" +failedWait = "Failed to wait for" +invalidInput = "Input is not valid" +unableToTrace = "Unable to set global default tracing subscriber. Tracing is diabled." diff --git a/runcommandonset/src/main.rs b/runcommandonset/src/main.rs index 6176f70b..f30defcd 100644 --- a/runcommandonset/src/main.rs +++ b/runcommandonset/src/main.rs @@ -2,22 +2,20 @@ // Licensed under the MIT License. use clap::Parser; +use rust_i18n::{i18n, t}; use std::{io::{self, Read, IsTerminal}, process::exit}; -use tracing::{error, warn, debug}; +use tracing::{error, warn, debug, trace}; use args::{Arguments, SubCommand, TraceLevel}; use runcommand::RunCommand; -//use rust_i18n::t; use utils::{enable_tracing, invoke_command, parse_input, EXIT_INVALID_ARGS}; -#[macro_use] -extern crate rust_i18n; -i18n!("locales", fallback = "en"); - pub mod args; pub mod runcommand; pub mod utils; +i18n!("locales", fallback = "en"); + fn main() { let args = Arguments::parse(); let trace_level = match args.trace_level { @@ -32,6 +30,7 @@ fn main() { "debug" => TraceLevel::Debug, "trace" => TraceLevel::Trace, _ => { + //warn!("{}: {trace_level}", t!("main.invalidTraceLevel")); warn!("{}: {trace_level}", t!("main.invalidTraceLevel")); TraceLevel::Info } @@ -43,12 +42,12 @@ fn main() { } }; enable_tracing(&trace_level, &args.trace_format); - warn!(t!("main.notIdempotent")); + warn!("{}", t!("main.notIdempotent")); let stdin = if std::io::stdin().is_terminal() { None } else { - debug!(t!("main.readStdin")); + debug!("{}", t!("main.readStdin")); let mut buffer: Vec = Vec::new(); io::stdin().read_to_end(&mut buffer).unwrap(); let stdin = match String::from_utf8(buffer) { @@ -60,7 +59,7 @@ fn main() { }; // parse_input expects at most 1 input, so wrapping Some(empty input) would throw it off if stdin.is_empty() { - debug!(t!("main.emptyStdin")); + debug!("{}", t!("main.emptyStdin")); None } else { @@ -77,9 +76,8 @@ fn main() { SubCommand::Set { arguments, executable, exit_code } => { command = parse_input(arguments, executable, exit_code, stdin); let (exit_code, stdout, stderr) = invoke_command(command.executable.as_ref(), command.arguments.clone()); - // TODO: convert this to tracing json once other PR is merged to handle tracing from resources - eprintln!("Stdout: {stdout}"); - eprintln!("Stderr: {stderr}"); + trace!("Stdout: {stdout}"); + trace!("Stderr: {stderr}"); command.exit_code = exit_code; } } diff --git a/runcommandonset/src/utils.rs b/runcommandonset/src/utils.rs index 62937499..54330743 100644 --- a/runcommandonset/src/utils.rs +++ b/runcommandonset/src/utils.rs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +use rust_i18n::t; use std::{io::Read, process::{Command, exit, Stdio}}; use tracing::{Level, error, debug, trace}; use tracing_subscriber::{filter::EnvFilter, layer::SubscriberExt, Layer}; @@ -33,7 +34,7 @@ pub fn parse_input(arguments: Option>, executable: Option, e command = match serde_json::from_str(&input) { Ok(json) => json, Err(err) => { - error!("Error: Input is not valid: {err}"); + error!("{}: {err}", t!("utils.invalidInput")); exit(EXIT_INVALID_INPUT); } } @@ -45,7 +46,7 @@ pub fn parse_input(arguments: Option>, executable: Option, e }; } else { - error!("Error: Executable is required when input is not provided via stdin"); + error!("{}", t!("utils.executableRequired")); exit(EXIT_INVALID_INPUT); } command @@ -72,7 +73,7 @@ pub fn enable_tracing(trace_level: &TraceLevel, trace_format: &TraceFormat) { }; let filter = EnvFilter::try_from_default_env() - .or_else(|_| EnvFilter::try_new("warning")) + .or_else(|_| EnvFilter::try_new("warn")) .unwrap_or_default() .add_directive(tracing_level.into()); 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) { let subscriber = tracing_subscriber::Registry::default().with(fmt).with(filter); if tracing::subscriber::set_global_default(subscriber).is_err() { - eprintln!("Unable to set global default tracing subscriber. Tracing is diabled."); + eprintln!("{}", t!("utils.unableToTrace")); } } @@ -132,33 +133,33 @@ pub fn invoke_command(executable: &str, args: Option>) -> (i32, Stri let mut child = match command.spawn() { Ok(child) => child, Err(e) => { - error!("Failed to execute {}: {e}", executable); + error!("{} '{executable}': {e}", t!("utils.failedToExecute")); exit(EXIT_DSC_ERROR); } }; let Some(mut child_stdout) = child.stdout.take() else { - error!("Failed to open stdout for {}", executable); + error!("{} {executable}", t!("utils.failedOpenStdout")); exit(EXIT_DSC_ERROR); }; let mut stdout_buf = Vec::new(); match child_stdout.read_to_end(&mut stdout_buf) { Ok(_) => (), Err(e) => { - error!("Failed to read stdout for {}: {e}", executable); + error!("{} '{executable}': {e}", t!("utils.failedReadStdout")); exit(EXIT_DSC_ERROR); } } let Some(mut child_stderr) = child.stderr.take() else { - error!("Failed to open stderr for {}", executable); + error!("{} {executable}", t!("utils.failedOpenStderr")); exit(EXIT_DSC_ERROR); }; let mut stderr_buf = Vec::new(); match child_stderr.read_to_end(&mut stderr_buf) { Ok(_) => (), Err(e) => { - error!("Failed to read stderr for {}: {e}", executable); + error!("{} '{executable}': {e}", t!("utils.failedReadStderr")); exit(EXIT_DSC_ERROR); } } @@ -166,7 +167,7 @@ pub fn invoke_command(executable: &str, args: Option>) -> (i32, Stri let exit_status = match child.wait() { Ok(exit_status) => exit_status, Err(e) => { - error!("Failed to wait for {}: {e}", executable); + error!("{} '{executable}': {e}", t!("utils.failedWait")); exit(EXIT_DSC_ERROR); } }; From 0535e080b4d85deaa2f960e079c6542fae6f12d1 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Dec 2024 17:07:57 -0800 Subject: [PATCH 3/5] remove commented out code --- runcommandonset/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runcommandonset/src/main.rs b/runcommandonset/src/main.rs index f30defcd..6419b79c 100644 --- a/runcommandonset/src/main.rs +++ b/runcommandonset/src/main.rs @@ -30,7 +30,6 @@ fn main() { "debug" => TraceLevel::Debug, "trace" => TraceLevel::Trace, _ => { - //warn!("{}: {trace_level}", t!("main.invalidTraceLevel")); warn!("{}: {trace_level}", t!("main.invalidTraceLevel")); TraceLevel::Info } From 460a5fd8eb3fba7c33b8841ba824e827c6b50b05 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Dec 2024 17:17:53 -0800 Subject: [PATCH 4/5] fix fallback --- runcommandonset/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runcommandonset/src/main.rs b/runcommandonset/src/main.rs index 6419b79c..6ac8b437 100644 --- a/runcommandonset/src/main.rs +++ b/runcommandonset/src/main.rs @@ -14,7 +14,7 @@ pub mod args; pub mod runcommand; pub mod utils; -i18n!("locales", fallback = "en"); +i18n!("locales", fallback = "en-us"); fn main() { let args = Arguments::parse(); From 09bf0c528d87617baa357c3ce8cbce0d3f374a23 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Fri, 6 Dec 2024 17:35:11 -0800 Subject: [PATCH 5/5] update tests based on product change --- .../tests/runcommandonset.set.tests.ps1 | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/runcommandonset/tests/runcommandonset.set.tests.ps1 b/runcommandonset/tests/runcommandonset.set.tests.ps1 index 9bc6ccf4..c6631204 100644 --- a/runcommandonset/tests/runcommandonset.set.tests.ps1 +++ b/runcommandonset/tests/runcommandonset.set.tests.ps1 @@ -36,20 +36,20 @@ Describe 'tests for runcommandonset set' { "arguments": ["-Command", "echo hello world"] } "@ - $input_json | runcommandonset set 2> $TestDrive/output.txt - $actual = Get-Content -Path $TestDrive/output.txt - $actual | Should -Contain 'Stdout: hello' - $actual | Should -Contain 'world' + $input_json | runcommandonset --trace-level trace --trace-format plaintext set 2> $TestDrive/output.txt + $actual = Get-Content -Path $TestDrive/output.txt -Raw + $actual | Should -BeLike '*Stdout: hello*' + $actual | Should -BeLike '*world*' $LASTEXITCODE | Should -Be 0 } It 'STDERR captured when calling resource directly with invalid args' { - $json = runcommandonset set -e pwsh -a "echo hello world" 2> $TestDrive/output.txt + $json = runcommandonset --trace-level trace --trace-format plaintext set -e pwsh -a "echo hello world" 2> $TestDrive/output.txt $stdout = $json | ConvertFrom-Json $stdout.exitCode | Should -Be 64 - $expected = "Stderr: The argument 'echo hello world' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again." - $stderr = Get-Content -Path $TestDrive/output.txt - $stderr | Should -Contain $expected + $expected = "*Stderr: The argument 'echo hello world' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.*" + $stderr = Get-Content -Path $TestDrive/output.txt -Raw + $stderr | Should -BeLike $expected $LASTEXITCODE | Should -Be 0 } @@ -78,9 +78,9 @@ Describe 'tests for runcommandonset set' { It 'Executable does not exist' { '{ "executable": "foo" }' | dsc -l trace resource set -r Microsoft.DSC.Transitional/RunCommandOnSet -f - 2> $TestDrive/output.txt $actual = Get-Content -Path $TestDrive/output.txt -Raw - $expected_logging = 'Failed to execute foo: No such file or directory (os error 2)' + $expected_logging = "Failed to execute 'foo': No such file or directory (os error 2)" if ($IsWindows) { - $expected_logging = 'Failed to execute foo: program not found' + $expected_logging = "Failed to execute 'foo': program not found" } $actual | Should -BeLike "*$expected_logging*" $LASTEXITCODE | Should -Be 2