-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyarchive.go
228 lines (169 loc) · 4.68 KB
/
easyarchive.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
"github.com/jdockerty/easyarchive/internal/glacierupload"
"github.com/jdockerty/easyarchive/internal/md5calc"
"github.com/jdockerty/easyarchive/internal/zipdir"
)
const (
runningOS = runtime.GOOS
configFile = "config.json"
)
type hashVal struct {
Filename string `json:"Filename"`
HashValue string `json:"Value"`
}
type configArchive struct {
ArchiveLocation string `json:"Archive Location"`
BucketName string `json:"S3 Bucket"`
Hashes []hashVal `json:"Hashes"`
}
var (
currentConfig configArchive
)
func createConfig() {
configFile, err := os.Create(configFile)
if err != nil {
log.Println("Error creating config.json.")
panic(err)
}
defer configFile.Close()
}
func setArchivePathAndBucket(fp, bucket string) {
cleanPath := filepath.Clean(fp)
currentConfig.ArchiveLocation = cleanPath
currentConfig.BucketName = bucket
currentConfig.Hashes = nil
writeArchivePathAndBucketToConfig(cleanPath, bucket)
log.Println("Archive path set to", cleanPath)
log.Println("You may now place files into the set path and they will be archived into S3 Glacier upon running the program again.")
time.Sleep(2 * time.Second)
}
func readUserInput() string {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
log.Println("Error reading user input string")
panic(err)
}
return input
}
func readConfigFile() configArchive {
configJSON, err := ioutil.ReadFile(configFile)
if err != nil {
createConfig()
}
var data configArchive
err = json.Unmarshal(configJSON, &data)
if err != nil {
return data
}
return data
}
func calcHashes() []hashVal {
m, err := md5calc.MD5All(currentConfig.ArchiveLocation)
if err != nil {
log.Println("Error running MD5All on archive path.")
panic(err)
}
var paths []string
for path := range m {
paths = append(paths, path)
}
sort.Strings(paths)
var tempNewHashes []hashVal
for _, path := range paths {
// fmt.Printf("%x %s\n", m[path], path)
basePath := filepath.Base(path)
hashString := fmt.Sprintf("%x", m[path])
hashHolder := hashVal{Filename: basePath, HashValue: hashString}
tempNewHashes = append(tempNewHashes, hashHolder)
}
return tempNewHashes
}
func writeHashes(new []hashVal) {
currentConfig.Hashes = new
dataStream, _ := json.MarshalIndent(currentConfig, "", "\t")
ioutil.WriteFile(configFile, dataStream, 0644)
}
func isEqualHash(old, new []hashVal) bool {
for i, v := range new {
if v.HashValue == old[i].HashValue {
continue
} else {
return false
}
}
return true
}
func hashesChanged(oldHashes, newHashes []hashVal) bool {
if len(oldHashes) != len(newHashes) {
return true
} else if isEqualHash(oldHashes, newHashes) == false {
// Hashes have changed if they are not equal
return true
}
// All hashes are the same, no action needed.
return false
}
func writeArchivePathAndBucketToConfig(path, bucket string) {
output, _ := json.MarshalIndent(currentConfig, "", "\t")
ioutil.WriteFile(configFile, output, 0644)
}
func getFilenames(val []hashVal) []string {
var files []string
for _, v := range val {
files = append(files, v.Filename)
}
return files
}
func archiveBucketExist() bool {
if len(currentConfig.BucketName) > 0 {
return true
}
return false
}
func initialSetup(eol string) {
filepath := readUserInput()
filepath = strings.Replace(filepath, eol, "", -1)
bucket := glacierupload.CreateBucket()
setArchivePathAndBucket(filepath, bucket)
main()
}
func main() {
currentConfig = readConfigFile()
// If archive path is set and an S3 bucket has been created in config.json, proceed.
if archivepath := currentConfig.ArchiveLocation; len(archivepath) > 0 && archiveBucketExist() {
newH := calcHashes()
if hashesChanged(currentConfig.Hashes, newH) {
log.Println("Change detected, writing new hashes to config.json...")
writeHashes(newH)
log.Println("Archiving files...")
filenames := getFilenames(currentConfig.Hashes)
outputZip := zipdir.ZipFiles(filenames, archivepath)
glacierupload.UploadArchive(currentConfig.BucketName, outputZip)
} else {
log.Println("No changes detected.")
}
} else {
// Set the archive path and re-run the main function after writing to config.json
log.Println("The archive file path is not set.")
if runningOS == "windows" {
log.Println("Enter the file path of the folder you wish to use, e.g. 'C:\\Users\\Jack\\Desktop\\MyBackups'")
initialSetup("\r\n")
} else if runningOS == "linux" {
log.Println("Enter the file path of the folder you wish to use, e.g. /home/jack/mybackupfolder")
initialSetup("\n")
}
}
}