-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt_cmd.go
74 lines (61 loc) · 1.55 KB
/
decrypt_cmd.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
package main
import (
"flag"
"fmt"
"os"
)
type decryptCmd struct {
flagSet *flag.FlagSet
inputPath string
outputPath string
decrypt cryptHandler
}
func newDecryptCmd(decryptH cryptHandler) *decryptCmd {
decryptcmd := &decryptCmd{
flagSet: flag.NewFlagSet("decrypt", flag.ExitOnError),
decrypt: decryptH,
}
decryptcmd.flagSet.StringVar(&decryptcmd.inputPath, "f", "encrypt-result", "your file path which you want to decrypt")
decryptcmd.flagSet.StringVar(&decryptcmd.outputPath, "o", "decrypt-result", "your file output name")
decryptcmd.flagSet.Usage = func() {
fmt.Fprintln(os.Stderr, "USAGE:")
fmt.Fprintln(os.Stderr, " decrypt -f [your file] -o [your new file]")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "COMMANDS:")
fmt.Fprintln(os.Stderr, " decrypt - to decrypt a file")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "OPTIONS:")
decryptcmd.flagSet.PrintDefaults()
}
return decryptcmd
}
func (c *decryptCmd) Validate(args []string) error {
if err := c.flagSet.Parse(args[2:]); err != nil {
return err
}
_, err := os.Stat(c.inputPath)
if err != nil {
if os.IsNotExist(err) {
return ErrFileNotFound
}
return err
}
return nil
}
func (c *decryptCmd) Execute(key []byte) error {
data, err := os.ReadFile(c.inputPath)
if err != nil {
return ErrFileNotFound
}
plaintext, err := c.decrypt(data, key)
if err != nil {
return err
}
if err = os.WriteFile(c.outputPath, plaintext, 0644); err != nil {
return err
}
return nil
}
func (c decryptCmd) Name() string {
return c.flagSet.Name()
}