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

Go CLI Hands-on

taxio
June 22, 2019

Go CLI Hands-on

taxio

June 22, 2019
Tweet

More Decks by taxio

Other Decks in Programming

Transcript

  1. (PͰ$-*πʔϧΛ࡞Δʂ

  2. ௕ஸ৔ʹͳΓ·͕͢
 Θ͍Θ͍΍͍͖ͬͯ·͠ΐ͏ʂ

  3. w ࠓ೔͸$-*πʔϧΛ࡞Δ࿩Λ͢ΔͷͰɼͦΕҎ֎ͷ(Pͷ࿩͸ͳΔ΂͘ආ͚ ·͢ɽ ຊ࣭ͱ͸཭Εͨίʔυ͕૿͑ΔͷͰ  w దٓɼ༨ஊͱ֤ͯ͠ϓϥΫςΟεͷิ଍Λ͠·͢ɽ ͓͜ͱΘΓ 3

  4. w ͳͥ(PͰ$-*πʔϧΛ࡞Δͷ͔ w ࠓ೔࡞Δπʔϧͷ࢓༷೺Ѳ w ඪ४ϥΠϒϥϦͰ࡞ͬͯΈΔ w Πϯετʔϧͯ͠ΈΔ w ੩తϑΝΠϧΛόΠφϦʹຒΊࠐΉ

    w 4DBMBCMFͳ࣮૷ʹ͢Δ 4DIFEVMF 4
  5. ͳͥ(PͰ$-*πʔϧΛ࡞Δͷ͔

  6. w HIR QFDP (PͰ࡞ΒΕ͍ͯΔπʔϧͨͪ 6

  7. w จ๏͕γϯϓϧ w ίϯύΠϧݴޠ w γϯάϧόΠφϦ w ΫϩείϯύΠϧՄೳ w ίϛϡχςΟ͕׆ൃ

    (PͷϝϦοτ 7
  8. w γεςϚνοΫʹݎ࿚ͳαʔϏεΛ૊Ήͷ͸ۤख ‣ ͦ͏͍͏ͷ͸3VTUͱ͔ؔ਺ܕݴޠͷ࢓ࣄ ‣ υϝΠϯϩδοΫʹूதͰ͖ͯߴ଎ʹαʔϏεΛ੒௕ͤ͞ΒΕΔͷ͕ڧΈ͔ͩΒ (Pͷۤखͳ͜ͱ 8

  9. w IUUQTCFODINBSLTHBNFUFBNQBHFTEFCJBOOFUCFODINBSLTHBNF GBTUFTUHPHQQIUNM w (P͸ίϯύΠϧݴޠ͚ͩͲ($͕Φʔόʔϔουʹͳͬͯগ͠஗͍ ‣ ͦͷ୅ΘΓϝϞϦ҆શ w ͔͠͠։ൃ଎౓ͱ࣮ߦ଎౓͕ͪΐ͏Ͳ͍͍ ଎౓͸

    9
  10. ααοͱ࡞Γ͍ͨ࣌ʹ
 બ୒͞ΕΔͷ͕(PMBOH

  11. ࠓ೔࡞Δπʔϧͷ঺հ

  12. w EBJMZSFQP ‣ ೔ใͷ਽ܗ࡞੒πʔϧ ‣ BEEαϒίϚϯυͰςϯϓϨʔτʹैͬͨϨϙʔτ࡞੒ - dailyrepo add --name

    [name] w ࠓճ͸งғؾΛ௫ΉͨΊʹػೳ͸࠷௿ݶ ೔ใ਽ܗ࡞੒πʔϧ 12
  13. ࣮૷͍ͯ͘͠લʹ
 ͱ͜ΖͰօ͞Μ

  14. งғؾͰ$-*πʔϧ
 ࡞͍ͬͯ·ͤΜ͔ʁ

  15. ࣮͸ͦΕɼϚφʔҧ൓͔΋

  16. w Ҿ਺͸ͲͷΑ͏ʹઃܭ͢Δ΂͖ʁ w ϑΝΠϧͷѻ͍͸Ͳ͏͢Δ΂͖ʁ w &YJU4UBUVT͸ԿΛฦ͢΂͖ʁ w ࠔͬͨͱ͖ʹಡΉEFFFFU͞Μͷૉ੖Β͍͠ࢿྉ ‣ IUUQTTQFBLFSEFDLDPNUDOLTNLPNBOEPSBJOUVSVOJUVJUFZVSVUPLJOJQV

    GBMTFZVSVLPUPOVNCFSZBQDBTJB ‣ PQUJPOTͱBSHVNFOUTͷҐஔؔ܎͕ೖΕସΘͬͯΔͷ͸Α͘ݟΔ ࣮૷͢Δͱ͖ʹߟ͑Δ͜ͱ 16
  17. 104*9(/6TUZMF

  18. w IUUQTQVCTPQFOHSPVQPSHPOMJOFQVCT w IUUQTXXXHOVPSHTPGUXBSFMJCDNBOVBM[email protected]MJCDIUNM w 6OJYΛ࢝Ίͱ֤ͨ͠छ04ؒͰҠ২͠΍͍͢ΞϓϦέʔγϣϯΛ
 ࡞ΔͨΊͷࠃࡍن֨ w ͍ͭ͜ΒͷৼΔ෣͍Λਅࣅ͓͚ͯ͠͹จ۟͸ݴΘΕͳͦ͞͏ 104*9(/6TUZMF

    18
  19. w "MMQSPHSBNTTIPVMETVQQPSU ‣ --version ‣ --help w MPOHPQUJPO͸Λ࢖͏ w TIPSUPQUJPO͸Λ࢖͏

    ‣ ϑϥάͷ৔߹·ͱΊΔ͜ͱ͕Ͱ͖ΔΑ͏ʹ  FHls -al ‣ Ҿ਺ΛऔΔ৔߹͸ɼؒʹεϖʔεΛ࢖ͬͯ΋࢖Θͳͯ͘΋ྑ͍  FHmysql -uroot w IUUQTXXXHOVPSHTPGUXBSFMJCDNBOVBM[email protected]"SHVNFOU4ZOUBYIUNM 'MBH0QUJPOT 19
  20. w جຊతʹ͕੒ޭͰ͕ࣦഊ w "EWBODFE#BTI4DSJQUJOH(VJEFʹࡌ͍ͬͯΔن֨ ‣ IUUQUMEQPSH-%1BCTIUNMFYJUDPEFTIUNM ‣ Ͳ͏ͤTIͰ࣮ߦ͢Δͱ͖͸CJOCBTIΛࢦఆ͢Δ͜ͱ͕ଟ͍͠ w 'SFF#4%ϥΠϒϥϦؔ਺ϚχϡΞϧ

    ‣ IUUQTLBXPSVKQOPSHEPD'SFF#4%KNBONBOTZTFYJUTQIQ w ͋Μ·ΓϋϯυϦϯάͯ͠ΔΞϓϦέʔγϣϯݟͨ͜ͱແ͍͚Ͳ &YJU4UBUVT 20
  21. w 9%(#BTF%JSFDUPSZ ‣ IUUQTTUBOEBSETGSFFEFTLUPQPSHCBTFEJSTQFDMBUFTUJOEFYIUNM ‣ πʔϧͷઃఆϑΝΠϧ΍ΩϟογϡΛͲ͜ʹஔ͔͘Λఆٛͨ͠࢓༷ॻ  )0.&DPOpHͱ͔ w Ұ࣌తͳϑΝΠϧ͸UNQʹஔ͘͜ͱ΋ଟ͍

    6OJYɾ-JOVYܥ ϑΝΠϧͷѻ͍ 21
  22. – @mygwdesign lσβΠϯ͸ར༻͢Δ΋ͷɻΞʔτ͸ؑ৆͢Δ΋ͷɻz

  23. $-*ΛσβΠϯ͠·͠ΐ͏

  24. Ͱ͸͍࣮͟૷ʂ

  25. w (JU)VCʹࠓ೔ͷϦϙδτϦΛ࡞͍ͬͯͩ͘͞ ‣ ࡞੒ͨ͠πʔϧΛ(JU)VC͔Β௚઀Πϯετʔϧ͢Δͱ͖ʹ࢖͍·͢ w ࠓ೔ͷ׬੒ܗ ‣ IUUQTHJUIVCDPNUBYJPHPDMJUVUPSJBM ‣ ໎ͬͨΒ͜͜Λࢀߟʹίϐϖ͍ͯͩ͘͠͞

    w ϋϯζΦϯͳͷͰ෼͔Βͳ͍৔߹͸ͲΜͲΜखΛ্͍͛ͯͩ͘͞ ‣ ๻͚ͩͰ͸ख͕·ΘΒͳ͍͜ͱ΋͋ΔͷͰɼ
 ྡͷਓ͕ࠔ͍ͬͯΔ৔߹͸ڭ͍͑ͯ͋͛ͯͩ͘͞ ࡞͍ͬͯ͘ʹ͋ͨͬͯ 25
  26. ඪ४ϥΠϒϥϦͰ࣮૷ͯ͠ΈΔ

  27. w qBH ‣ IUUQTHPMBOHPSHQLHqBH ‣ ίϚϯυϥΠϯҾ਺Ͱ౉͞ΕΔϑϥάͷύʔεपΓΛ؅ཧͯ͘͠ΕΔ ‣ ͱͯ΋ബ͍ϥΠϒϥϦͳͷͰͥͻιʔεΛݺΜͰงғؾΛ௫ΜͰΈ·͠ΐ͏ ϥΠϒϥϦ঺հ 27

  28. w ׬੒ܗIUUQTHJUIVCDPNUBYJPHPDMJUVUPSJBMCMPCNBTUFSDNE EBJMZSFQPNBJOHP ‣ TUBUJLύοέʔδΛ࢖ͬͯΔͷ͸Ұ୴ແࢹͯ͠ɼJPVUJM3FBE'JMF Λ࢖͍ͬͯͩ͘͞ w σϑΥϧτͷqBHΦϒδΣΫτΛ࢖͏ͱFSSPS)BOEMJOHʹ&YJU0O&SSPS͕
 ઃఆ͞Ε͍ͯΔͷͰɼIFMQग़ྗ࣌ʹFYJUTUBUVTͰऴྃͯ͠͠·͏ ‣

    AIA AIFMQAΛࣗ෼Ͱఆٛ͢Ε͹໰୊ͳ͍ ࣮૷͢Δ 28
  29. -JWF$PEJOH

  30. w MPOHPQUJPO͸Ͱ͖·͢ ‣ IUUQNBUPQFIBUFOBCMPHDPNFOUSZ ‣ εϚʔτͰ͸ͳ͍ w TIPSUPQUJPO͸·ͱΊΕͳͦ͞͏ ‣ ஌Βͳ͍͚ͩͰɼ΋͔ͨ͠͠ΒͰ͖Δ͔΋

    w ͦ΋ͦ΋(Pࣗମ͕ͦ͏͍͏$-*Λఏڙ͍ͯ͠ͳ͍ qBHͰ͸104*9(/6TUZMFͳ$-*͸࡞Εͳ͍ʁ 30
  31. Πϯετʔϧͯ͠ΈΔ

  32. w go install͢Δͱ(01"5)CJOҎԼʹόΠφϦ͕ஔ͔ΕΔ w NBJOύοέʔδͷ͋ΔϦϞʔτϦϙδτϦΛgo getͯ͠΋0, ‣ HPHFUHJUIVCDPNUBYJPHPDMJIBOETPODNEEBJMZSFQP ‣ IUUQTHPMBOHPSHDNEHPIES

    %[email protected]@[email protected]@[email protected] w όΠφϦ໊͸ʁ ‣ NBJOύοέʔδͷ͋ΔσΟϨΫτϦ໊ )PXUPJOTUBMM 32
  33. w (PͰ͸खܰʹΫϩείϯύΠϧͰ͖Δͷ͕ڧΈͰͨ͠Ͷ ‣ GOOS={{OS໊}} GOARCH={{ΞʔΩςΫνϟ໊}} go build ‣ (PΠϯετʔϧ࣌ʹଞ؀ڥͷґଘ΋ೖΕ͓͚ͯ͹͜Ε͚ͩͰΫϩείϯύΠϧՄೳ ͷ

    ͸ͣ  ‣ $(0ΛແޮԽ͓͚ͯ͠͹ɼTUBUJDMJOL΋Ͱ͖Δ CGO_ENABLED=0  ‣ ରԠ؀ڥIUUQTHPMBOHPSHEPDJOTUBMMTPVSDFFOWJSPONFOU w $*ͰόΠφϦੜ੒ͯ͠ɼ(JU)VCͷ3FMFBTFʹࡌͤΔͷ͕Α͋͘Δӡ༻ํ๏ ‣ (P3FMFBTFSΛ࢖͏ͱָ ‣ IUUQTHJUIVCDPNHPSFMFBTFSHPSFMFBTFS ‣ IUUQTHPSFMFBTFSDPNDJ )PXUPDSPTTDPNQJMF 33
  34. σΟϨΫτϦߏ੒ʹ͍ͭͯ 34 /Project
 main.go # package main
 lib/
 dailyrepo.go #

    package dailyrepo w ެ։͍ͨ͠ϓϩδΣΫτ͕πʔϧͳͷ͔ϥΠϒϥϦͳͷ͔ʹΑͬͯɼ
 σΟϨΫτϦߏ੒͸େ͖͘มΘΔɽ ‣ ࢀߟΈΜͳͷ(Pݴޠ ࠓ౓վగ൛͕ग़ΔΒ͍͠Ͱ͢  w Ϣʔβ͕go getͨ࣌͠ͷҙਤ͠ͳ͍JOTUBMMΛճආͤ͞Δ /Project
 dailyrepo.go # package dailyrepo
 cmd/
 dailyrepo/
 main.go # package main πʔϧ όΠφϦ ͕ϝΠϯͷ੒Ռ෺ͷ৔߹ ϥΠϒϥϦ͕ϝΠϯͷ੒Ռ෺ͷ৔߹
  35. ੩తϑΝΠϧΛຒΊࠐΉ

  36. w (PͷڧΈͷ̍ͭʹγϯάϧόΠφϦͱ͍͏ͷ͕͋ͬͨ ‣ ੩తϑΝΠϧʹґଘ͢Δπʔϧͩͬͨ৔߹ɼͦͷϑΝΠϧ΋Πϯετʔϧ࣌ʹ
 μ΢ϯϩʔυͤ͞Δͷʁ ‣ όΠφϦʹຒΊࠐΜ͡Ό͑ w SE1BSUZͷϥΠϒϥϦΛ࢖࣮ͬͯ૷͢Δ ‣

    KUFFVXFOHPCJOEBUBͱ͍͏ͷ͕͕͋ͬͨࠓ͸%FQSFDBUFE  ͱ͍͏͔࡞ऀ͕(JU)VCΞΧ΢ϯτΛফͨ͠ ‣ "XFTPNF(PΛݟͯ୳ͦ͏  IUUQTBXFTPNFHPDPNSFTPVSDFFNCFEEJOH ຒΊࠐΉ 36
  37. ҰԠެࣜͬΆ͍΍Γํʁ 37 $ go build -ldflags "-X main.Version=v0.0.1" w SE1BSUZʹཔΒͳͯ͘΋όΠφϦʹ੩తϑΝΠϧ͸ຒΊࠐΊΔ

    w #VJME࣌ʹม਺ʹ஋ΛຒΊࠐΉ͜ͱ͕Ͱ͖Δ ‣ (JUͷ৘ใΛऔͬͯ7FSTJPOΛຒΊࠐΉͷ͸Α͘ݟΔ w ͍΍Ͱ΋͜ΕͰϑΝΠϧͷ಺༰ΛຒΊࠐΉͷ͸ਏ͍
  38. w IUUQTHJUIVCDPNSBLZMMTUBUJL w ੩తϑΝΠϧͷ[JQσʔλ όΠτྻ Λ(PίʔυͷதʹϋʔυίʔσΟϯά ͍ͯ͠Δɽ ‣ *TUIJTBDSB[ZJEFB /P

    OPUOFDFTTBSZ SBLZMMTUBUJL 38
  39. TUBUJLͰຒΊࠐΜͰΈΔ 39 # install
 $ go get github.com/rakyll/statik # ϑΝΠϧ࡞੒


    $ statik --src=templates/ w ͖ͬ͞ͷςϯϓϨʔτϑΝΠϧಡΈࠐΈΛࠩ͠ସ͑·͠ΐ͏ ‣ IUUQTHJUIVCDPNUBYJPHPDMJUVUPSJBMCMPCNBTUFSDNEEBJMZSFQPNBJOHP
  40. TUBUJLͰຒΊࠐΜͰΈΔ 40 bptl, err := ioutil.ReadFile("../../templates/report.md.tmpl") statikFs, err := fs.New()

    if err != nil { log.Fatal(err) } tplFile, err := statikFs.Open("/report.md.tmpl") if err != nil { log.Fatal(err) } defer tplFile.Close() btpl, err := ioutil.ReadAll(tplFile)
  41. -JWF$PEJOH

  42. ͜͜ͰҰ୴׬੒ʂ

  43. Ͱ΋ػೳ͕૿͖͑ͯͨΒʁ ͜ͷϑϥά؅ཧͰεέʔϧͰ͖Δ

  44. 4DBMBCMFͳ࣮૷ʹม͑Δ

  45. w ϑϥάͷύʔαʔΛ΋ͬͱݡ͍΋ͷʹ͢Δ ‣ ྲྀੴʹTIPSU MPOHPQUJPO͸ཉ͍͠ ‣ TIPSUPQUJPOΛ·ͱΊ͍ͨ w ςετ͠΍͍͢Α͏ʹ͢Δ ‣

    ϑΝΠϧγεςϜૢ࡞ͩͬͯςετ͍ͨ͠ ‣ ඪ४ग़ྗͩͬͯςετ͍ͨ͠ ํ਑ 45
  46. None
  47. w ͜ͷਓͷੈք؍ʹै͑͹େମඞཁͳػೳ͸ἧ͏ w QqBH ‣ 104*9(/6TUZMF४ڌͳqBHΑΓͪΐͬͱݡ͍ϑϥάύʔαʔ w DPCSB ‣ QqBHΛϕʔεʹ࡞ΒΕͨ$-*ϑϨʔϜϫʔΫ

    w BGFSP ‣ ϑΝΠϧγεςϜͷந৅ԽϥΠϒϥϦ w WJQFS ‣ ઃఆϑΝΠϧ؅ཧϥΠϒϥϦ TQGͱ͍͏εΰ͍ਓ 47
  48. ·ͣDPCSBͰ࣮૷͍ͯ͘͠ 48 # install
 $ go get github.com/spf13/cobra # init


    $ cobra init dailyrepo --pkg-name github.com/{Ϣʔβʔ໊}/{ϦϙδτϦ໊}/cmd/dailyrepo # create `add` sub command
 $ cobra add add w ؤு͖ͬͯͬ͞ͷ࣮૷Λ࠶ݱ ‣ IUUQTHJUIVCDPNUBYJPHPDMJUVUPSJBMUSFFNBTUFSDNEEBJMZSFQPFY
  49. -JWF$PEJOH

  50. w IUUQTHJUIVCDPNBWFMJOPBXFTPNFHPTUBOEBSEDMJ w Α͘ฉ͘΍ͭ ‣ KFTTFWELHPqBHT ‣ BMFDUIPNBTLJOHQJO ‣ VSGBDFDMJ

    ‣ NJUDIFMMIDMJ QqBH DPCSBҎ֎ͷબ୒ࢶ 50
  51. TQGBGFSP 51 # install
 $ go get github.com/spf13/afero w IUUQTHJUIVCDPNTQGBGFSP

    w ϑΝΠϧγεςϜͷૢ࡞Λந৅Խͯ͘͠ΕΔϥΠϒϥϦ ‣ BGFSP'T0T04ଆͷϑΝΠϧγεςϜ ‣ BGFSP.FN.BQ'TϝϞϦ্ʹߏங͞ΕͨϑΝΠϧγεςϜ  ςετ࣌ʹ࢖͏
  52. -JWF$PEJOH

  53. w GNU1SJOUMO ͸PT4UEPVUʹରͯ͠GNU'QSJOUMO ͍ͯ͠Δɽ ‣ PT4UEPVUΛCZUFT#V⒎FSʹࠩ͠ସ͑ΕΔΑ͏ʹ͓ͯ͘͠ w ࠓճ͸WFSTJPOͷQSJOUΛςετͯ͠Έ·͢ ඪ४ग़ྗΛςετ͢Δ 53

  54. -JWF$PEJOH

  55. ׬੒ʂ

  56. w HJUIVCDPNTQGWJQFS ‣ ίϚϯυϥΠϯϑϥάɼ؀ڥม਺ɼઃఆϑΝΠϧ KTPO UPNM ZBNM FUD ɼ
 ϦϞʔτઃఆγεςϜ

    FUDE FUD ɼσϑΥϧτ஋͔Βઃఆ஋Λऔಘ͢Δɽ w os.UserHomeDir, os.UserCacheDir, 
 os.UserConfigDir ͜Ε͸·ͩϦϦʔε͞Εͯͳ͍ Λ࢖ͬͯ
 ઃఆϑΝΠϧΛݟ͚ͭΑ͏ ༨ஊઃఆϑΝΠϧͷ࿩ 56
  57. w ࠷ۙͷπʔϧ͸݁ߏ(PͰॻ͔Ε͍ͯΔ w (Pͩͱ࡞੒࣌΋࡞੒ޙ΋࢖͍΍͍͢$-*πʔϧΛ࡞Δ͜ͱ͕Ͱ͖Δ w டংͷແ͍$-*͸Ξʔτ ·ͱΊ 57

  58. w MPHHJOH ‣ cobra.OnInitialize()ͰWFSCPTFͷਅِ஋ΛݟͯMPHͷઃఆΛม͑ͨΓ͢Δ w ΞʔΩςΫνϟ ‣ جຊతʹࠓ೔ͷϑΝΠϧγεςϜ΍ඪ४ग़ྗͷந৅ԽͷΑ͏ͳΠϝʔδͰ
 ͦΕͧΕͷ੹຿Λશ͏͢ΔϞδϡʔϧΛந৅Խ͍ͯ͘͠ ‣

    (PͰ"1*αʔόʔΛ࣮૷͢Δͱ͖͸ϨΠϠʔυΞʔΩςΫνϟ͕Α͘࠾༻͞Ε·͢ w ΤϥʔϋϯυϦϯά ‣ (PͰYFSSPST͕ඪ४ͷFSSPSTʹ૊Έࠐ·ΕΔΒ͍͠Ͱ͢  IUUQTHPHPPHMFTPVSDFDPNQSPQPTBM NBTUFSEFTJHOFSSPSWBMVFTNE ‣ ϋϯυϦϯάʹ͍ͭͯIUUQTNFEJVNDPNZBLLBCFUUFSHPFSSPSIBOEMJOHXJUI YFSSPSTFD ࠓ೔࿩ͯ͠ͳ͍಺༰ 58
  59. w HP[(Pʹؔ͢Δχϡʔε·ͱΊ ‣ IUUQHP[IFYBDPTBOFU w HPXJLJ৭ʑͳϓϥΫςΟε͕·ͱ·͍ͬͯΔ ‣ IUUQTHJUIVCDPNHPMBOHHPXJLJ w 5IF(P#MPHެࣜΞφ΢ϯεͱ͔

    ‣ IUUQTCMPHHPMBOHPSH ༨ஊ(PΛֶͼ͍ͨͳΒ 59
  60. Ҏ্ʂ ͓ർΕ༷Ͱͨ͠ʂ