-
Notifications
You must be signed in to change notification settings - Fork 1
/
decrypt-file.go
62 lines (53 loc) · 1.08 KB
/
decrypt-file.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
package main
import (
"fmt"
"os"
"io/fs"
"io/ioutil"
"github.com/urfave/cli/v2"
)
var cmdDecryptFile = &cli.Command{
Name: "decrypt-file",
Usage: "Decrypt entire file",
Flags: []cli.Flag{
inFlag,
outFlag,
strategyFlag,
passphraseFlag,
flagLogLevel,
},
Action: func(ctx *cli.Context) error {
inPath := ctx.String("in")
inFile, err := os.OpenFile(inPath, os.O_RDONLY, 0)
if err != nil {
return err
}
cipher, err := getCipher(ctx)
if err != nil {
return err
}
rawInput, err := ioutil.ReadAll(inFile)
if err != nil {
return err
}
decryptedData, err := cipher.Decrypt(string(rawInput))
if err != nil {
return err
}
outPath := ctx.String("out")
switch outPath {
case "/dev/stdout":
fmt.Printf(decryptedData)
return nil
case "/dev/stderr":
fmt.Fprintf(os.Stderr, decryptedData)
return nil
}
// For in-place edits, overwrite the file
outFileMode := os.O_EXCL
if outPath == inPath {
outFileMode = os.O_WRONLY | os.O_TRUNC
}
return ioutil.WriteFile(outPath, []byte(decryptedData), fs.FileMode(outFileMode))
},
}