-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathphotosorcery.go
106 lines (83 loc) · 2.33 KB
/
photosorcery.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
package main
import (
"errors"
"flag"
"os"
"path/filepath"
)
func main() {
if len(os.Args) == 1 {
logAndExit(errors.New("no command given"))
}
switch command := os.Args[1]; command {
case "merge":
files, outputPath := parseMergeInput(os.Args[2:])
merge(files, outputPath)
case "convert":
files, outputDir, fileType := parseConvertInput(os.Args[2:])
convert(files, outputDir, fileType)
default:
logAndExit(errors.New("invalid command"))
}
}
func parseConvertInput(args []string) ([]string, string, FileType) {
fset := flag.NewFlagSet("fset", flag.ContinueOnError)
typePtr := fset.String("type", "", "Target image type")
outPtr := fset.String("out", "", "Directory to write to")
fset.Parse(args)
if *typePtr == "" || *outPtr == "" {
logAndExit(errors.New("type or output dir not provided"))
}
outputPath, err := filepath.Abs(*outPtr)
if err != nil {
logAndExit(errors.New("invalid file path"))
}
outputStat, err := os.Stat(outputPath)
if err != nil {
logAndExit(errors.New("error getting outputdir stats"))
}
if !outputStat.Mode().IsDir() {
logAndExit(errors.New("output path is not directory"))
}
targetType := getFileType(*typePtr)
if targetType == ERR {
logAndExit(errors.New("invalid target file type"))
}
files := formatFiles(args[4:])
return files, outputPath, targetType
}
func parseMergeInput(args []string) ([]string, string) {
fset := flag.NewFlagSet("fset", flag.ContinueOnError)
outPtr := fset.String("out", "", "Directory to write to")
fset.Parse(args)
if *outPtr == "" {
logAndExit(errors.New("no output dir specified"))
}
outputPath, err := filepath.Abs(*outPtr)
if err != nil {
logAndExit(errors.New("invalid file path"))
}
files := formatFiles(args[2:])
return files, outputPath
}
func formatFiles(args []string) []string {
cleanedFiles := make([]string, len(args))
for index, file := range args {
if _, err := os.Stat(file); os.IsNotExist(err) {
logAndExit(errors.New("invalid file"))
}
absPath, err := filepath.Abs(file)
if err != nil {
logAndExit(errors.New("invalid file"))
}
fileStat, err := os.Stat(absPath)
if err != nil {
logAndExit(errors.New("error checking input file stats"))
}
if !fileStat.Mode().IsRegular() {
logAndExit(errors.New("input file is not a regular file"))
}
cleanedFiles[index] = absPath
}
return cleanedFiles
}