Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rust tutorial - implement a cli tool.

Yoshihito
September 13, 2019

Rust tutorial - implement a cli tool.

Yoshihito

September 13, 2019
Tweet

More Decks by Yoshihito

Other Decks in Technology

Transcript

  1. About me • Ϋϥεϝιουגࣜձࣾ (Developers.IOͷͱ͜) • ෱ԬΦϑΟεॴଐ • CXࣄۀຊ෦෦ •

    αʔόʔαΠυΤϯδχΞ • ΍ͬͯΔ͜ͱ • AWSͷΠϯϑϥߏங • ϞόΠϧΞϓϦؔ࿈ͷαʔό։ൃ • ෼ੳ༻σʔλͷऩू/Ճ޻ • IoT (NEW) 2
  2. ͳͥRust͔ • ΤίγεςϜ͕ॆ࣮ • cargo • rustup • crates.io •

    ڧྗͳܕγεςϜ • ϝϞϦ҆શ • ॻָ͍͍ͯͯ͠ 6
  3. Ͳ͏΍ֶͬͯͿ͔ • ๛෋ͳΦϯϥΠϯڭࡐΛಡΈړΔ • TRPL (h)ps:/ /doc.rust-lang.org/book/) • Rust by

    Example (h)ps:/ /doc.rust-lang.org/rust-by-example/) • ࣮ࡍʹॻ͍ͯΈΔ • ΦϯϥΠϯδϟοδܥ • πʔϧ࡞੒ 7
  4. UNIXλΠϜελϯϓ (ݱ࣮) • ௨ৗ͸ΤϙοΫ͔Βͷܦաඵ਺Λ࢖͏ • DynamoDBͷTTLͱ͔ • ΤϙοΫ͔ΒͷܦաϛϦඵ਺Λ࢖͏͜ͱ΋͋Δ • CloudWatch

    Logsͷݕࡧظؒͷࢦఆ (--start-9me΍--end-9me) • ΞΫηεස౓͕ߴ͍APIͷ࣮ߦཤྺͳͲ • Ϣʔβ୯ҐͰه࿥͢Δ৔߹ • ඵ਺୯ҐͰه࿥͢Δͱ࣮ߦॱংΛࣝผͰ͖ͳ͍ 10
  5. UNIXλΠϜελϯϓม׵ͷྫ 3೔લʙ౰೔ͷޕલ0࣌·Ͱʹൃੜͨ͠ΤϥʔΛର৅ʹݕࡧ͢Δ৔߹ ݕࡧ։࢝೔࣌ʙऴྃ೔࣌ΛܾΊΔ (୯Ґ:ϛϦඵ) START_TS=$(python -c 'from datetime import *;

    print(int((datetime.today() + timedelta(days=-3)).timestamp()) * 1000)') STOP_TS=$(python -c 'from datetime import *; print(int((datetime.today() + timedelta(days=0)).timestamp()) * 1000)') AWS CLIʹݕࡧύλʔϯ/ݕࡧൣғΛࢦఆ͢Δ aws logs filter-log-events \ --filter-patten 'ERROR' \ --start-time "${START_TS}" \ --end-time "${STOP_TS}" ... 11
  6. UNIXλΠϜελϯϓͷม׵ํ๏ • dateίϚϯυΛ࢖͏ • LinuxͱmacOSͰίϚϯυͷॻ͕ࣜҧͬͯࠞཚ͢Δ → ࢖Θͳ͘ͳͬͨ • LLݴޠΛ࢖͏ •

    ຖճಉ͡Α͏ͳϫϯϥΠφʔॻ͘ → ΊΜͲ͍ • WEBαʔϏεΛ࢖͏ • ϒϥ΢β։͍ͯίϐϖ → ΊΜͲ͍ → πʔϧ࡞੒ͷ୊ࡐʹͪΐ͏Ͳྑ͍ʂ 12
  7. πʔϧͷར༻Πϝʔδ 3೔લʙࡢ೔·ͰͷϩάΛݕࡧ͢Δ৔߹ • લ४උͳ͠ • ίϚϯυ࣮ߦ aws logs filter-log-events \

    --filter-patten 'ERROR' \ --start-time $(my-cli --base today --delta -3day) \ --end-time $(my-cli --base today) ... 13
  8. ॲཧ֓ཁͱϥΠϒϥϦͷରԠ • ίϚϯυϥΠϯҾ਺ղੳ → clap-rs • ೔࣌σʔλੜ੒ → chrono •

    ೔࣌ͷࠩ෼Λద༻ (/medeltaతͳ) → ֘౰ͳ͠ • ݁Ռग़ྗ → ඪ४ग़ྗɺϥΠϒϥϦෆཁ 16
  9. ೔࣌σʔλੜ੒ (chrono) h"ps:/ /github.com/chronotope/chrono READMEΑΓҾ༻ use chrono::prelude::*; let utc: DateTime<Utc>

    = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z` let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00` use chrono::prelude::*; use chrono::offset::LocalResult; let dt = Utc.ymd(2014, 7, 8).and_hms(9, 10, 11); // `2014-07-08T09:10:11Z` // July 8 is 188th day of the year 2014 (`o` for "ordinal") assert_eq!(dt, Utc.yo(2014, 189).and_hms(9, 10, 11)); // July 8 is Tuesday in ISO week 28 of the year 2014. assert_eq!(dt, Utc.isoywd(2014, 28, Weekday::Tue).and_hms(9, 10, 11)); 18
  10. enumͷػೳ֦ு (strum) h"ps:/ /github.com/Peternator7/strum READMEΑΓҾ༻ // You need to bring

    the type into scope to use it!!! use strum::IntoEnumIterator; #[derive(EnumIter,Debug)] enum Color { Red, Green { range:usize }, Blue(usize), Yellow, } // It's simple to iterate over the variants of an enum. fn debug_colors() { for color in Color::iter() { println!("My favorite color is {:?}", color); } } 19
  11. ࣮૷ͯ͠Έͨ h"ps:/ /github.com/yoshihitoh/ut-cli $ tokei ------------------------------------------------------------------------------- Language Files Lines Code

    Comments Blanks ------------------------------------------------------------------------------- BASH 2 43 30 2 11 Markdown 1 177 177 0 0 Rust 25 2757 2312 35 410 TOML 1 26 24 0 2 ------------------------------------------------------------------------------- Total 29 3003 2543 37 423 ------------------------------------------------------------------------------- 20
  12. ࢼͯ͠ΈΔ ೔࣌දه → UNIXλΠϜελϯϓ 3೔લʙ౰೔·Ͱ $ ut generate --base today

    --delta -3day 1567954800 $ ut generate --base today --delta -0day 1568214000 ࢦఆͨ͠೔࣌ $ ut generate --ymd 20190913 --hms 123456 1568345696 21
  13. ࢼͯ͠ΈΔ UNIXλΠϜελϯϓ → ೔࣌දه $ ut parse $(ut generate --ymd

    20190913 --hms 123456) 2019-09-13 12:34:56 (+09:00) # ଞͷίϚϯυͱൺֱ $ date -r $(ut generate --ymd 20190913 --hms 123456) 2019೥ 9݄13೔ ༵ۚ೔ 12࣌34෼56ඵ JST 22
  14. ଞͷίϚϯυϥΠϯͱ࿈ಈͤ͞Δ aws logs filter-log-events \ --filter-pa1en 'ERROR' \ --start-6me $(ut

    -p ms generate --base today --delta -3day) \ --end-6me $(ut -p ms generate --base today) ... 23