-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
177 lines (154 loc) · 4.57 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"fmt"
"os"
"path"
"strings"
docopt "github.com/docopt/docopt-go"
"github.com/goggle/flatten/filesystem"
"github.com/goggle/flatten/flatten"
"github.com/goggle/flatten/osabstraction"
)
const version = "0.8.0"
func ask(question string, defaultYes bool) bool {
var defaultString string
if defaultYes {
defaultString = "[y]/n:"
} else {
defaultString = "y/[n]:"
}
reader := bufio.NewReader(os.Stdin)
fmt.Printf(question + " " + defaultString + " ")
test, _ := reader.ReadString('\n')
test = strings.Trim(test, "\n ")
if defaultYes {
if test == "n" || test == "N" || test == "no" || test == "No" || test == "NO" {
return false
}
return true
}
if test == "y" || test == "Y" || test == "yes" || test == "Yes" || test == "YES" {
return true
}
return false
}
func simulate(sourceFI osabstraction.FileInfo, destinationFI osabstraction.FileInfo, copyOnly bool, includeSourceFiles bool) (string, error) {
fs := filesystem.Filesystem{}
fs.Init()
err := fs.AddFromRealFilesystem(sourceFI.FullPath())
if err != nil {
return "", err
}
err = fs.AddFromRealFilesystem(destinationFI.FullPath())
if err != nil {
return "", err
}
err = flatten.Flatten(sourceFI, destinationFI, fs, copyOnly, includeSourceFiles)
if err != nil {
return "", err
}
tree := filesystem.Tree{}
tree.Create(destinationFI, fs)
return fmt.Sprintf("%v", tree), nil
}
func main() {
usage := `flatten.
Usage:
flatten [SOURCE] [DESTINATION] [-c | --copy-only] [-f | --force] [--include-source-files] [-s | --simulate-only] [--verbose]
flatten -h | --help
flatten -v
Recursively flatten the directory structure from SOURCE to DESTINATION.
Arguments:
SOURCE Optional source directory (default is current directory).
DESTINATION Optional destination directory (default is current directory).
Options:
-c --copy-only Do not remove anything from the source directory.
-f --force Do not propose a simulation first, immediately execute the command.
--include-source-files Include the files which are directly located in the SOURCE directory.
-s --simulate-only Do not move or copy any files on the system,
just output the expected result.
--verbose Explain what is being done.
-v --version Show version.
-h --help Show this screen.`
arguments, _ := docopt.Parse(usage, nil, true, "flatten "+version, false)
var source string
var destination string
src := arguments["SOURCE"]
if src == nil {
p, err := os.Getwd()
if err != nil {
fmt.Printf("%v\n", err)
return
}
source = p
} else {
source = src.(string)
}
dst := arguments["DESTINATION"]
if dst == nil {
p, err := os.Getwd()
if err != nil {
fmt.Printf("%v\n", err)
return
}
destination = p
} else {
destination = dst.(string)
}
verbose := arguments["--verbose"].(bool)
if verbose {
flatten.SetVerbose()
}
includeSourceFiles := arguments["--include-source-files"].(bool)
copyOnly := arguments["--copy-only"].(bool)
simulateOnly := arguments["--simulate-only"].(bool)
force := arguments["--force"].(bool)
performSimulation := false
askSecondQuestion := true
sourceFI := osabstraction.File(path.Clean(source))
destinationFI := osabstraction.File(path.Clean(destination))
if simulateOnly {
performSimulation = true
}
// Propose to do a simulation first as long we are not in the
// "force" or "simulation-only" mode:
if !force && !simulateOnly {
anw := ask("Flatten performs changes on the file system. Do you want to simulate this process first?", true)
if anw {
performSimulation = true
} else {
performSimulation = false
askSecondQuestion = false
}
}
if performSimulation {
treeString, err := simulate(sourceFI, destinationFI, copyOnly, includeSourceFiles)
if err != nil {
fmt.Println("Could not simulate the process. The following error occured:")
fmt.Println(err)
os.Exit(1)
}
fmt.Printf(treeString)
}
// If we are in "simulation-only" mode, we can exit the program
// at this point.
if simulateOnly {
os.Exit(0)
}
// Ask the user to continue the process as long as we are not in
// the "force" mode.
if !force && askSecondQuestion {
anw := ask("The above changes will be performed. Do you want to continue?", false)
if !anw {
os.Exit(0)
}
}
// Perform the flattening process on the real filesystem:
osWrapper := osabstraction.RealOS{}
err := flatten.Flatten(sourceFI, destinationFI, osWrapper, copyOnly, includeSourceFiles)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}