Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "io/ioutil"
- "os"
- "os/exec"
- log "github.com/cihub/seelog"
- "github.com/jlaffaye/ftp"
- )
- type Config struct {
- Host string `json:"host"`
- User string `json:"user"`
- Pass string `json:"pass"`
- File string `json:"file"`
- }
- func initLogger() {
- config := `
- <seelog type="sync">
- <outputs>
- <filter levels="trace,debug,info">
- <console formatid="ltsv"/>
- </filter>
- <filter levels="warn,error,critical">
- <console formatid="ltsv_error"/>
- </filter>
- <file formatid="ltsv" path="result.log"/>
- </outputs>
- <formats>
- <format id="ltsv" format="time:%Date(2006-01-02T15:04:05.000Z07:00)%tlev:%l%tmsg:%Msg%n"/>
- <format id="ltsv_error"
- format="%EscM(31)time:%Date(2006-01-02T15:04:05.000Z07:00)%tlev:%l%tmsg:%Msg%EscM(0)%n"/>
- </formats>
- </seelog>`
- logger, err := log.LoggerFromConfigAsBytes([]byte(config))
- if err != nil {
- panic(err)
- }
- log.ReplaceLogger(logger)
- }
- func main() {
- initLogger()
- defer log.Flush()
- config, err := readConfig()
- if err != nil {
- panic(err)
- }
- startRoutine(config)
- }
- func readConfig() (Config, error) {
- var config Config
- file, err := ioutil.ReadFile("config.json")
- if err != nil {
- return config, err
- }
- json.Unmarshal(file, &config)
- return config, err
- }
- func startRoutine(config Config) {
- // ファイル圧縮
- if commpressFiles(config) {
- // FTP送信
- ftpPut(config)
- }
- }
- func commpressFiles(config Config) bool {
- p, _ := os.Getwd()
- command := "\"C:\\Program Files (x86)\\Lhaplus\\Lhaplus.exe\" /c:lzh /o:.\\" + config.File + " " + p + `\files\a.txt`
- log.Infof("command=%s", command)
- _, err := exec.Command(command).Output()
- if err != nil {
- log.Info(err)
- return false
- }
- return true
- }
- func ftpPut(config Config) {
- log.Infof("Connect %s", config.Host)
- client, err := ftp.Connect(config.Host)
- if err != nil {
- log.Info(err)
- return
- }
- defer client.Quit()
- log.Infof("Login user=%s pass=%s", config.User, config.Pass)
- err = client.Login(config.User, config.Pass)
- if err != nil {
- log.Info(err)
- return
- }
- log.Infof("open file=%s ", config.File)
- file, err := os.Open(config.File)
- if err != nil {
- log.Info(err)
- return
- }
- log.Info("put!!!")
- err = client.Stor(config.File, file)
- if err != nil {
- log.Info(err)
- return
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement