From 893c4d1a9423b7f6911e21c2b116472dba8be88d Mon Sep 17 00:00:00 2001 From: Kenny <86278669+kvcache@users.noreply.github.com> Date: Fri, 20 May 2022 17:07:28 -0700 Subject: [PATCH] chore: upgrade momento sdk - consistency, less copypasta (#150) This change establishes a common pattern for interacting with the service. Rather than copy/pasting matches, MomentoError -> CliError mapping and json dump code around, this names those functionalities as functions: * interact_with_momento: Completes a momento future and maps the result to a Result< HappyCaseType, CliError > * print_whatever_this_is_as_json: prints whatever you feed it as json * drop windows lint, linux lint is enough * also choco install protoc in a windows build near you. Relates to https://github.com/tokio-rs/prost/pull/620 This change was precipitated by the change in the SDK to simplify and coalesce client creation behind the SimpleCacheClientBuilder and triggered by the SDK upgrade. There are small changes to debug and error messages, but this change should cause no functional changes. tired of copypasta. ``` pub async fn delete_cache(cache_name: String, auth_token: String) -> Result<(), CliError> { debug!("deleting cache..."); let mut momento = get_momento_client(auth_token).await?; match momento.delete_cache(&cache_name).await { Ok(_) => (), Err(e) => return Err(CliError { msg: e.to_string() }), }; Ok(()) } ``` becomes ``` pub async fn delete_cache(cache_name: String, auth_token: String) -> Result<(), CliError> { let mut client = get_momento_client(auth_token).await?; interact_with_momento("deleting cache...", client.delete_cache(&cache_name)).await } ``` --- .github/workflows/cd.yml | 2 + .github/workflows/ci.yml | 12 +- Cargo.lock | 341 ++++++++++++---------- Cargo.toml | 4 +- src/commands/cache/cache_cli.rs | 97 +++--- src/commands/signingkey/signingkey_cli.rs | 64 ++-- src/utils/client.rs | 49 ++++ src/utils/mod.rs | 1 + 8 files changed, 316 insertions(+), 254 deletions(-) create mode 100644 src/utils/client.rs diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 95ca117d..5fe2ada4 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -130,6 +130,8 @@ jobs: needs: release steps: - uses: actions/checkout@v2 + - name: i guess windows-latest does not have protoc or cmake but we need protoc + run: choco install protoc - uses: actions-rs/toolchain@v1 with: toolchain: stable diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00fc1a6e..fafb2705 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,16 +23,6 @@ jobs: - name: Rigorous lint via Clippy run: cargo clippy --all-targets --all-features -- -D warnings - rustfmt-windows: - name: Style & Lint - Windows - runs-on: windows-latest - steps: - - uses: actions/checkout@v2 - - name: rustfmt - run: cargo fmt -- --check - - name: Rigorous lint via Clippy - run: cargo clippy --all-targets --all-features -- -D warnings - build: runs-on: ubuntu-latest steps: @@ -79,6 +69,8 @@ jobs: runs-on: windows-latest steps: - uses: actions/checkout@v2 + - name: i guess windows-latest does not have protoc or cmake but we need protoc + run: choco install protoc - uses: actions-rs/toolchain@v1 with: toolchain: stable diff --git a/Cargo.lock b/Cargo.lock index 377ba562..be227d60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,20 +90,54 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] -name = "base64" -version = "0.13.0" +name = "axum" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +checksum = "ab2504b827a8bef941ba3dd64bdffe9cf56ca182908a147edd6189c95fbcae7d" +dependencies = [ + "async-trait", + "axum-core", + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "hyper", + "itoa 1.0.1", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde", + "sync_wrapper", + "tokio", + "tower", + "tower-http", + "tower-layer", + "tower-service", +] [[package]] -name = "base64-url" -version = "1.4.13" +name = "axum-core" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67a99c239d0c7e77c85dddfa9cebce48704b3c49550fcd3b84dd637e4484899f" +checksum = "da31c0ed7b4690e2c78fe4b880d21cd7db04a346ebc658b4270251b695437f17" dependencies = [ - "base64", + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "mime", ] +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.3.2" @@ -182,13 +216,22 @@ version = "3.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a1132dc3944b31c20dd8b906b3a9f0a5d0243e092d59171414969657ac6aa85" dependencies = [ - "heck 0.4.0", + "heck", "proc-macro-error", "proc-macro2", "quote", "syn", ] +[[package]] +name = "cmake" +version = "0.1.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" +dependencies = [ + "cc", +] + [[package]] name = "colored" version = "2.0.0" @@ -358,7 +401,7 @@ dependencies = [ "indexmap", "slab", "tokio", - "tokio-util", + "tokio-util 0.6.9", "tracing", ] @@ -368,15 +411,6 @@ version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" -[[package]] -name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "heck" version = "0.4.0" @@ -403,9 +437,9 @@ dependencies = [ [[package]] name = "http" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31f4c6746584866f0feabcc69893c5b51beef3831656a968ed7ae254cdc4fd03" +checksum = "ff8670570af52249509a86f5e3e18a08c60b177071826898fde8997cf5f6bfbb" dependencies = [ "bytes", "fnv", @@ -423,6 +457,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfe8eed0a9285ef776bb792479ea3834e8b94e13d615c2f66d03dd50a435a29" + [[package]] name = "httparse" version = "1.5.1" @@ -473,9 +513,9 @@ checksum = "d87c48c02e0dc5e3b849a2041db3029fd066650f8f717c07bf8ed78ccb895cac" dependencies = [ "http", "hyper", - "rustls 0.20.2", + "rustls", "tokio", - "tokio-rustls 0.23.2", + "tokio-rustls", ] [[package]] @@ -593,9 +633,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.14" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] @@ -621,6 +661,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" +[[package]] +name = "matchit" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73cbba799671b762df5a175adf59ce145165747bb891505c43d09aefbbf38beb" + [[package]] name = "memchr" version = "2.4.1" @@ -657,22 +703,18 @@ dependencies = [ [[package]] name = "momento" -version = "0.3.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29f048337ca301939302805d9a005c1e02fad98b1fd8d0dd949ff754bed0ea18" +checksum = "431bdfdeea355427ae190ceb532ebd6211064dba4de5c8c58a03f9bf4b27c4a3" dependencies = [ - "base64-url", "chrono", "jsonwebtoken", - "prost", - "rustls 0.19.1", + "log", + "momento-protos", + "rand", "serde", "serde_json", - "tokio", "tonic", - "tonic-build", - "tower", - "webpki 0.21.4", ] [[package]] @@ -697,6 +739,17 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "momento-protos" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e75ce56191eb20358b7e2ba26bc11d19abc087e9f78c568e24ea4c4476cd2ca6" +dependencies = [ + "prost", + "tonic", + "tonic-build", +] + [[package]] name = "multimap" version = "0.8.3" @@ -897,6 +950,16 @@ dependencies = [ "termtree", ] +[[package]] +name = "prettyplease" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9e07e3a46d0771a8a06b5f4441527802830b43e679ba12f44960f48dd4c6803" +dependencies = [ + "proc-macro2", + "syn", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -923,18 +986,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.36" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "prost" -version = "0.9.0" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "444879275cb4fd84958b1a1d5420d15e6fcf7c235fe47f053c9c2a80aceb6001" +checksum = "bc03e116981ff7d8da8e5c220e374587b98d294af7ba7dd7fda761158f00086f" dependencies = [ "bytes", "prost-derive", @@ -942,12 +1005,14 @@ dependencies = [ [[package]] name = "prost-build" -version = "0.9.0" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5" +checksum = "65a1118354442de7feb8a2a76f3d80ef01426bd45542c8c1fdffca41a758f846" dependencies = [ "bytes", - "heck 0.3.3", + "cfg-if", + "cmake", + "heck", "itertools", "lazy_static", "log", @@ -962,9 +1027,9 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9cc1a3263e07e0bf68e96268f37665207b49560d98739662cdfaae215c720fe" +checksum = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc" dependencies = [ "anyhow", "itertools", @@ -975,9 +1040,9 @@ dependencies = [ [[package]] name = "prost-types" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534b7a0e836e3c482d2693070f982e39e7611da9695d4d1f5a4b186b51faef0a" +checksum = "2d0a014229361011dc8e69c8a1ec6c2e8d0f2af7c91e3ea3f5b2170298461e68" dependencies = [ "bytes", "prost", @@ -1003,14 +1068,13 @@ dependencies = [ [[package]] name = "rand" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", "rand_core", - "rand_hc", ] [[package]] @@ -1032,15 +1096,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "rand_hc" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" -dependencies = [ - "rand_core", -] - [[package]] name = "redox_syscall" version = "0.2.10" @@ -1052,9 +1107,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.4" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" dependencies = [ "aho-corasick", "memchr", @@ -1108,18 +1163,18 @@ dependencies = [ "mime", "percent-encoding", "pin-project-lite", - "rustls 0.20.2", - "rustls-pemfile", + "rustls", + "rustls-pemfile 0.2.1", "serde", "serde_json", "serde_urlencoded", "tokio", - "tokio-rustls 0.23.2", + "tokio-rustls", "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "webpki-roots 0.22.2", + "webpki-roots", "winreg", ] @@ -1138,19 +1193,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "rustls" -version = "0.19.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7" -dependencies = [ - "base64", - "log", - "ring", - "sct 0.6.1", - "webpki 0.21.4", -] - [[package]] name = "rustls" version = "0.20.2" @@ -1159,18 +1201,18 @@ checksum = "d37e5e2290f3e040b594b1a9e04377c2c671f1a1cfd9bfdef82106ac1c113f84" dependencies = [ "log", "ring", - "sct 0.7.0", - "webpki 0.22.0", + "sct", + "webpki", ] [[package]] name = "rustls-native-certs" -version = "0.5.0" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a07b7c1885bd8ed3831c289b7870b13ef46fe0e856d288c30d9cc17d75a2092" +checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50" dependencies = [ "openssl-probe", - "rustls 0.19.1", + "rustls-pemfile 1.0.0", "schannel", "security-framework", ] @@ -1184,6 +1226,15 @@ dependencies = [ "base64", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7522c9de787ff061458fe9a829dc790a3f5b22dc571694fc5883f448b94d9a9" +dependencies = [ + "base64", +] + [[package]] name = "ryu" version = "1.0.9" @@ -1206,16 +1257,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "sct" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "sct" version = "0.7.0" @@ -1358,15 +1399,21 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.86" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20518fe4a4c9acf048008599e464deb21beeae3d3578418951a189c235a7a9a8" + [[package]] name = "tempfile" version = "3.3.0" @@ -1516,26 +1563,15 @@ dependencies = [ "syn", ] -[[package]] -name = "tokio-rustls" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6" -dependencies = [ - "rustls 0.19.1", - "tokio", - "webpki 0.21.4", -] - [[package]] name = "tokio-rustls" version = "0.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a27d5f2b839802bd8267fa19b0530f5a08b9c08cd417976be2a65d130fe1c11b" dependencies = [ - "rustls 0.20.2", + "rustls", "tokio", - "webpki 0.22.0", + "webpki", ] [[package]] @@ -1563,6 +1599,20 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f988a1a1adc2fb21f9c12aa96441da33a1728193ae0b95d2be22dbd17fcb4e5c" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + [[package]] name = "toml" version = "0.5.8" @@ -1574,12 +1624,13 @@ dependencies = [ [[package]] name = "tonic" -version = "0.6.2" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff08f4649d10a70ffa3522ca559031285d8e421d727ac85c60825761818f5d0a" +checksum = "5be9d60db39854b30b835107500cf0aca0b0d14d6e1c3de124217c23a29c2ddb" dependencies = [ "async-stream", "async-trait", + "axum", "base64", "bytes", "futures-core", @@ -1594,24 +1645,26 @@ dependencies = [ "prost", "prost-derive", "rustls-native-certs", + "rustls-pemfile 1.0.0", "tokio", - "tokio-rustls 0.22.0", + "tokio-rustls", "tokio-stream", - "tokio-util", + "tokio-util 0.7.2", "tower", "tower-layer", "tower-service", "tracing", "tracing-futures", - "webpki-roots 0.21.1", + "webpki-roots", ] [[package]] name = "tonic-build" -version = "0.6.2" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9403f1bafde247186684b230dc6f38b5cd514584e8bec1dd32514be4745fa757" +checksum = "d9263bf4c9bfaae7317c1c2faf7f18491d2fe476f70c414b73bf5d445b00ffa1" dependencies = [ + "prettyplease", "proc-macro2", "prost-build", "quote", @@ -1633,12 +1686,31 @@ dependencies = [ "slab", "tokio", "tokio-stream", - "tokio-util", + "tokio-util 0.6.9", "tower-layer", "tower-service", "tracing", ] +[[package]] +name = "tower-http" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d342c6d58709c0a6d48d48dabbb62d4ef955cf5f0f3bbfd845838e7ae88dbae" +dependencies = [ + "bitflags", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.1" @@ -1750,6 +1822,12 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f" +[[package]] +name = "unicode-ident" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" + [[package]] name = "unicode-normalization" version = "0.1.19" @@ -1759,18 +1837,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" - -[[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - [[package]] name = "untrusted" version = "0.7.1" @@ -1902,16 +1968,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "webpki" -version = "0.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "webpki" version = "0.22.0" @@ -1922,22 +1978,13 @@ dependencies = [ "untrusted", ] -[[package]] -name = "webpki-roots" -version = "0.21.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aabe153544e473b775453675851ecc86863d2a81d786d741f6b76778f2a48940" -dependencies = [ - "webpki 0.21.4", -] - [[package]] name = "webpki-roots" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "552ceb903e957524388c4d3475725ff2c8b7960922063af6ce53c9a43da07449" dependencies = [ - "webpki 0.22.0", + "webpki", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 97ad75a5..4be99615 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ path = "src/main.rs" [package] name = "momento-cli" version = "0.11.9" -edition = "2018" +edition = "2021" [dependencies] colored = "2.0.0" @@ -14,7 +14,7 @@ toml = "0.5.8" maplit = "1.0.2" configparser = "3.0.0" regex = "1" -momento = "0.3.1" +momento = "0.5.0" [dev-dependencies] assert_cmd = "2.0.2" diff --git a/src/commands/cache/cache_cli.rs b/src/commands/cache/cache_cli.rs index 97297969..d971c7cc 100644 --- a/src/commands/cache/cache_cli.rs +++ b/src/commands/cache/cache_cli.rs @@ -1,48 +1,32 @@ use log::debug; -use momento::simple_cache_client::SimpleCacheClient; use std::num::NonZeroU64; use std::process::exit; use crate::error::CliError; - -async fn get_momento_instance(auth_token: String) -> Result { - match SimpleCacheClient::new_momento_cli(auth_token, NonZeroU64::new(100).unwrap()).await { - Ok(m) => Ok(m), - Err(e) => Err(CliError { msg: e.to_string() }), - } -} +use crate::utils::client::{get_momento_client, interact_with_momento}; pub async fn create_cache(cache_name: String, auth_token: String) -> Result<(), CliError> { - debug!("creating cache..."); - let mut momento = get_momento_instance(auth_token).await?; - match momento.create_cache(&cache_name).await { - Ok(_) => (), - Err(e) => return Err(CliError { msg: e.to_string() }), - }; - Ok(()) + let mut client = get_momento_client(auth_token).await?; + + interact_with_momento("creating cache...", client.create_cache(&cache_name)).await } pub async fn delete_cache(cache_name: String, auth_token: String) -> Result<(), CliError> { - debug!("deleting cache..."); - let mut momento = get_momento_instance(auth_token).await?; - match momento.delete_cache(&cache_name).await { - Ok(_) => (), - Err(e) => return Err(CliError { msg: e.to_string() }), - }; - Ok(()) + let mut client = get_momento_client(auth_token).await?; + + interact_with_momento("deleting cache...", client.delete_cache(&cache_name)).await } pub async fn list_caches(auth_token: String) -> Result<(), CliError> { - debug!("list cache called"); - let mut momento = get_momento_instance(auth_token).await?; - match momento.list_caches(None).await { - Ok(res) => { - res.caches - .into_iter() - .for_each(|cache| println!("{}", cache.cache_name)); - } - Err(e) => return Err(CliError { msg: e.to_string() }), - } + let mut client = get_momento_client(auth_token).await?; + + let list_result = interact_with_momento("listing caches...", client.list_caches(None)).await?; + + list_result + .caches + .into_iter() + .for_each(|cache| println!("{}", cache.cache_name)); + Ok(()) } @@ -54,41 +38,38 @@ pub async fn set( ttl_seconds: u64, ) -> Result<(), CliError> { debug!("setting key: {} into cache: {}", key, cache_name); - let mut momento = get_momento_instance(auth_token).await?; - match momento - .set( + let mut client = get_momento_client(auth_token).await?; + + interact_with_momento( + "setting...", + client.set( &cache_name, key, value, Some(NonZeroU64::new(ttl_seconds).unwrap()), - ) - .await - { - Ok(_) => debug!("set success"), - Err(e) => return Err(CliError { msg: e.to_string() }), - }; - Ok(()) + ), + ) + .await + .map(|_| ()) } pub async fn get(cache_name: String, auth_token: String, key: String) -> Result<(), CliError> { debug!("getting key: {} from cache: {}", key, cache_name); - let mut momento = get_momento_instance(auth_token).await?; - match momento.get(&cache_name, key).await { - Ok(r) => { - if matches!( - r.result, - momento::response::cache_get_response::MomentoGetStatus::HIT - ) { - println!("{}", r.as_string()); - } else { - debug!("cache miss"); - exit(1); - } + + let mut client = get_momento_client(auth_token).await?; + + let response = interact_with_momento("getting...", client.get(&cache_name, key)).await?; + match response.result { + momento::response::cache_get_response::MomentoGetStatus::HIT => { + println!("{}", response.as_string()) + } + momento::response::cache_get_response::MomentoGetStatus::MISS => { + debug!("cache miss"); + exit(1) } - Err(e) => { - return Err(CliError { - msg: format!("failed to get from cache: {}", e), - }) + momento::response::cache_get_response::MomentoGetStatus::ERROR => { + debug!("something terrible happened with the wire protocol"); + exit(13) } }; Ok(()) diff --git a/src/commands/signingkey/signingkey_cli.rs b/src/commands/signingkey/signingkey_cli.rs index a17385bf..63d71b6a 100644 --- a/src/commands/signingkey/signingkey_cli.rs +++ b/src/commands/signingkey/signingkey_cli.rs @@ -1,49 +1,39 @@ -use log::debug; -use momento::simple_cache_client::SimpleCacheClient; -use std::num::NonZeroU64; +use crate::{ + error::CliError, + utils::client::{get_momento_client, interact_with_momento, print_whatever_this_is_as_json}, +}; -use crate::error::CliError; +pub async fn create_signing_key(ttl_minutes: u32, auth_token: String) -> Result<(), CliError> { + let mut client = get_momento_client(auth_token).await?; -async fn get_momento_instance(auth_token: String) -> Result { - match SimpleCacheClient::new(auth_token, NonZeroU64::new(100).unwrap()).await { - Ok(m) => Ok(m), - Err(e) => Err(CliError { msg: e.to_string() }), - } -} + print_whatever_this_is_as_json( + &interact_with_momento( + "creating signing key...", + client.create_signing_key(ttl_minutes), + ) + .await?, + ); -pub async fn create_signing_key(ttl_minutes: u32, auth_token: String) -> Result<(), CliError> { - debug!("creating signing key..."); - let mut momento = get_momento_instance(auth_token).await?; - match momento.create_signing_key(ttl_minutes).await { - Ok(res) => { - println!("{}", serde_json::to_string_pretty(&res).unwrap()); - } - Err(e) => return Err(CliError { msg: e.to_string() }), - }; Ok(()) } pub async fn revoke_signing_key(key_id: String, auth_token: String) -> Result<(), CliError> { - debug!("revoking signing key..."); - let mut momento = get_momento_instance(auth_token).await?; - match momento.revoke_signing_key(&key_id).await { - Ok(_) => (), - Err(e) => return Err(CliError { msg: e.to_string() }), - }; - Ok(()) + let mut client = get_momento_client(auth_token).await?; + + interact_with_momento( + "revoking signing key...", + client.revoke_signing_key(&key_id), + ) + .await } pub async fn list_signing_keys(auth_token: String) -> Result<(), CliError> { - debug!("listing signing keys..."); - let mut momento = get_momento_instance(auth_token).await?; - match momento.list_signing_keys(None).await { - Ok(res) => { - println!( - "{}", - serde_json::to_string_pretty(&res.signing_keys).unwrap() - ); - } - Err(e) => return Err(CliError { msg: e.to_string() }), - }; + let mut client = get_momento_client(auth_token).await?; + + print_whatever_this_is_as_json( + &interact_with_momento("listing signing keys...", client.list_signing_keys(None)) + .await + .map(|list_result| list_result.signing_keys)?, + ); Ok(()) } diff --git a/src/utils/client.rs b/src/utils/client.rs new file mode 100644 index 00000000..b38756bf --- /dev/null +++ b/src/utils/client.rs @@ -0,0 +1,49 @@ +use std::{future::Future, num::NonZeroU64}; + +use momento::{ + response::error::MomentoError, + simple_cache_client::{SimpleCacheClient, SimpleCacheClientBuilder}, +}; + +use crate::error::CliError; + +pub async fn get_momento_client(auth_token: String) -> Result { + SimpleCacheClientBuilder::new_with_explicit_agent_name( + auth_token, + NonZeroU64::new(100).unwrap(), + "cli", + ) + .map_or_else( + |error| { + Err(CliError { + msg: error.to_string(), + }) + }, + |builder| Ok(builder.build()), + ) +} + +pub fn print_whatever_this_is_as_json(value: &T) +where + T: serde::Serialize, +{ + println!( + "{}", + serde_json::to_string_pretty(value).expect("Could not print whatever this is as json") + ); +} + +pub async fn interact_with_momento( + debug_note: &str, + momento_interaction: FutureT, +) -> Result +where + FutureT: Future>, +{ + log::debug!("{}", debug_note); + + let result = momento_interaction.await; + result.map_err(|error| CliError { + msg: error.to_string(), + }) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 551e05fc..906105bf 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod client; pub mod file; pub mod ini_config; pub mod user;