-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (96 loc) · 2.63 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
AVI Video Compression: ffmpeg -i input.avi -vcodec msmpeg4v2 output.avi
MP4 Video Compression: ffmpeg -i input.mp4 -acodec mp2 output.mp4
*/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/g-kutty/v-comp/compressor"
"github.com/g-kutty/v-comp/graceful"
"github.com/g-kutty/v-comp/logger"
"github.com/g-kutty/v-comp/watcher"
)
const appVersion = "1.0"
var (
path string
help bool
version bool
threads int
)
// Read command line arguments before start.
func init() {
flag.StringVar(&path, "p", "", "")
flag.StringVar(&path, "path", "", "")
flag.IntVar(&threads, "t", 4, "")
flag.IntVar(&threads, "thread", 4, "")
flag.BoolVar(&version, "v", false, "")
flag.BoolVar(&version, "version", false, "")
flag.BoolVar(&help, "h", false, "")
flag.BoolVar(&help, "help", false, "")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: v-comp [options]\n")
fmt.Fprintf(os.Stderr, "options:\n")
fmt.Fprintf(os.Stderr, "\t-p, -path Directory The directory to watch.\n")
fmt.Fprintf(os.Stderr, "\t-t, -thread Threads Number of threads run at a time.\n")
fmt.Fprintf(os.Stderr, "\t-v, -version Version Prints the version.\n")
fmt.Fprintf(os.Stderr, "\t-h, -help Help Show this help.\n")
}
}
func main() {
// gracefull shutdown.
go graceful.ActivateGracefulShutdown()
parseFlags()
// video files.
var files []string
// log
logger.Info().Command("watching", "w").Message(logger.FormattedMessage(path)).Log()
// walk through files.
err := filepath.Walk(path, watcher.Visit(&files))
if err != nil {
logger.Error().Message(err.Error()).Log()
return
}
// compress all video files.
err = compressor.Compress(files, threads)
if err != nil {
logger.Error().Message(err.Error()).Log()
return
}
}
// parseFlags read command line arguments.
func parseFlags() {
flag.Parse()
// display version.
if version {
fmt.Printf("v-comp v%s\n", appVersion)
os.Exit(0)
}
// display help guides for command line arguments.
if help {
flag.Usage()
os.Exit(0)
}
validateFlags()
}
// validateFlags validate the flag values.
func validateFlags() {
if path == "" {
// Get the current working directory if no path is specified.
dir, _ := os.Getwd()
path, _ = filepath.Abs(dir)
} else {
dir, err := os.Stat(path)
if err != nil {
logger.Error().Message("Cannot find path,", logger.FormattedMessage(path)).Log()
os.Exit(1)
}
if !dir.IsDir() {
logger.Error().Message(fmt.Sprintf("Invalid path, %s. Path must be directory.", logger.FormattedMessage(path))).Log()
os.Exit(1)
}
path, _ = filepath.Abs(path)
}
}