$30 off During Our Annual Pro Sale. View Details »

sendmailのラッパーをGo言語で作ってみた話

 sendmailのラッパーをGo言語で作ってみた話

Hiromi Kimura

December 15, 2016
Tweet

More Decks by Hiromi Kimura

Other Decks in Technology

Transcript

  1. Πϯετʔϧ logfile: /var/log/sendmail.log $ wget -O /usr/local/bin/sendmail-logger https:// github.com/kimromi/sendmail-logger/releases/download/ v0.0.2/sendmail-logger_linux_amd64

    $ chmod 755 /usr/local/bin/sendmail-logger ઃఆϑΝΠϧFUDTFOENBJMMPHHFSDPOpHZBNM ྫ $FOU04CJU όΠφϦΛ(JUIVCʹஔ͍͍ͯΔͷͰ 04ʹ͋ͬͨ΋ͷΛμ΢ϯϩʔυ͢Δ͚ͩ
  2. ࣮ߦϩάग़ྗ w MPH(Pඪ४ͷMPHϥΠϒϥϦ package main import ( "log" "os" )

    func init() { f, _ := os.OpenFile("out.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) log.SetOutput(f) } func main() { log.Print("loglog") } ¥ 3FBEBOE8SJUF ϑΝΠϧͳ͔ͬͨΒ࡞Δ ຤ඌʹςΩετ௥Ճ
  3. ඪ४ೖྗಡΈࠐΈ w CVpP(Pඪ४ͷ*0όοϑΝϦϯά package main import ( "bufio" "fmt" "os"

    ) func main() { body := "" scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { body += scanner.Text() + "\n" } if err := scanner.Err(); err != nil { fmt.Print(err) } } ෳ਺ߦͷೖྗ͕͋Δͱ͖͸ GPSͰશߦऔಘ͢Δ
  4. ZBNMϑΝΠϧಡΈࠐΈ w HPZBNMZBNMZBNMಡΈࠐΈϥΠϒϥϦ IUUQTHJUIVCDPNHPZBNMZBNM package main import ( "fmt" "io/ioutil"

    yaml "gopkg.in/yaml.v2" ) type Config struct { LogFile string `yaml:"logfile"` } func main() { file, _ := ioutil.ReadFile("/etc/sendmail-logger/config.yaml") conf := Config{} yaml.Unmarshal([]byte(file), &conf) fmt.Print(conf) } ZBNMϑΝΠϧͰͷઃఆ໊Λ TUSVDUͰఆ͓ٛͯ͘͠
  5. ϝʔϧϩάϑΝΠϧग़ྗ package main import ( "fmt" "os" ) func main()

    { file, err := os.OpenFile("mail.log", os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0664) if err != nil { fmt.Print(err) } defer file.Close() file.Write(([]byte)("write")) } w PT(Pඪ४ͷ04ػೳ΁ͷΠϯλʔϑΣʔεύοέʔδ 8SJUF0OMZ EFGFSͰ࣮ߦ׬ྃ࣌ʹ ϑΝΠϧΫϩʔζ͢Δ
  6. TFOENBJMίϚϯυ࣮ߦ w PTFYFD(Pඪ४ͷ֎෦ίϚϯυ࣮ߦύοέʔδ package main import ( "fmt" "os/exec" )

    func main() { out, err := exec.Command("ls", "-la").Output() if err != nil { fmt.Print(err) } fmt.Print(string(out)) } ίϚϯυ࣮ߦͭͭ͠ ඪ४ग़ྗΛม਺ʹऔΓࠐΉ
  7. HPY w ΫϩείϯύΠϧ༻ͷύοέʔδ $ go get github.com/mitchellh/gox $ gox Number

    of parallel builds: 3 --> linux/arm: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger --> darwin/386: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger --> linux/386: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger --> netbsd/arm: _/Users/PMAC243S/repos/github.com/kimromi/sendmail-logger ɾɾɾ IUUQTHJUIVCDPNNJUDIFMMIHPY ίϚϯυൃͰ ֤04༻ͷόΠφϦΛ࡞੒ HPHFUͰΠϯετʔϧ
  8. HIS w HJUIVCDPNʹWFSTJPOUBH෇͖ͰϦϦʔε IUUQTHJUIVCDPNUDOLTNHIS $ go get github.com/tcnksm/ghr $ gox

    -output "pkg/{{.Dir}}_{{.OS}}_{{.Arch}}" $ ghr v0.0.2 pkg/ WFSTJPOUBHΛ෇༩ͯ͠ϦϦʔε όΠφϦμ΢ϯϩʔυͰ͖Δ HPHFUͰΠϯετʔϧ