-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (91 loc) · 1.97 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
package main
import (
"fmt"
"os"
)
func printUsageAndExit(exitCode int) {
fmt.Println("Usage:")
fmt.Println(" Help:")
fmt.Println(" sft -h")
fmt.Println(" Encryption:")
fmt.Println(" sft <options> encrypt <file> ...")
fmt.Println(" Options:")
fmt.Println(" -p set password (rarely needed)")
fmt.Println("")
fmt.Println(" Decryption:")
fmt.Println(" sft <options> decrypt https://filetransfer.kpn.com/download/<uuid>#<base64key>")
fmt.Println(" Options:")
fmt.Println(" -s show the list of files (do not download them) and exit")
os.Exit(exitCode)
}
func parseOptions() ([]string, Options) {
fmt.Println("")
var options = Options{}
i := 0
var arg string
for i, arg = range os.Args[1:] {
if len(arg) != 2 {
return os.Args[1+i:], options
}
if arg[0] == '-' {
switch arg[1] {
case 'p':
{
options.Password = true
}
case 's':
{
options.Show = true
}
case 'h':
{
options.Help = true
}
default:
{
fmt.Println("Unknown argument", arg)
printUsageAndExit(1)
}
}
} else {
break
}
}
return os.Args[1+i:], options
}
func parseMode(rem []string, options *Options) (encrypt bool, f []string) {
if len(rem) < 2 {
fmt.Println("Too few arguments")
printUsageAndExit(1)
}
if rem[0] == "encrypt" {
encrypt = true
} else if rem[0] == "decrypt" {
encrypt = false
} else {
fmt.Println("Unknown argument: ", rem[0])
printUsageAndExit(1)
}
if !encrypt {
if len(rem) != 2 {
fmt.Println("Too many arguments for decryption")
printUsageAndExit(1)
}
}
return encrypt, rem[1:]
}
func main() {
rem, options := parseOptions()
if options.Help {
printUsageAndExit(0)
}
encrypt, f := parseMode(rem, &options)
if encrypt {
url := encryptFiles(f, &options)
fmt.Println("Successfully encrypted and uploaded the file(s)")
fmt.Println("Download url:")
fmt.Println(url)
} else {
decryptFromUrl(f[0], &options)
}
}