-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.go
88 lines (80 loc) · 2.32 KB
/
cmdline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package methuforecasttimelapse
import (
"flag"
_ "image/png"
"io"
"log"
"os"
"path"
"time"
)
const (
LOG_FILE_NAME = "log.txt"
CHECKING_PERIOD = 1 * time.Hour
//CHECKING_PERIOD = 10 * time.Second
)
func setupLogger() *os.File {
logFile, err := os.OpenFile(LOG_FILE_NAME, os.O_CREATE|os.O_APPEND, 0666)
if err == nil {
multiWriter := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(multiWriter)
return logFile
} else {
log.Println(err)
return nil
}
}
func main() {
//logFile := setupLogger()
//defer logFile.Close()
log.Println(" --------- Started")
doCheckStructure := flag.Bool("check", true, "Check directory structure")
doDownloadImages := flag.Bool("download", false, "Download images")
doCreateGif := flag.Bool("gif", false, "Generate gif file")
doServe := flag.Bool("serve", true, "Start the webserver")
doPeriodicChecking := flag.Bool("periodicdownload", true, "Do hourly image check")
gifDir := flag.String("gifdir", "gifs", "Directory for generated gifs")
imagesDir := flag.String("imagesdir", "images", "Directory for downloaded images")
frameTime := flag.Int("frametime", 50, "Delay between frames in 10ms")
gifFileName := flag.String("gifname", "anim.gif", "Name of the produced gif file")
address := flag.String("address", "0.0.0.0", "Local address to bind to")
port := flag.Int("port", 8080, "Local port to bind to")
flag.Parse()
if *doCheckStructure {
log.Println("Checking directory structure")
ok := EnsureDirectoryStructure(*imagesDir, *gifDir)
if !ok {
log.Fatal("Failed to create directory structure")
}
}
if *doDownloadImages {
log.Println("Downloading images")
DownloadImages(*imagesDir)
}
if *doCreateGif {
log.Println("Creating gif")
err := CreateGif(*frameTime, *imagesDir, path.Join(*gifDir, *gifFileName))
if err != nil {
log.Println(err)
}
}
if *doPeriodicChecking {
t := time.NewTicker(CHECKING_PERIOD)
go func() {
for range t.C {
log.Println("Downloading images, periodic")
downloaded := DownloadImages(*imagesDir)
if downloaded > 0 {
log.Println("Creating gif because image was downloaded")
CreateGif(*frameTime, *imagesDir, path.Join(*gifDir, *gifFileName))
}
}
}()
defer t.Stop()
}
if *doServe {
log.Println(" --------- Starting webserver")
StartServer(*address, *port)
}
log.Println(" --------- Finished")
}