Skip to content

Commit

Permalink
feat: improve log messages (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
zze0s authored Mar 26, 2023
1 parent b3fdac3 commit 3b06dbc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
12 changes: 6 additions & 6 deletions cmd/bencode.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ func RunBencodeEdit() *cobra.Command {
}

for _, file := range files {
fmt.Printf("found: %s", file)
processedFiles++

_, fileName := filepath.Split(file)

if export != "" {
export = filepath.Join(export, fileName)
}

if err := bencoding.Process(file, export, replacements, verbose); err != nil {
log.Fatalf("error processing file: %v", err)
log.Fatalf("error processing file: %q\n", err)
}

processedFiles++

if verbose {
log.Printf("[%d/%d] sucessfully processed file %s", len(files), processedFiles, fileName)
fmt.Printf("[%d/%d] sucessfully processed file %s\n", len(files), processedFiles, fileName)
}

}

fmt.Printf("processed %d files\n", processedFiles)
fmt.Printf("migraterr bencode processed %d files\n", processedFiles)
}

return command
Expand Down
24 changes: 13 additions & 11 deletions internal/bencoding/bencode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
func Process(path, export string, replacements []string, verbose bool) error {
read, err := os.ReadFile(path)
if err != nil {
log.Fatalf("error reading file: %s err: %q", path, err)
log.Fatalf("error reading file: %s err: %q\n", path, err)
}

var fastResume map[string]interface{}
if err := bencode.DecodeString(string(read), &fastResume); err != nil {
log.Printf("could not decode bencode file: %s", path)
log.Printf("could not decode bencode file: %s\n", path)
}

if len(replacements) > 0 {
Expand Down Expand Up @@ -53,25 +53,27 @@ func Process(path, export string, replacements []string, verbose bool) error {

if export != "" {
if err = Encode(export, fastResume); err != nil {
log.Printf("could not export fastresume file %s error: %q", path, err)
log.Printf("could not export fastresume file %s error: %q\n", path, err)
return err
}
} else {
if err = Encode(path, fastResume); err != nil {
log.Printf("could not write fastresume file %s error: %q", path, err)
log.Printf("could not write fastresume file %s error: %q\n", path, err)
return err
}
}

log.Printf("sucessfully processed file %s", path)
if verbose {
log.Printf("sucessfully processed file %s\n", path)
}

return nil
}

func Encode(path string, data any) error {
file, err := os.Create(path)
if err != nil {
log.Printf("os create error: %q", err)
log.Printf("os create error: %q\n", err)
return err
}

Expand All @@ -80,7 +82,7 @@ func Encode(path string, data any) error {
bufferedWriter := bufio.NewWriter(file)
encoder := bencode.NewEncoder(bufferedWriter)
if err := encoder.Encode(data); err != nil {
log.Printf("encode error: %q", err)
log.Printf("encode error: %q\n", err)
return err
}

Expand All @@ -94,21 +96,21 @@ func Encode(path string, data any) error {
func Info(path string) error {
read, err := os.ReadFile(path)
if err != nil {
log.Fatalf("error reading file: %s err: %q", path, err)
log.Fatalf("error reading file: %s err: %q\n", path, err)
}

var fastResume map[string]interface{}
if err := bencode.DecodeString(string(read), &fastResume); err != nil {
log.Printf("could not decode bencode file %s", path)
log.Printf("could not decode bencode file %s\n", path)
}

_, fileName := filepath.Split(path)

fmt.Printf("Filename: %s\n", fileName)
fmt.Printf("\nFilename: %s\n", fileName)
for k, v := range fastResume {
fmt.Printf("%s: %v\n", k, v)
}
fmt.Println("")
fmt.Printf("\n")

return nil
}

0 comments on commit 3b06dbc

Please sign in to comment.