@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"flag"
5
+ "fmt"
5
6
"log"
6
7
"net/http"
7
8
"os"
@@ -13,15 +14,10 @@ import (
13
14
"github.com/diskfs/go-diskfs/filesystem/squashfs"
14
15
)
15
16
16
- func main () {
17
- filename := flag .String ("filename" , "" , "File to serve" )
18
- addr := flag .String ("addr" , ":8100" , "address & port to server on" )
19
- fsType := flag .String ("type" , "iso9660" , "Filesystem type (iso9660, fat32, squashfs)" )
20
- flag .Parse ()
21
-
17
+ func serve (filename , addr , fsType * string ) error {
22
18
f , err := os .Open (* filename )
23
19
if err != nil {
24
- log . Fatalf ( "Cannot open %q: %s" , * filename , err )
20
+ return fmt . Errorf ( "cannot open %q: %s" , * filename , err )
25
21
}
26
22
b := file .New (f , true )
27
23
@@ -35,15 +31,26 @@ func main() {
35
31
case "squashfs" :
36
32
fs , err = squashfs .Read (b , 0 , 0 , 0 )
37
33
default :
38
- log . Fatalf ( "Unknown filesystem type %q" , * fsType )
34
+ return fmt . Errorf ( "unknown filesystem type %q" , * fsType )
39
35
}
40
36
if err != nil {
41
- log . Fatalf ( "Cannot open %s image in %q: %s" , * fsType , * filename , err )
37
+ return fmt . Errorf ( "cannot open %s image in %q: %s" , * fsType , * filename , err )
42
38
}
43
39
44
40
http .Handle ("/" , http .FileServer (http .FS (filesystem .FS (fs ))))
45
41
46
42
log .Printf ("Serving %q on HTTP port: %s\n " , * filename , * addr )
47
- log .Fatal (http .ListenAndServe (* addr , nil ))
43
+ //nolint:gosec // we know this violates G114, but it is just an example
44
+ return http .ListenAndServe (* addr , nil )
45
+ }
48
46
47
+ func main () {
48
+ filename := flag .String ("filename" , "" , "File to serve" )
49
+ addr := flag .String ("addr" , ":8100" , "address & port to server on" )
50
+ fsType := flag .String ("type" , "iso9660" , "Filesystem type (iso9660, fat32, squashfs)" )
51
+ flag .Parse ()
52
+
53
+ if err := serve (filename , addr , fsType ); err != nil {
54
+ log .Fatalf ("Error serving: %s" , err )
55
+ }
49
56
}
0 commit comments