Mercurial > mercurial > hgweb_golang.cgi
comparison src/gf/gf.go @ 37:43adde439537
ver.up: gf.go
| author | pyon@macmini | 
|---|---|
| date | Tue, 26 Nov 2019 21:04:22 +0900 | 
| parents | a2e33e922a54 | 
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| 36:a2e33e922a54 | 37:43adde439537 | 
|---|---|
| 1 /* | 1 /* | 
| 2 Last Change: 2019-11-21 Thu 02:11:35. | 2 Last Change: 2019-11-25 月 13:55:58. | 
| 3 | 3 | 
| 4 gf.go: Get Files (ver.0.6) | 4 gf.go: Get Files (ver.0.7) | 
| 5 | 5 | 
| 6 ./gf.json: server & file list | 6 ./gf.json: server & file list | 
| 7 e.g. | 7 e.g. | 
| 8 { | 8 { | 
| 9 "Server": { | 9 "Server": { | 
| 12 }, | 12 }, | 
| 13 "Files": [ | 13 "Files": [ | 
| 14 "mahjong.html", | 14 "mahjong.html", | 
| 15 "gammon", | 15 "gammon", | 
| 16 "gammon/backgammon.html" | 16 "gammon/backgammon.html" | 
| 17 ] | 17 ], | 
| 18 "Backup": false | |
| 18 } | 19 } | 
| 19 | 20 | 
| 20 TODO: | 21 TODO: | 
| 21 + reserve dirtree option [-r] | 22 + reserve dirtree option [-r] | 
| 22 + gunzip/untar option | 23 + gunzip/untar option | 
| 31 "log" | 32 "log" | 
| 32 "os" | 33 "os" | 
| 33 "path" | 34 "path" | 
| 34 "path/filepath" | 35 "path/filepath" | 
| 35 "net/http" | 36 "net/http" | 
| 37 "time" | |
| 36 ) | 38 ) | 
| 37 | 39 | 
| 38 const version = "0.6" | 40 const version = "0.7" | 
| 39 const defaultjson = "gf.json" | 41 const defaultjson = "gf.json" | 
| 40 const samplejson = `{ | 42 const samplejson = `{ | 
| 41 "Server": { | 43 "Server": { | 
| 42 "Address": "hoge.com", | 44 "Address": "hoge.com", | 
| 43 "Port": "80" | 45 "Port": "80" | 
| 44 }, | 46 }, | 
| 45 "Files": [ | 47 "Files": [ | 
| 46 "mahjong.html", | 48 "mahjong.html", | 
| 47 "gammon/backgammon.html" | 49 "gammon/backgammon.html" | 
| 48 ] | 50 ], | 
| 51 "Backup": false | |
| 49 }` | 52 }` | 
| 50 | 53 | 
| 51 type SV struct { | 54 type SV struct { | 
| 52 Address string | 55 Address string | 
| 53 Port string | 56 Port string | 
| 58 } | 61 } | 
| 59 | 62 | 
| 60 type GF struct { | 63 type GF struct { | 
| 61 Server SV | 64 Server SV | 
| 62 Files []string | 65 Files []string | 
| 66 Backup bool | |
| 63 } | 67 } | 
| 64 | 68 | 
| 65 type HO struct { // html object | 69 type HO struct { // html object | 
| 66 Status string | 70 Status string | 
| 67 StatusCode int | 71 StatusCode int | 
| 131 n := len(gf.Files) | 135 n := len(gf.Files) | 
| 132 if n > 1 { | 136 if n > 1 { | 
| 133 print_msg(fmt.Sprintf("%d files downloading", n)) | 137 print_msg(fmt.Sprintf("%d files downloading", n)) | 
| 134 } | 138 } | 
| 135 | 139 | 
| 140 if gf.Backup { | |
| 141 bkdir := "backup" + time.Now().Format("200601021504") | |
| 142 if err := os.Mkdir(bkdir, 0755); err != nil { | |
| 143 log.Fatal(err) | |
| 144 } | |
| 145 for _, f := range gf.Files { | |
| 146 file := path.Base(f) | |
| 147 if _, err := os.Stat(file); os.IsNotExist(err) { | |
| 148 continue | |
| 149 } | |
| 150 if err := os.Link(file, filepath.Join(bkdir, file)); err != nil { | |
| 151 log.Fatal(err) | |
| 152 } | |
| 153 } | |
| 154 files, err := ioutil.ReadDir(bkdir) | |
| 155 if err != nil { | |
| 156 log.Fatal(err) | |
| 157 } | |
| 158 if len(files) == 0 { | |
| 159 os.Remove(bkdir) | |
| 160 } | |
| 161 | |
| 162 } | |
| 163 | |
| 136 ch := make(chan string) | 164 ch := make(chan string) | 
| 137 for _, f := range gf.Files { | 165 for _, f := range gf.Files { | 
| 138 go http_gets(gf.Server.String(), f, ch) | 166 go http_gets(gf.Server.String(), f, ch) | 
| 139 } | 167 } | 
| 140 | 168 | 
