Skip to content

Commit

Permalink
Merge pull request #4 from abdullah-alaadine/handle-errors
Browse files Browse the repository at this point in the history
Adding Handle errors
  • Loading branch information
AAVision authored Jul 7, 2023
2 parents 6c6544b + b190931 commit 40ad927
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func main() {
color.Yellowln("Processing...")
start := time.Now()

mungeInit(userInput)
err := mungeInit(userInput)
if err != nil {
color.Errorln("Error:", err)
return
}

duration := time.Since(start)
color.Cyanln("Finished in:", duration)
Expand Down
45 changes: 26 additions & 19 deletions munge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"errors"
"os"
"strings"

Expand All @@ -13,8 +14,7 @@ import (
var tmpWordList []string
var inputFileLines []string

func mungeInit(arg UserInput) {

func mungeInit(arg UserInput) error {
level := arg.Level
wordListPath := arg.Word
input := arg.Input
Expand All @@ -30,8 +30,10 @@ func mungeInit(arg UserInput) {
}

if wordListPath != "" && input == "" {

readWordList(wordListPath)
err := readWordList(wordListPath)
if err != nil {
return err
}

if rDeplicate {
inputFileLines = removeDuplication(inputFileLines)
Expand All @@ -40,24 +42,24 @@ func mungeInit(arg UserInput) {
for _, line := range inputFileLines {
mungeword(line, level)
}

writeMunge(output)

} else if wordListPath == "" && input != "" {
mungeword(input, level)

writeMunge(output)

} else {
color.Error.Println("Input or wordlist required!")
return errors.New("input or wordlist required")
}

err := writeMunge(output)
if err != nil {
return err
}

return nil
}

func readWordList(path string) {
func readWordList(path string) error {
readFile, err := os.Open(path)

if err != nil {
color.Error.Println("Error reading the file!")
return err
}
defer readFile.Close()

Expand All @@ -67,29 +69,34 @@ func readWordList(path string) {
for fileScanner.Scan() {
inputFileLines = append(inputFileLines, fileScanner.Text())
}
}

func writeMunge(fileName string) {
return nil
}

func writeMunge(fileName string) error {
if fileName == "" {
fileName = "munge-passwords.txt"
}

file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

if err != nil {
color.Error.Println("Failed creating file!")
return err
}
defer file.Close()

datawriter := bufio.NewWriter(file)

for _, data := range tmpWordList {
_, _ = datawriter.WriteString(data + "\n")
_, err := datawriter.WriteString(data + "\n")
if err != nil {
return err
}
}

color.Greenp("Saved to ", fileName, "\n")
datawriter.Flush()

return nil
}

func removeDuplication(arr []string) []string {
Expand Down

0 comments on commit 40ad927

Please sign in to comment.