Skip to content

Commit 2063e8f

Browse files
author
Rijul Gulati
committed
build fix
1 parent 032d5f1 commit 2063e8f

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

main.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,25 @@ func main() {
2727
flag.Parse()
2828

2929
if *inputFilePtr == "" {
30-
fmt.Println(andotp.FormatError("No input file provided\nSee -h for available options."))
30+
fmt.Println(formatError("No input file provided\nSee -h for available options."))
3131
os.Exit(0)
3232
}
3333

3434
if *encryptPtr && *decryptPtr {
35-
fmt.Println(andotp.FormatError("Please provide any one of encrypt (-e) or decrypt (-d) flag"))
35+
fmt.Println(formatError("Please provide any one of encrypt (-e) or decrypt (-d) flag"))
3636
os.Exit(0)
3737
}
3838

3939
if *passwordPtr == "" {
4040
*passwordPtr = getPassword()
4141
if *passwordPtr == "" {
42-
fmt.Println(andotp.FormatError("No password provided."))
42+
fmt.Println(formatError("No password provided."))
4343
os.Exit(0)
4444
}
4545
}
4646

4747
if *encryptPtr {
48-
49-
plaintext, err := andotp.ReadFile(*inputFilePtr)
50-
if err != nil {
51-
fmt.Print(err.Error())
52-
os.Exit(0)
53-
}
54-
48+
plaintext := readFile(*inputFilePtr)
5549
filecontent, err := andotp.Encrypt(plaintext, *passwordPtr)
5650

5751
if err != nil {
@@ -62,24 +56,16 @@ func main() {
6256
processfile(filecontent, *outputFilePtr)
6357

6458
} else if *decryptPtr {
65-
66-
encryptedtext, err := andotp.ReadFile(*inputFilePtr)
67-
if err != nil {
68-
fmt.Print(err.Error())
69-
os.Exit(0)
70-
}
71-
59+
encryptedtext := readFile(*inputFilePtr)
7260
filecontent, err := andotp.Decrypt(encryptedtext, *passwordPtr)
73-
7461
if err != nil {
7562
fmt.Print(err.Error())
7663
os.Exit(0)
7764
}
78-
7965
processfile(filecontent, *outputFilePtr)
8066

8167
} else {
82-
fmt.Println(andotp.FormatError("Please provide encrypt (-e) or decrypt (-d) flag"))
68+
fmt.Println(formatError("Please provide encrypt (-e) or decrypt (-d) flag"))
8369
os.Exit(0)
8470
}
8571
}
@@ -96,8 +82,21 @@ func getPassword() string {
9682
fmt.Print("Password: ")
9783
pass, err := term.ReadPassword(int(syscall.Stdin))
9884
if err != nil {
99-
fmt.Print(andotp.FormatError(err.Error()))
85+
fmt.Print(formatError(err.Error()))
10086
}
10187
fmt.Print("\n")
10288
return string(pass)
10389
}
90+
91+
func readFile(file string) []byte {
92+
f, err := os.ReadFile(file)
93+
if err != nil {
94+
fmt.Println(err.Error())
95+
os.Exit(1)
96+
}
97+
return f
98+
}
99+
100+
func formatError(e string) error {
101+
return fmt.Errorf("error: %s", e)
102+
}

0 commit comments

Comments
 (0)