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

Command-line interface tool design / PHPerKaigi...

Command-line interface tool design / PHPerKaigi 2024

Ken’ichiro Oyama

March 08, 2024
Tweet

More Decks by Ken’ichiro Oyama

Other Decks in Technology

Transcript

  1. 4 ࠷ۙྗΛೖΕ͍ͯΔOSSͷ঺հ runnʢϥϯΤψʣ • runn ( means "Run N". is

    pronounced /rʌ́n én/. ) is a package/tool for running operations following a scenario. • https://github.com/k1LoW/runn • γφϦΦΛYAMLͰॻ͍ͯͦΕΛ΋ͱʹૢ࡞ʢΦϖϨʔγϣϯʣΛࣗಈԽͰ͖Δύο έʔδ/πʔϧ • ओʹAPIςεςΟϯάπʔϧͱͯ͠࠾༻͞Ε͍ͯΔ • νϡʔτϦΞϧ΍ΫοΫϒοΫ΋͋Δ
  2. ΞδΣϯμ • Command-line interface • Command-line interface tool design •

    ίϚϯυମܥͷσβΠϯ • έʔεελσΟ • πʔϧͱͯ͠ͷ࣮૷ͷσβΠϯ • έʔεελσΟ • ͓ΘΓʹ 5
  3. 7 Command-line interface Command-line interfaceͱ͸ • Ϣʔβ͕จࣈྻʢίϚϯυʣΛೖྗͯ͠ϓϩάϥϜʹ໋ྩ͢ΔϢʔβΠϯλʔϑΣʔε • Α͘ରͱͳΔͷ͕GUIʢGraphical user

    interfaceʣ • ༨ஊ: ʮCUIʢCharacter user interfaceʣʯΛ࢖͏ͷ͸೔ຊޠͰͷ࢖༻͕ओྲྀΒ͍͠ʢWikipediaௐ΂ʣ
  4. 8 Command-line interface ίϚϯυϥΠϯΠϯλϓϦλ • Ϣʔβ͔Βೖྗ͞ΕΔจࣈྻͷߦͷίϚϯυΛղऍ͠ɺOS΍ϓϩάϥϛϯάݴޠॲཧܥͳͲʹ ౉͢΋ͷ • ίϚϯυϥΠϯΠϯλϓϦλ͸࣍ͷ4ͭΛॱʹߦ͏ʢREPLʣɻ 1.

    ೖྗͷಡΈऔΓ…Read 2. ೖྗจࣈʢίϚϯυʣΛ࣮ߦʢධՁʣ...Eval 3. ࣮ߦ݁ՌΛදࣔ…Print 4. ೖྗͷಡΈऔΓʹ໭Δ…Loop • Bash΍Bourne Shellͱ͍ͬͨUNIXγΣϧ΍RubyͷirbɺPHPͷ`php -a`ͳͲ
  5. 9 Command-line interface CLIπʔϧʢCommand-line interface toolʣ • ຊൃදͰ͸ίϚϯυΛೖྗ͢Δ͜ͱͰUNIXγΣϧ͔Β࣮ߦ͞ΕΔϓϩάϥϜΛCLIπʔϧͱݺͿ • lsͳͲɻcomposer΋ͦ͏ɻ

    • ʢ࣮͸cd΍pwd͸shell built-in commandͳͷͰҧ͏͜ͱͳΔʣ • ίϚϯυϥΠϯΠϯλϓϦλΛ௨࣮ͯ͡ߦ͞Εग़ྗΛฦ͢ϓϩάϥϜ = CLIπʔϧ • CLIπʔϧ͸࣮ߦ࣌ʹ Ҿ਺ʢargsʣ΍ ϑϥάʢflagsʣɺඪ४ೖྗʢSTDINʣͳͲΛೖྗͱͯ͠ ड͚औΔʢਖ਼֬ʹ͸Ҿ਺ͱϑϥάͷ۠ผ͸CLIπʔϧ಺Ͱߦ͏ʣ • CLIπʔϧ͸ʮCommand-line interfaceΛ࣋ͭπʔϧʯ
  6. 11 Command-line interface tool design CLIπʔϧͷσβΠϯ 2ͭͷଆ໘͔ΒͷCommand-line interface tool design

    1. ίϚϯυମܥͷσβΠϯ … interface design • ͲͷΑ͏ͳίϚϯυମܥΛϢʔβʹఏڙ͢Δͷ͔ 2. πʔϧͱͯ͠ͷ࣮૷ͷσβΠϯ … tool design • ͲͷΑ͏ͳΞʔΩςΫνϟʹ͢Δͷ͔
  7. 13 ίϚϯυମܥͷσβΠϯ CLIπʔϧ͕࣋ͭϢʔβΠϯλʔϑΣʔεʹٻΊΒΕΔ͜ͱ • WebΞϓϦέʔγϣϯͱରൺͯ͠ΈΔͱɺWebΞϓϦέʔγϣϯʹ͓͚ΔϢʔβΠϯλʔϑΣʔε ͸େ͖͘2ͭ 1. ϒϥ΢βʢ͔ΒݟΔHTMLϖʔδʣ 2. Web

    API • GUIͱϒϥ΢βɺCLIͱWebAPI͕ରൺ͞ΕΔΑ͏ʹΈ͑Δ͕ͦ͏Ͱ͸ͳ͍ 1. ϒϥ΢β = ਓΛϢʔβͱͨ͠ϢʔεέʔεʢͱϢʔβΠϯλʔϑΣʔεʣ 2. Web API = ػցΛϢʔβͱͨ͠ϢʔεέʔεʢͱϢʔβΠϯλʔϑΣʔεʣ • CLIπʔϧ΋WebΞϓϦέʔγϣϯͱಉ༷ʹਓͱػցͷ2ͭͷϢʔεέʔε͕͋Γɺ
 1ͭͷϢʔβΠϯλʔϑΣʔεͰ2ͭͷϢʔεέʔεΛಉ࣌ʹຬͨ͢ඞཁ͕͋Δ
  8. 17 ίϚϯυମܥͷσβΠϯ ʮ1ͭͷCLIπʔϧʯͱ͍͏ϨΠϠʔ • ίϚϯυೖྗͱ͍͏୯७ͳΠϯλʔϑΣΠεʹ࣮͸ଟ͘ͷػೳΛؚΊΔ͜ͱ͕Ͱ͖Δ • αϒίϚϯυ • ྫ͑͹ɺaws-cliͱ͍͏CLIπʔϧ͸୯ҰͷϢʔβΠϯλʔϑΣʔεͰ༷ʑͳAWSϦιʔεͷૢ࡞ ͕Ͱ͖Δ

    • ϒϥ΢β্ʢAWS Management Console্ʣͰ͸ෳ਺ͷϖʔδΛߦ͖དྷ͢Δඞཁ͕͋Δ • ͜Ε͚ͩͰ΋े෼ϝϦοτʹͳΔ • ҰํͰଟ͘ͷػೳΛ1ͭͷCLIπʔϧͰૢ࡞͢Δඞཁ͕͋Δ • େલఏͱͯ͠CLIπʔϧͷதͰ౷Ұ͞ΕͨίϚϯυମܥͰ͋Δ͜ͱ͕ٻΊΒΕΔ
  9. 18 ίϚϯυମܥͷσβΠϯ ʮUNIXγΣϧ্ʯͱ͍͏ϨΠϠʔ • UNIXγΣϧ্ͷCLIπʔϧ͸࣮ߦํ๏ͱೖग़ྗ͕౷Ұ͞Ε͍ͯΔ • fork, execve • STDIN,

    STDOUT, STDOUTʢExit code, args, flagsʣ • ύΠϓͱ͍͏ίϚϯυΛ૊Έ߹ΘͤΔͨΊͷڧྗͳػೳ • UNIXγΣϧ্ͰධՁ͞ΕΔϓϩάϥϜʢ·ͨ͸ผͷϓϩάϥϜʹΑͬͯධՁ͞ΕΔϓϩάϥ Ϝʣ͸ඃ࣮ߦ΋࣮ߦ΋͜ͷ୯ҰͷΠϯλʔϑΣʔεʹରԠ͢Ε͹ྑ͍ • ݁Ռͱͯ͠࿈ܞ΍ࣗಈԽ͕༰қʹͳΔɻͭ·Γ૊Έ߹ΘͤΔ͜ͱ͕͋Δ • ૊Έ߹ΘͤΛߟྀͨ͠ίϚϯυମܥͰ͋Ε͹͋Δ΄Ͳ࢖͍΍͍͢
  10. 19 ίϚϯυମܥͷσβΠϯ UNIX఩ֶ • ʢ਺͋ΔʣUNIX఩ֶʢͷ͏ͪͷ1ͭʣ͔ΒCLIπʔϧͷσβΠϯʹؔ܎͢ΔՕॴΛڧௐ • μάϥεɾϚΩϧϩΠଞόʔδϣϯʢ1978೥ Bell System Technical

    Journalʣ 1. ͦΕͧΕͷϓϩάϥϜ͕1ͭͷ͜ͱΛ͏·͘͜ͳ͢Α͏ʹɻ৽͍͠࢓ࣄΛ͢ΔͨΊʹɺ৽͍͠ʮػೳʯ Λ௥Ճͯ͠ݹ͍ϓϩάϥϜΛෳࡶʹ͢ΔͷͰ͸ͳ͘ɺ৽͍͠ϓϩάϥϜΛߏங͢Δɻ 2. ͢΂ͯͷϓϩάϥϜͷग़ྗ͕ɺ·ͩݟ͵ผͷϓϩάϥϜͷೖྗʹͳΔ͜ͱɻग़ྗʹ༨ܭͳ৘ใΛೖΕͳ ͍Α͏ʹɻݫີʹྻڍ͞Εͨೖྗܗࣜ΍όΠφϦܗࣜ͸ආ͚ΔɻΠϯλϥΫςΟϒͳೖྗʹͩ͜ΘΒͳ ͍ 3. ιϑτ΢ΣΞ͸΋ͪΖΜOSͰ͋ͬͯ΋ɺૣظʹɺཧ૝ͱͯ͠͸਺िؒҎ಺ʹࢼͤΔΑ͏ઃܭɾߏங͢Δɻෆث༻ͳ෦෼͸᪳᪯ͳࣺͯͯ͘࡞Γ௚ ͢Α͏ʹ 4. ճΓಓΛͯ͠΋ɺ࢖͍ऴΘͬͨޙʹࣺͯΔ͜ͱʹͳͬͯ΋ɺϓϩάϥϛϯάͷ࡞ۀΛܰݮ͢ΔͨΊʹ͸ɺ͓ख఻͍͞ΜͰ͸ͳ͘πʔϧΛ࢖͏ https://ja.wikipedia.org/wiki/UNIX఩ֶ ΑΓҾ༻
  11. 21 ίϚϯυମܥͷσβΠϯ ʮೖྗ͠΍͍͢ʯͱ͸ • ૝૾͠΍͍͢ɻ֮͑΍͍͢ • ౷Ұ͞Εͨϑϥά΍αϒίϚϯυͷମܥ • ϑϥά΍αϒίϚϯυ͸໊ࢺͳͷ͔ಈࢺͳͷ͔ɺओޠ͸Կͳͷ͔ •

    ଞͷΑ͘஌ΒΕͨCLIπʔϧͱಉ͡Α͏ͳίϚϯυମܥ • ʮৄࡉग़ྗ͸ `-v (--verbose)` ʯͳͲ • ίϚϯυ͸૊Έ߹ΘͤΔ͜ͱ͕૝ఆ͞ΕΔͨΊʮࣗ෼ͷίϚϯυମܥʯ͚ͩΛߟ͍͑ͯͯ͸࢖͍ ʹ͘͘ͳΔ • ೖྗ͢Δจࣈ͕୯७ʹগͳ͍ • ֮͑Εͯ͠·͑͹࣮ߦͰ͖Δɻͦ͏͢Δͱසൟʹ࣮ߦ͢ΔίϚϯυͷλΠϓ਺͸গͳ͍ํ͕ྑ͍ ʢγϣʔτΧοτͱಉ͡ʣ • γϣʔτϑϥάͷఏڙ
  12. 22 ίϚϯυମܥͷσβΠϯ ʮཧղ͠΍͍͢ʯͱ͸ • ࣮ߦ݁Ռ͕Θ͔Γ΍͍͢ • ࣮ߦ݁Ռͷग़ྗ͕ɺಡΜͰҙຯΛཧղͰ͖Δ • ࣮ߦ݁Ռͷग़ྗ͕ɺಡ·ͳͯ͘΋ݟͨ໨ͰཧղͰ͖Δ •

    ৭ʢ੒ޭͳΒ྘ɺࣦഊͳΒ੺ʣ • ࣮ߦ͢ΔίϚϯυ͕ԿΛ࣮ߦ͢Δͷ͔͕Θ͔Γ΍͍͢ • ಡΜͰҙຯ͕Θ͔ΔίϚϯυମܥɻϩϯάϑϥάͷఏڙ
  13. 23 ίϚϯυମܥͷσβΠϯ ػցΛϢʔβͱͨ͠Ϣʔεέʔε • ίϚϯυͱͯ͠ొ࿥Ͱ͖Ε͹͍͍ɻͲΜͳίϚϯυମܥͰ΋Ұ౓ొ࿥ͯ͠͠·͑͹ͳΜͰ΋OK • CRON΍Consul health checksͳͲ •

    Ұ౓ొ࿥ͯ͠͠·͑͹OKͳͷ͔ͩΒΉ͠Ζೖྗ͸͠ʹ͘͘ͱ΋Մಡੑͷߴ͍ίϚϯυ͕هड़Ͱ ͖Δํ͕ྑ͍ • ొ࿥ͨ͠ίϚϯυɺޙͰݟͯͦͷҙຯ͸Θ͔Γ·͔͢ʁ͍֮͑ͯ·͔͢ʁ • ػցʹΘ͔Γ΍͍࣮͢ߦ݁Ռ • ऴྃεςʔλεʢExit codeʣ • STDOUTɺSTDERRͷ࢖͍෼͚
  14. 26 έʔεελσΟ ʮௐࠪ࣌ʹҰ࣌తʹ࢖༻͢ΔίϚϯυͳͷͰ֮͑ΒΕͳ͍ʯ • ࣮ߦ͢ΔͱόοΫάϥ΢ϯυϓϩηε্ཱ͕͕ͪΓࢦఆظ͚ؒͩαʔόͷϝτϦΫεΛ؂ࢹ͠ɺ ࣄલʹઃఆͨ͠৚݅ʹୡͨ࣌͠ʹ೚ҙͷίϚϯυͷ࣮ߦ΍Slack௨஌Λߦ͏CLIπʔϧ • ʮࠓ౓CPUར༻཰͕80%Λ௒͑ͨ࣌ʹtopίϚϯυͷ݁ՌΛͱΓ͍ͨʯͳͲ • ෳࡶͳ৚݅ઃఆ͕Ͱ͖Δ͕ɺௐࠪ࣌ʹҰ࣌తʹ࢖༻͢ΔίϚϯυͳͷͰͦ΋ͦ΋ίϚϯυࣗମ֮

    ͑ΒΕͳ͍ • sheer-heart-attack launch ͚ͩΛ֮͑ͯ΋Β͍ɺΠϯλϥΫςΟϒϞʔυʹͨ͠ • ೖྗ׬ྃޙʹ sheer-heart-attack launch ͷϓϩηε͔Βೖྗͨ͠஋Λflagsʹม׵ͨ͠͏͑Ͱ sheer-heart-attack task ʢϝΠϯͷ؂ࢹίϚϯυʣΛόοΫάϥ΢ϯυ࣮ߦ sheer-heart-attack https://github.com/k1LoW/sheer-heart-attack
  15. 28 έʔεελσΟ ʮͨͩͨͩϩάʹ৭෇͚Λ͍ͨ͠ʯ • ϩάͷ͏ͪಛఆͷΩʔϫʔυ͚ͩ৭෇͚Λ͢Δπʔϧ • ʮ৭Λ͚͍ͭͨʯͷ໨త͸໨ཱ͍ͨͤͨ • ͦΕ͚ͩɻ৭Λࢦఆ͍ͨ͠Θ͚Ͱ͸ͳ͍ •

    ໨ཱ͍ͨͤͨͱߟ͍͑ͯΔͷ͸ਓɺػցͰ͸ͳ͍ • tail -F /var/log/nginx/access.log | colr POST GET 404 500 search • argsʹΩʔϫʔυΛฒ΂ͯ΋Β͏͚ͩͷίϚϯυମܥʹͨ͠ • ϩάͳͲͷೖྗ͸ඪ४ೖྗܦ༝ʹݶఆ͢Δ͜ͱͰίϚϯυମܥΛγϯϓϧʹ • ৭ͷࢦఆػೳ΋ͳ͠ colr https://github.com/k1LoW/colr
  16. 29 έʔεελσΟ ʮجຊ͸CIʹ૊ΈࠐΉ͜ͱ͕ϝΠϯɻ͔͠͠ϩʔΧϧͰ΋ผͷϢʔεέʔε͕͋Δʯ • CI্ͰίʔυΧόϨοδ͚ͩͰͳ͘CIʢGitHub ActionsʣͷAPIΛୟ͍ͯςετ࣮ߦ࣌ؒͷܭଌ ΍ΧόϨοδϨϙʔτͷૹ৴ͳͲΛߦ͏πʔϧ • CI্ͷ؀ڥม਺ͱઃఆϑΝΠϧͰڍಈΛܾఆ͢Δ •

    ҰํͰʮίʔυΧόϨοδΛ֬ೝ͍ͨ͠ʯ͸ϩʔΧϧ্ͷϢʔεέʔεͰ΋͋Δɻ • ಉ͡ octocov ίϚϯυͰ΋ɺ؀ڥม਺ʹΑͬͯڍಈΛมߋ͢ΔΑ͏ʹ͠ɺCI্ͰͷॲཧΛޡͬ ࣮ͯߦ͠ͳ͍Α͏ʹ͍ͯ͠Δɻ • ػցͷϢʔεέʔεΛϝΠϯͱͭͭ͠΋ਓͷϢʔεέʔεͷ৔߹Λߟྀͨ͠ύλʔϯ octocov https://github.com/k1LoW/octocov
  17. 31 πʔϧͱͯ͠ͷ࣮૷ͷσβΠϯ CLIπʔϧͱWebΞϓϦέʔγϣϯͱͷൺֱ • WebΞϓϦέʔγϣϯͱରൺͯ͠ΈΔͱɺ • WebΞϓϦέʔγϣϯ • ೖग़ྗ͸HTTPϦΫΤετ-Ϩεϙϯε •

    ػೳΛ࣮ݱ͢ΔͨΊʹσʔλϕʔε΍ϑΝΠϧ΁ͷಡΈॻ͖΍֎෦APIͱͷ௨৴ͳͲ͕ߦΘΕ Δɻ • CLIπʔϧ • ೖྗ͸ίϚϯυ/STDINɺग़ྗ͸STDOUT/STDERRʢͱExit codeʣ • ػೳΛ࣮ݱ͢ΔͨΊʹσʔλϕʔε΍ϑΝΠϧ΁ͷಡΈॻ͖΍֎෦APIͱͷ௨৴ͳͲ͕ߦΘΕ Δɻ • ࣮͸ͱͯ΋͍ۙɻWebΞϓϦέʔγϣϯͷϓϥΫςΟεΛͦͷ··࢖͏͜ͱ͕Ͱ͖Δ͜ͱ͕΄ͱΜͲɻ
  18. 32 πʔϧͱͯ͠ͷ࣮૷ͷσβΠϯ WebΞϓϦέʔγϣϯͱҟͳΔಛੑ • ࣮ߦ৔ॴ͕ෆಛఆଟ਺ • ʮಛఆͷαʔό্ʯͱ͍͏Θ͚Ͱ͸ͳ͍ʢʮͦΕͧΕͷϩʔΧϧʯͳͲʣ • 1ճͷ࣮ߦ͕׬ྃ͢ΔͱCLIπʔϧͷϓϩηε͸ऴྃ͢Δ •

    ΩϟογϡͳͲͷҰ࣌৘ใͷอ࣋Λ͢Δ৔߹ɺΠϯϝϞϦΛબ୒Ͱ͖ͳ͍ • ෆಛఆଟ਺ͷϢʔβ͔ΒͷೖྗΛಉ࣌ʹड͚෇͚Δ͜ͱ͸ͳ͍ • ͦͷ࢓༷͕ඞཁͳ৔߹ɺͦΕ͸΋͏CLIπʔϧͰ͸ͳ͍΄͏͕ྑͦ͞͏ • Web-DBͷΑ͏ͳӨڹ͕ดͨ͡ߏ੒ʹͳΔ͜ͱ͸গͳ͘ɺ֎෦ͷཁૉ΁࡞༻͢ΔػೳΛ࣋ͭ͜ͱ ͕ଟ͍
  19. 33 πʔϧͱͯ͠ͷ࣮૷ͷσβΠϯ CLIπʔϧͷΞʔΩςΫνϟ • ೖग़ྗ͸ܾ·͓ͬͯΓɺ࣮૷͢΂͖ϏδωεϩδοΫ΋ఆٛͰ͖Δ఺͸WebΞϓϦέʔγϣϯͱಉ ͡ɻWebΞϓϦέʔγϣϯͰ࢖͍ͬͯΔΞʔΩςΫνϟύλʔϯ΍ݪଇ͸CLIπʔϧͰ΋༗ޮ • ҰํͰɺCLIπʔϧ͸ • UNIX఩ֶΑΓʮ1ͭͷ͜ͱΛ͏·͘΍ΔʯΑ͏ʹ࣮૷͞Ε


    Δ͜ͱ͕ଟ͍ͨΊEntities΍Use Cases͸খ͘͞ͳΓ͕ͪ • Presenters/UI͸͍ͭ΋
 ίϚϯυ/STDIN/STDOUT/STDERR/Exit codeͰࣗ໌ • ֎෦ͷཁૉ΁࡞༻͢ΔػೳΛ࣋ͭ͜ͱ͕ଟ͍ͨΊGateways
 ʹଟ༷ੑ͕ੜ·Ε΍͍͢
  20. 36 έʔεελσΟ ʮ͍Ζ͍Ζͳσʔλϕʔεͷ৘ใΛऔಘ͍ͨ͠ʯ • σʔλϕʔεʹ઀ଓͯ͠σʔλϕʔεεΩʔϚυΩϡϝϯτΛੜ੒͢Δπʔϧ • ෳ਺ͷछྨͷσʔλϕʔεΛαϙʔτ • ͲͷछྨͷσʔλϕʔεͰ΋ಉ͡Α͏ͳεΩʔϚυΩϡϝϯτ
 Λੜ੒͢Δඞཁ͕͋Δ

    • Gateways͕େ׆༂ • WebΞϓϦέʔγϣϯͷٞ࿦ʹ͋Γ͕ͪͳ
 ʮDBΛஔ͖׵͑ΔͳΜͯΞϓϦέʔγϣϯͷण໋͔Β
 Έͯ΋ଘࡏ͠ͳ͍ʯͱ͍͏͜ͱʹͳΒͳ͍ɻΉ͠Ζ૿͑Δ tbls https://github.com/k1LoW/tbls