Skip to content

Instantly share code, notes, and snippets.

@zaz600
Created June 15, 2015 03:25
Show Gist options
  • Save zaz600/5afbc6bb3a0f4e8d9bc1 to your computer and use it in GitHub Desktop.
Save zaz600/5afbc6bb3a0f4e8d9bc1 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"time"
)
type Config struct {
Src_dir string `json:"src_dir"`
Dst_dir string `json:"dst_dir"`
Timeout int `json:"timeout"`
IfExists string `json:"ifexists"`
User string `json:"user"`
Password string `json:"password"`
}
type Global_config struct {
Dirs []Config `json:"dirs"`
}
func readconfig() (x *Global_config, err error) {
var file []byte
if file, err = ioutil.ReadFile("gotosser.json"); err != nil {
return nil, err
}
x = new(Global_config)
if err = json.Unmarshal(file, &x); err != nil {
return nil, err
}
return x, nil
}
func ScanLocalDir(c Config) {
// Сканирует локальный каталог или сетевой диск
// Бесконечный цикл
for {
log.Printf("Обрабатываем входящий каталог %s\n", c.Src_dir)
items, err := ioutil.ReadDir(c.Src_dir)
if err == nil {
for _, item := range items {
// обрабатываем только файлы. Не каталоги, символические ссылки и т.п.
if item.Mode().IsRegular() {
src_file := item.Name()
log.Println(src_file)
}
}
} else {
log.Println(err)
}
time.Sleep(time.Duration(c.Timeout) * time.Second)
}
}
func main() {
cfg, err := readconfig()
if err != nil {
log.Println("readconfig:", err)
os.Exit(-1)
}
for _, c := range cfg.Dirs {
// Если каталог-источник не FTP
if !strings.HasPrefix(c.Src_dir, "ftp://") {
// запускаем в отдельном потоке бесконечный цикл сканирования каталога
go ScanLocalDir(c)
}
}
// бесконечный цикл в главном потоке, чтобы программа не завершалась
for {
time.Sleep(60 * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment