Advertisement
Guest User

Untitled

a guest
Nov 18th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "io/ioutil"
  6. "os"
  7.  
  8. "os/exec"
  9.  
  10. log "github.com/cihub/seelog"
  11. "github.com/jlaffaye/ftp"
  12. )
  13.  
  14. type Config struct {
  15. Host string `json:"host"`
  16. User string `json:"user"`
  17. Pass string `json:"pass"`
  18. File string `json:"file"`
  19. }
  20.  
  21. func initLogger() {
  22. config := `
  23. <seelog type="sync">
  24. <outputs>
  25. <filter levels="trace,debug,info">
  26. <console formatid="ltsv"/>
  27. </filter>
  28. <filter levels="warn,error,critical">
  29. <console formatid="ltsv_error"/>
  30. </filter>
  31. <file formatid="ltsv" path="result.log"/>
  32. </outputs>
  33. <formats>
  34. <format id="ltsv" format="time:%Date(2006-01-02T15:04:05.000Z07:00)%tlev:%l%tmsg:%Msg%n"/>
  35. <format id="ltsv_error"
  36. format="%EscM(31)time:%Date(2006-01-02T15:04:05.000Z07:00)%tlev:%l%tmsg:%Msg%EscM(0)%n"/>
  37. </formats>
  38. </seelog>`
  39.  
  40. logger, err := log.LoggerFromConfigAsBytes([]byte(config))
  41. if err != nil {
  42. panic(err)
  43. }
  44. log.ReplaceLogger(logger)
  45. }
  46.  
  47. func main() {
  48. initLogger()
  49. defer log.Flush()
  50.  
  51. config, err := readConfig()
  52. if err != nil {
  53. panic(err)
  54. }
  55. startRoutine(config)
  56. }
  57.  
  58. func readConfig() (Config, error) {
  59. var config Config
  60. file, err := ioutil.ReadFile("config.json")
  61. if err != nil {
  62. return config, err
  63. }
  64. json.Unmarshal(file, &config)
  65. return config, err
  66. }
  67.  
  68. func startRoutine(config Config) {
  69. // ファイル圧縮
  70. if commpressFiles(config) {
  71. // FTP送信
  72. ftpPut(config)
  73. }
  74. }
  75.  
  76. func commpressFiles(config Config) bool {
  77. p, _ := os.Getwd()
  78. command := "\"C:\\Program Files (x86)\\Lhaplus\\Lhaplus.exe\" /c:lzh /o:.\\" + config.File + " " + p + `\files\a.txt`
  79. log.Infof("command=%s", command)
  80. _, err := exec.Command(command).Output()
  81. if err != nil {
  82. log.Info(err)
  83. return false
  84. }
  85. return true
  86. }
  87.  
  88. func ftpPut(config Config) {
  89. log.Infof("Connect %s", config.Host)
  90. client, err := ftp.Connect(config.Host)
  91. if err != nil {
  92. log.Info(err)
  93. return
  94. }
  95. defer client.Quit()
  96.  
  97. log.Infof("Login user=%s pass=%s", config.User, config.Pass)
  98. err = client.Login(config.User, config.Pass)
  99. if err != nil {
  100. log.Info(err)
  101. return
  102. }
  103.  
  104. log.Infof("open file=%s ", config.File)
  105. file, err := os.Open(config.File)
  106. if err != nil {
  107. log.Info(err)
  108. return
  109. }
  110.  
  111. log.Info("put!!!")
  112. err = client.Stor(config.File, file)
  113. if err != nil {
  114. log.Info(err)
  115. return
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement