Skip to content

Commit

Permalink
Remove panic()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankerch committed May 20, 2023
1 parent ab1aa01 commit ec3f0d6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
33 changes: 21 additions & 12 deletions cmd/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,58 @@ import (
"github.com/tankerch/dvpl_converter/common/utils"
)

func convertDVPLtoFile(path string) {
func convertDVPLtoFile(path string) error {
// Validation
fileIsDVPL := utils.IsDVPLFile(path)
if !fileIsDVPL {
return
isDVPL := utils.IsDVPL(path)
if !isDVPL {
return fmt.Errorf("%s skipped, isn't DVPL file", path)
}

// Input
fileBuf, err := os.ReadFile(path)
if err != nil {
panic(err)
return fmt.Errorf("%s failed to read file", path)
}

// Processed
outputBuf, err := dvpl.DecryptDVPL(fileBuf)
if err != nil {
panic(err)
return fmt.Errorf("%s failed to decrypt", path)
}

// Output
var outputPath = utils.DVPLOriginalName(path)
fout, err := os.Create(outputPath)
if err != nil {
panic(err)
return fmt.Errorf("%s failed to create output", path)
}
defer fout.Close()
fout.Write(outputBuf)

// (Optional) Delete original
if deleteOriginalFlag {
if err := os.Remove(path); err != nil {
panic(err)
return fmt.Errorf("%s failed to delete original file", path)
}
}
fmt.Printf("\t%s\n", path)
return nil
}

func StartDecrypting() {
fmt.Println("Start decrypting:")
dirInfo, err := os.Stat(inputDirPath)
if err != nil {
panic(err)
fmt.Printf("Failed to access %s\n", inputDirPath)
return
}

// dirPath is single file
if !dirInfo.IsDir() {
convertDVPLtoFile(inputDirPath)
if err := convertDVPLtoFile(inputDirPath); err != nil {
fmt.Println(err)
return
}
fmt.Printf("- %s\n", inputDirPath)
return
}

Expand All @@ -69,7 +74,11 @@ func StartDecrypting() {
if d.IsDir() {
return nil
}
convertDVPLtoFile(path)
if err := convertDVPLtoFile(path); err != nil {
fmt.Println(err)
return nil
}
fmt.Printf("%s converted\n", path)
return nil
})

Expand Down
34 changes: 22 additions & 12 deletions cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,58 @@ import (
"github.com/tankerch/dvpl_converter/common/utils"
)

func convertFileToDVPL(path string) {
func convertFileToDVPL(path string) error {
// Validation
fileIsDVPL := utils.IsDVPLFile(path)
if fileIsDVPL {
return
isDVPL := utils.IsDVPL(path)
if isDVPL {
return fmt.Errorf("%s skipped, is DVPL file", path)
}

// Input
fileBuf, err := os.ReadFile(path)
if err != nil {
panic(err)
return fmt.Errorf("%s failed to read file", path)
}

// Processed
outputBuf, err := dvpl.EncryptDVPL(fileBuf)
if err != nil {
panic(err)
return fmt.Errorf("%s failed to encrypt", path)
}

// Output
var outputPath = fmt.Sprintf("%s.dvpl", path)
fout, err := os.Create(outputPath)
if err != nil {
panic(err)
return fmt.Errorf("%s failed to create output", path)
}
defer fout.Close()
fout.Write(outputBuf)

// (Optional) Delete original
if deleteOriginalFlag {
if err := os.Remove(path); err != nil {
panic(err)
return fmt.Errorf("%s failed to delete original file", path)
}
}
fmt.Printf("\t%s\n", path)
return nil
}

func StartEcryption() {
fmt.Println("Start Encrypting:")
dirInfo, err := os.Stat(inputDirPath)
if err != nil {
panic(err)
fmt.Printf("Failed to access %s\n", inputDirPath)
return
}

// dirPath is single file
if !dirInfo.IsDir() {
convertFileToDVPL(inputDirPath)
if err := convertFileToDVPL(inputDirPath); err != nil {
fmt.Println(err)
return
}
fmt.Printf("- %s\n", inputDirPath)
return
}

Expand All @@ -69,7 +74,12 @@ func StartEcryption() {
if d.IsDir() {
return nil
}
convertFileToDVPL(path)
if err := convertFileToDVPL(path); err != nil {
fmt.Println(err)
return nil
}

fmt.Printf("%s succesfully converted\n", path)
return nil
})

Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -26,7 +27,8 @@ and now it's used on all known clients, except files that are contained within A
Run: func(cmd *cobra.Command, args []string) {
cwd, err := os.Getwd()
if err != nil {
panic(err)
fmt.Println("failed to get current directory")
return
}

qs := []*survey.Question{
Expand Down Expand Up @@ -64,7 +66,7 @@ and now it's used on all known clients, except files that are contained within A

err = survey.Ask(qs, &answers)
if err != nil {
panic(err)
fmt.Println("failed create survey prompt")
}
inputDirPath = answers.Directory
deleteOriginalFlag = !answers.KeepOriginal
Expand Down
6 changes: 3 additions & 3 deletions common/utils/checkFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"path/filepath"
)

func IsDVPLFile(filename string) bool {
func IsDVPL(filename string) bool {
return filepath.Ext(filename) == ".dvpl"
}

func IsProgramFile(filename string) bool {
exePath, err := os.Executable()
if err != nil {
panic(err)
return false
}

absPath, err := filepath.Abs(filename)
if err != nil {
panic(err)
return false
}
return absPath == exePath
}

0 comments on commit ec3f0d6

Please sign in to comment.