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. w "MMQSPHSBNTTIPVMETVQQPSU ‣ --version ‣ --help w MPOHPQUJPO͸Λ࢖͏ w TIPSUPQUJPO͸Λ࢖͏

    ‣ ϑϥάͷ৔߹·ͱΊΔ͜ͱ͕Ͱ͖ΔΑ͏ʹ  FHls -al ‣ Ҿ਺ΛऔΔ৔߹͸ɼؒʹεϖʔεΛ࢖ͬͯ΋࢖Θͳͯ͘΋ྑ͍  FHmysql -uroot w IUUQTXXXHOVPSHTPGUXBSFMJCDNBOVBMIUNM@OPEF"SHVNFOU4ZOUBYIUNM 'MBH0QUJPOT 19
  2. w (JU)VCʹࠓ೔ͷϦϙδτϦΛ࡞͍ͬͯͩ͘͞ ‣ ࡞੒ͨ͠πʔϧΛ(JU)VC͔Β௚઀Πϯετʔϧ͢Δͱ͖ʹ࢖͍·͢ w ࠓ೔ͷ׬੒ܗ ‣ IUUQTHJUIVCDPNUBYJPHPDMJUVUPSJBM ‣ ໎ͬͨΒ͜͜Λࢀߟʹίϐϖ͍ͯͩ͘͠͞

    w ϋϯζΦϯͳͷͰ෼͔Βͳ͍৔߹͸ͲΜͲΜखΛ্͍͛ͯͩ͘͞ ‣ ๻͚ͩͰ͸ख͕·ΘΒͳ͍͜ͱ΋͋ΔͷͰɼ
 ྡͷਓ͕ࠔ͍ͬͯΔ৔߹͸ڭ͍͑ͯ͋͛ͯͩ͘͞ ࡞͍ͬͯ͘ʹ͋ͨͬͯ 25
  3. w (PͰ͸खܰʹΫϩείϯύΠϧͰ͖Δͷ͕ڧΈͰͨ͠Ͷ ‣ GOOS={{OS໊}} GOARCH={{ΞʔΩςΫνϟ໊}} go build ‣ (PΠϯετʔϧ࣌ʹଞ؀ڥͷґଘ΋ೖΕ͓͚ͯ͹͜Ε͚ͩͰΫϩείϯύΠϧՄೳ ͷ

    ͸ͣ  ‣ $(0ΛແޮԽ͓͚ͯ͠͹ɼTUBUJDMJOL΋Ͱ͖Δ CGO_ENABLED=0  ‣ ରԠ؀ڥIUUQTHPMBOHPSHEPDJOTUBMMTPVSDFFOWJSPONFOU w $*ͰόΠφϦੜ੒ͯ͠ɼ(JU)VCͷ3FMFBTFʹࡌͤΔͷ͕Α͋͘Δӡ༻ํ๏ ‣ (P3FMFBTFSΛ࢖͏ͱָ ‣ IUUQTHJUIVCDPNHPSFMFBTFSHPSFMFBTFS ‣ IUUQTHPSFMFBTFSDPNDJ )PXUPDSPTTDPNQJMF 33
  4. σΟϨΫτϦߏ੒ʹ͍ͭͯ 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 πʔϧ όΠφϦ ͕ϝΠϯͷ੒Ռ෺ͷ৔߹ ϥΠϒϥϦ͕ϝΠϯͷ੒Ռ෺ͷ৔߹
  5. w (PͷڧΈͷ̍ͭʹγϯάϧόΠφϦͱ͍͏ͷ͕͋ͬͨ ‣ ੩తϑΝΠϧʹґଘ͢Δπʔϧͩͬͨ৔߹ɼͦͷϑΝΠϧ΋Πϯετʔϧ࣌ʹ
 μ΢ϯϩʔυͤ͞Δͷʁ ‣ όΠφϦʹຒΊࠐΜ͡Ό͑ w SE1BSUZͷϥΠϒϥϦΛ࢖࣮ͬͯ૷͢Δ ‣

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

    w #VJME࣌ʹม਺ʹ஋ΛຒΊࠐΉ͜ͱ͕Ͱ͖Δ ‣ (JUͷ৘ใΛऔͬͯ7FSTJPOΛຒΊࠐΉͷ͸Α͘ݟΔ w ͍΍Ͱ΋͜ΕͰϑΝΠϧͷ಺༰ΛຒΊࠐΉͷ͸ਏ͍
  7. TUBUJLͰຒΊࠐΜͰΈΔ 39 # install
 $ go get github.com/rakyll/statik # ϑΝΠϧ࡞੒


    $ statik --src=templates/ w ͖ͬ͞ͷςϯϓϨʔτϑΝΠϧಡΈࠐΈΛࠩ͠ସ͑·͠ΐ͏ ‣ IUUQTHJUIVCDPNUBYJPHPDMJUVUPSJBMCMPCNBTUFSDNEEBJMZSFQPNBJOHP
  8. 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)
  9. w ͜ͷਓͷੈք؍ʹै͑͹େମඞཁͳػೳ͸ἧ͏ w QqBH ‣ 104*9(/6TUZMF४ڌͳqBHΑΓͪΐͬͱݡ͍ϑϥάύʔαʔ w DPCSB ‣ QqBHΛϕʔεʹ࡞ΒΕͨ$-*ϑϨʔϜϫʔΫ

    w BGFSP ‣ ϑΝΠϧγεςϜͷந৅ԽϥΠϒϥϦ w WJQFS ‣ ઃఆϑΝΠϧ؅ཧϥΠϒϥϦ TQGͱ͍͏εΰ͍ਓ 47
  10. ·ͣ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
  11. TQGBGFSP 51 # install
 $ go get github.com/spf13/afero w IUUQTHJUIVCDPNTQGBGFSP

    w ϑΝΠϧγεςϜͷૢ࡞Λந৅Խͯ͘͠ΕΔϥΠϒϥϦ ‣ BGFSP'T0T04ଆͷϑΝΠϧγεςϜ ‣ BGFSP.FN.BQ'TϝϞϦ্ʹߏங͞ΕͨϑΝΠϧγεςϜ  ςετ࣌ʹ࢖͏
  12. w HJUIVCDPNTQGWJQFS ‣ ίϚϯυϥΠϯϑϥάɼ؀ڥม਺ɼઃఆϑΝΠϧ KTPO UPNM ZBNM FUD ɼ
 ϦϞʔτઃఆγεςϜ

    FUDE FUD ɼσϑΥϧτ஋͔Βઃఆ஋Λऔಘ͢Δɽ w os.UserHomeDir, os.UserCacheDir, 
 os.UserConfigDir ͜Ε͸·ͩϦϦʔε͞Εͯͳ͍ Λ࢖ͬͯ
 ઃఆϑΝΠϧΛݟ͚ͭΑ͏ ༨ஊઃఆϑΝΠϧͷ࿩ 56
  13. w MPHHJOH ‣ cobra.OnInitialize()ͰWFSCPTFͷਅِ஋ΛݟͯMPHͷઃఆΛม͑ͨΓ͢Δ w ΞʔΩςΫνϟ ‣ جຊతʹࠓ೔ͷϑΝΠϧγεςϜ΍ඪ४ग़ྗͷந৅ԽͷΑ͏ͳΠϝʔδͰ
 ͦΕͧΕͷ੹຿Λશ͏͢ΔϞδϡʔϧΛந৅Խ͍ͯ͘͠ ‣

    (PͰ"1*αʔόʔΛ࣮૷͢Δͱ͖͸ϨΠϠʔυΞʔΩςΫνϟ͕Α͘࠾༻͞Ε·͢ w ΤϥʔϋϯυϦϯά ‣ (PͰYFSSPST͕ඪ४ͷFSSPSTʹ૊Έࠐ·ΕΔΒ͍͠Ͱ͢  IUUQTHPHPPHMFTPVSDFDPNQSPQPTBM NBTUFSEFTJHOFSSPSWBMVFTNE ‣ ϋϯυϦϯάʹ͍ͭͯIUUQTNFEJVNDPNZBLLBCFUUFSHPFSSPSIBOEMJOHXJUI YFSSPSTFD ࠓ೔࿩ͯ͠ͳ͍಺༰ 58