-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (103 loc) · 2.85 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
package main
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
"sync"
"time"
"github.com/korbexmachina/go-archive-it/utils"
)
func main() {
start := time.Now()
count := 0
verbose := false
configDir, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Failed to resolve user config directory: %s", err)
}
configDir = filepath.Join(configDir, ".config")
configPath := filepath.Join(configDir, "go-archive-it/config.yaml")
helpMessage := `
Usage: go-archive-it [OPTION] ...
---------------------------------
-h, help Display this help message
-e, ext Use external config file (~/.config/go-archive-it/ext.yaml)
-i, init [NAME] Initialize named config file (~/.config/go-archive-it/[NAME].yaml)
-p, path [NAME] Use named config file (~/.config/go-archive-it/[NAME].yaml)
-v, verbose Verbose logging
---------------------------------
Running with no arguments will use the default config file (~/.config/go-archive-it/config.yaml)
`
if len(os.Args) < 2 {
log.Print("Running with no arguments\n")
} else {
switch os.Args[1] {
case "-h", "help":
fmt.Printf(helpMessage)
os.Exit(0)
case "-e", "ext":
configPath = filepath.Join(configDir, "go-archive-it/ext.yaml")
log.Printf("Running with external config: %s", configPath)
case "-i", "init":
name := ""
if len(os.Args) < 3 || os.Args[2] == "" {
name = "go-archive-it/config"
} else {
name = "go-archive-it/" + os.Args[2]
}
utils.ConfigExists(filepath.Join(configDir, name + ".yaml"))
os.Exit(0)
case "-p", "path":
name := "go-archive-it/" + os.Args[2]
configPath = filepath.Join(configDir, name + ".yaml")
log.Printf("Running with named config: %s", configPath)
case "-v", "verbose":
verbose = true
default:
log.Fatalf("Unknown argument: %s", os.Args[1])
}
}
utils.ConfigExists(configPath)
config := utils.LoadConfig(configPath)
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
dir := usr.HomeDir
archivePath := config.ArchivePath
if archivePath == "~" {
archivePath = dir
} else if strings.HasPrefix(archivePath, "~/") {
archivePath = filepath.Join(dir, archivePath[2:])
}
var wg sync.WaitGroup
// The loop that actually runs everything
for _, path := range config.VaultPath {
count++
// Tilda expansion
if path == "~" {
path = dir
} else if strings.HasPrefix(path, "~/") {
path = filepath.Join(dir, path[2:])
}
wg.Add(1)
go func(path string) {
defer wg.Done()
utils.Archive(path, archivePath, config.ArchiveType)
}(path)
wg.Add(1)
go func(path string) {
defer wg.Done()
err := utils.Cleanup(filepath.Join(archivePath, filepath.Base(path)), config.Retention, verbose)
if err != nil {
log.Fatalf("Failed to cleanup: %s", err)
}
}(path)
}
wg.Wait()
elapsed := time.Since(start)
log.Printf("%d Archive(s) created in [[ %f ]] seconds", count, elapsed.Seconds())
}