-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (105 loc) · 2.71 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
113
114
115
116
117
118
119
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/fatih/color"
flags "github.com/jessevdk/go-flags"
)
func main() {
cmd := &CopierCommand{}
parser := flags.NewParser(cmd, flags.Default)
parser.Command.Find("copy")
_, err := parser.Parse()
if err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}
}
type CopierCommand struct {
CopyCommand CopyCommand `command:"copy"`
}
type CopyCommand struct {
InputDir string `short:"i" long:"input-directory" default:"." description:"The directory from which I would read the photos"`
OutputDir string `short:"o" long:"output-directory" default:"." description:"The directory from which I would put the photos' folder in"`
}
func (c *CopyCommand) Execute(args []string) error {
return copyFiles(c.InputDir, c.OutputDir)
}
func copyFiles(from, to string) error {
dirsInOutput := make(map[string]bool)
files, err := ioutil.ReadDir(from)
if err != nil {
return fmt.Errorf("input read error: %w", err)
}
if _, err := os.Stat(to); os.IsNotExist(err) {
if err := os.MkdirAll(to, 0755); err != nil {
return err
}
}
outputDirs, err := ioutil.ReadDir(to)
if err != nil {
return fmt.Errorf("output read error: %w", err)
}
for _, dir := range outputDirs {
if dir.IsDir() {
dirsInOutput[dir.Name()] = true
}
}
for _, file := range files {
if file.IsDir() {
if err := copyFiles(filepath.Join(from, file.Name()), to); err != nil {
return err
}
continue
}
log.Println(filepath.Join(from, file.Name()), "👮♂️ying")
dirToSaveIn := file.ModTime().Format("20060102(Mon Jan 02 2006)")
if !dirsInOutput[dirToSaveIn] {
os.MkdirAll(filepath.Join(to, dirToSaveIn), 0755)
dirsInOutput[dirToSaveIn] = true
}
if _, err := os.Stat(filepath.Join(to, dirToSaveIn, file.Name())); err == nil {
log.Println(color.RedString("^^ file exists.. skipping"))
continue
}
if err := copyFile(filepath.Join(from, file.Name()), filepath.Join(to, dirToSaveIn, file.Name()), file.Mode()); err != nil {
return err
}
log.Println(color.GreenString("done"))
}
return nil
}
func copyFile(from, to string, mode os.FileMode) error {
if runtime.GOOS == `linux` {
cpCmd := exec.Command("cp", "-rf", from, to)
return cpCmd.Run()
} else if runtime.GOOS == `darwin` {
cpCmd := exec.Command("ditto", from, to)
return cpCmd.Run()
} else {
fromFile, err := os.Open(from)
if err != nil {
log.Fatal(err)
return err
}
defer fromFile.Close()
toFile, err := os.OpenFile(to, os.O_RDWR|os.O_CREATE, mode)
if err != nil {
log.Fatal(err)
return err
}
defer toFile.Close()
_, err = io.Copy(toFile, fromFile)
if err != nil {
log.Fatal(err)
return err
}
return nil
}
}