Skip to content

Commit 182373d

Browse files
committed
Refactor export functions
- Pass output path as parameter - Return and handle errors correctly
1 parent 0454e29 commit 182373d

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

bin/stitch/export-jpeg.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package main
22

33
import (
4+
"fmt"
45
"image/jpeg"
56
"log"
67
"os"
78
)
89

9-
func exportJPEG(stitchedImage *StitchedImage) error {
10-
log.Printf("Creating output file %q.", *flagOutputPath)
11-
f, err := os.Create(*flagOutputPath)
10+
func exportJPEG(stitchedImage *StitchedImage, outputPath string) error {
11+
log.Printf("Creating output file %q.", outputPath)
12+
f, err := os.Create(outputPath)
1213
if err != nil {
13-
log.Panic(err)
14+
return fmt.Errorf("failed to create file: %w", err)
1415
}
1516
defer f.Close()
1617

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

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

2526
return nil

bin/stitch/export-png.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package main
22

33
import (
4+
"fmt"
45
"image/png"
56
"log"
67
"os"
78
)
89

9-
func exportPNG(stitchedImage *StitchedImage) error {
10-
log.Printf("Creating output file %q.", *flagOutputPath)
11-
f, err := os.Create(*flagOutputPath)
10+
func exportPNG(stitchedImage *StitchedImage, outputPath string) error {
11+
log.Printf("Creating output file %q.", outputPath)
12+
f, err := os.Create(outputPath)
1213
if err != nil {
13-
log.Panic(err)
14+
return fmt.Errorf("failed to create file: %w", err)
1415
}
1516
defer f.Close()
1617

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

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

2526
return nil

bin/stitch/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,13 @@ func main() {
269269
fileExtension := strings.ToLower(filepath.Ext(*flagOutputPath))
270270
switch fileExtension {
271271
case ".png":
272-
exportPNG(stitchedImage)
272+
if err := exportPNG(stitchedImage, *flagOutputPath); err != nil {
273+
log.Panicf("Export of PNG file failed: %v", err)
274+
}
273275
case ".jpg", ".jpeg":
274-
exportJPEG(stitchedImage)
276+
if err := exportJPEG(stitchedImage, *flagOutputPath); err != nil {
277+
log.Panicf("Export of JPEG file failed: %v", err)
278+
}
275279
default:
276280
log.Panicf("Unknown output format %q.", fileExtension)
277281
}

0 commit comments

Comments
 (0)