Skip to content

Commit

Permalink
Refactor export functions
Browse files Browse the repository at this point in the history
- Pass output path as parameter
- Return and handle errors correctly
  • Loading branch information
Dadido3 committed Dec 22, 2023
1 parent 0454e29 commit 182373d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
11 changes: 6 additions & 5 deletions bin/stitch/export-jpeg.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"fmt"
"image/jpeg"
"log"
"os"
)

func exportJPEG(stitchedImage *StitchedImage) error {
log.Printf("Creating output file %q.", *flagOutputPath)
f, err := os.Create(*flagOutputPath)
func exportJPEG(stitchedImage *StitchedImage, outputPath string) error {
log.Printf("Creating output file %q.", outputPath)
f, err := os.Create(outputPath)
if err != nil {
log.Panic(err)
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()

Expand All @@ -19,7 +20,7 @@ func exportJPEG(stitchedImage *StitchedImage) error {
}

if err := jpeg.Encode(f, stitchedImage, options); err != nil {
log.Panic(err)
return fmt.Errorf("failed to encode image %q: %w", outputPath, err)
}

return nil
Expand Down
11 changes: 6 additions & 5 deletions bin/stitch/export-png.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"fmt"
"image/png"
"log"
"os"
)

func exportPNG(stitchedImage *StitchedImage) error {
log.Printf("Creating output file %q.", *flagOutputPath)
f, err := os.Create(*flagOutputPath)
func exportPNG(stitchedImage *StitchedImage, outputPath string) error {
log.Printf("Creating output file %q.", outputPath)
f, err := os.Create(outputPath)
if err != nil {
log.Panic(err)
return fmt.Errorf("failed to create file: %w", err)
}
defer f.Close()

Expand All @@ -19,7 +20,7 @@ func exportPNG(stitchedImage *StitchedImage) error {
}

if err := encoder.Encode(f, stitchedImage); err != nil {
log.Panic(err)
return fmt.Errorf("failed to encode image %q: %w", outputPath, err)
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions bin/stitch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ func main() {
fileExtension := strings.ToLower(filepath.Ext(*flagOutputPath))
switch fileExtension {
case ".png":
exportPNG(stitchedImage)
if err := exportPNG(stitchedImage, *flagOutputPath); err != nil {
log.Panicf("Export of PNG file failed: %v", err)
}
case ".jpg", ".jpeg":
exportJPEG(stitchedImage)
if err := exportJPEG(stitchedImage, *flagOutputPath); err != nil {
log.Panicf("Export of JPEG file failed: %v", err)
}
default:
log.Panicf("Unknown output format %q.", fileExtension)
}
Expand Down

0 comments on commit 182373d

Please sign in to comment.