Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vault-thirteen committed Sep 23, 2024
1 parent 98c3bb3 commit a96961d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 47 deletions.
16 changes: 11 additions & 5 deletions cmd/hasher/check.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"fmt"
"io"
"log"
"os"

ch "github.com/vault-thirteen/Hasher/pkg/Models/Check"
Expand All @@ -15,6 +17,7 @@ import (

const (
ErrFolderCheckIsNotPossible = "folder check is not possible, use file check"
ErrFErrorOnLine = "Error on line %v."
)

func checkHash(args *cla.CommandLineArguments) (results *ch.Check, err error) {
Expand Down Expand Up @@ -51,11 +54,18 @@ func checkHashesInFile(args *cla.CommandLineArguments) (results *ch.Check, err e

var rdr = reader.New(f)
var line []byte
var lineN int = 1
results = ch.NewCheck()
var hashText string
var hash any
var result *ch.CheckedFile

defer func() {
if err != nil {
log.Println(fmt.Sprintf(ErrFErrorOnLine, lineN))
}
}()

for {
line, err = rdr.ReadLineEndingWithCRLF()
if err != nil {
Expand All @@ -66,11 +76,6 @@ func checkHashesInFile(args *cla.CommandLineArguments) (results *ch.Check, err e
}
}

if len(line) == 0 {
results.AddFile(nil)
continue
}

result = &ch.CheckedFile{}

hashText, result.Path, err = hasher.ParseFileLine(line)
Expand All @@ -89,6 +94,7 @@ func checkHashesInFile(args *cla.CommandLineArguments) (results *ch.Check, err e
}

results.AddFile(result)
lineN++
continue
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/hasher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Notes:
func main() {
args, err := cla.New()
if err != nil {
log.Println(err)
showIntro()
log.Println(err)
showUsage()
os.Exit(1)
return
Expand Down Expand Up @@ -72,7 +72,7 @@ func work(args *cla.CommandLineArguments) (err error) {

func mustBeNoError(err error) {
if err != nil {
panic(err)
log.Fatalln(err.Error())
}
}

Expand Down
12 changes: 5 additions & 7 deletions pkg/Models/Check/Check.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *Check) AddFile(file *CheckedFile) {
c.Counter.Total++

if file == nil {
c.Counter.Damaged++
c.Counter.Bad++
return
}

Expand All @@ -39,11 +39,9 @@ func (c *Check) AddFile(file *CheckedFile) {
}

func (c *Check) PrintReport() {
nBD := c.Counter.Bad + c.Counter.Damaged

// IF all files are good.
if nBD == 0 {
fmt.Println(fmt.Sprintf(TplSummary, c.Counter.Total, c.Counter.Good, nBD))
// If all files are good.
if c.Counter.Bad == 0 {
fmt.Println(fmt.Sprintf(TplSummary, c.Counter.Total, c.Counter.Good, c.Counter.Bad))
fmt.Println(MsgAllClear)
return
}
Expand All @@ -69,5 +67,5 @@ func (c *Check) PrintReport() {

// 3. Summary.
fmt.Println(TplHr)
fmt.Println(fmt.Sprintf(TplSummary, c.Counter.Total, c.Counter.Good, nBD))
fmt.Println(fmt.Sprintf(TplSummary, c.Counter.Total, c.Counter.Good, c.Counter.Bad))
}
7 changes: 3 additions & 4 deletions pkg/Models/Check/CheckCounter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ch

type CheckCounter struct {
Good int
Bad int
Damaged int
Total int
Good int
Bad int
Total int
}
4 changes: 2 additions & 2 deletions pkg/Models/CommandLineArguments/CommandLineArguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

const (
ErrSyntax = "syntax error"
ErrObjectIsNotFound = "object is not found"
ErrSyntax = "syntax error in command line arguments"
ErrObjectIsNotFound = "target object is not found"
)

type CommandLineArguments struct {
Expand Down
37 changes: 12 additions & 25 deletions pkg/Models/Hashing/Hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,53 @@ func (h *Hashing) GetType() (ht ht.HashType) {

func (h *Hashing) Calculate(file string) (result *HashingResult, err error) {
var data any
var resultType HashingResultType

switch h.typ.ID() {
case ht.Id_FileExistence:
resultType = HashingResultType_Boolean
data, err = checkFileExistence(file)
if err != nil {
return nil, err
}

result, err = NewHashingResult(data, HashingResultType_Boolean)
if err != nil {
return nil, err
}

case ht.Id_FileSize:
resultType = HashingResultType_Integer
data, err = getFileSize(file)
if err != nil {
return nil, err
}

result, err = NewHashingResult(data, HashingResultType_Integer)
if err != nil {
return nil, err
}

case ht.Id_CRC32:
resultType = HashingResultType_Binary
data, err = calculateBinaryFileHash(file, ht.Id_CRC32)
if err != nil {
return nil, err
}

result, err = NewHashingResult(data, HashingResultType_Binary)
if err != nil {
return nil, err
}

case ht.Id_MD5:
resultType = HashingResultType_Binary
data, err = calculateBinaryFileHash(file, ht.Id_MD5)
if err != nil {
return nil, err
}

result, err = NewHashingResult(data, HashingResultType_Binary)
if err != nil {
return nil, err
}

case ht.Id_SHA256:
resultType = HashingResultType_Binary
data, err = calculateBinaryFileHash(file, ht.Id_SHA256)
if err != nil {
return nil, err
}

result, err = NewHashingResult(data, HashingResultType_Binary)
if err != nil {
return nil, err
}

default:
return nil, c.Error(ht.ErrUnknownHashType)
}

result, err = NewHashingResult(data, resultType)
if err != nil {
return nil, err
}

return result, nil
}

Expand Down Expand Up @@ -188,6 +174,7 @@ func (h *Hashing) ParseFileLine(line []byte) (hashText string, filePath string,
return hashText, filePath, nil
}

// Hash sum is not a byte array.
var p1, p2 string
p1, p2, err = splitLine(line)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/Models/Hashing/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func calculateBinaryFileHash(filePath string, hti ht.HashTypeId) (sum []byte, er
var data []byte
data, err = af.GetFileContents(filePath)
if err != nil {
return sum, err
return nil, err
}

switch hti {
Expand Down Expand Up @@ -46,7 +46,7 @@ func getFileSize(filePath string) (fileSize int, err error) {
var fi os.FileInfo
fi, err = os.Stat(filePath)
if err != nil {
return 0, err
return -1, err
}

x := fi.Size()
Expand Down

0 comments on commit a96961d

Please sign in to comment.