@@ -3,6 +3,7 @@ package main
33import (
44 "errors"
55 "fmt"
6+ "log/slog"
67 "os"
78 "path/filepath"
89 "strconv"
@@ -15,7 +16,8 @@ import (
1516const mountAction = "/mountAction"
1617
1718func main () {
18- fmt .Printf ("WriteFile - Write file to disk\n ------------------------\n " )
19+ logger := slog .New (slog .NewTextHandler (os .Stdout , nil ))
20+ logger .Info ("WriteFile - Write file to a disk device" )
1921
2022 blockDevice := os .Getenv ("DEST_DISK" )
2123 filesystemType := os .Getenv ("FS_TYPE" )
@@ -29,69 +31,87 @@ func main() {
2931
3032 // Validate inputs
3133 if blockDevice == "" {
32- log .Fatalf ("No Block Device speified with Environment Variable [DEST_DISK]" )
34+ logger .Error ("No Block Device speified with Environment Variable [DEST_DISK]" )
35+ os .Exit (1 )
3336 }
3437
3538 if ! filepath .IsAbs (filePath ) {
36- log .Fatal ("Provide path must be an absolute path" )
39+ logger .Error ("Provide path must be an absolute path" )
40+ os .Exit (1 )
3741 }
3842
3943 modePrime , err := strconv .ParseUint (mode , 8 , 32 )
4044 if err != nil {
41- log .Fatalf ("Could not parse mode: %v" , err )
45+ logger .Error ("Could not parse mode" , "error" , err )
46+ os .Exit (1 )
4247 }
4348
4449 fileMode := os .FileMode (modePrime )
4550
4651 dirModePrime , err := strconv .ParseUint (dirMode , 8 , 32 )
4752 if err != nil {
48- log .Fatalf ("Could not parse dirmode: %v" , err )
53+ logger .Error ("Could not parse dirmode" , "error" , err )
54+ os .Exit (1 )
4955 }
5056
5157 newDirMode := os .FileMode (dirModePrime )
5258
5359 fileUID , err := strconv .Atoi (uid )
5460 if err != nil {
55- log .Fatalf ("Could not parse uid: %v" , err )
61+ logger .Error ("Could not parse uid" , "error" , err )
62+ os .Exit (1 )
5663 }
5764
5865 fileGID , err := strconv .Atoi (gid )
5966 if err != nil {
60- log .Fatalf ("Could not parse gid: %v" , err )
67+ logger .Error ("Could not parse gid" , "error" , err )
68+ os .Exit (1 )
6169 }
6270
6371 dirPath , fileName := filepath .Split (filePath )
6472 if len (fileName ) == 0 {
65- log .Fatal ("Provide path must include a file component" )
73+ logger .Error ("Provide path must include a file component" )
74+ os .Exit (1 )
6675 }
6776
6877 // Create the /mountAction mountpoint (no folders exist previously in scratch container)
6978 if err := os .Mkdir (mountAction , os .ModeDir ); err != nil {
70- log .Fatalf ("Error creating the action Mountpoint [%s]" , mountAction )
79+ logger .Error ("Error creating the action Mountpoint" , "mountAction" , mountAction , "error" , err )
80+ os .Exit (1 )
7181 }
7282
7383 // Mount the block device to the /mountAction point
7484 if err := syscall .Mount (blockDevice , mountAction , filesystemType , 0 , "" ); err != nil {
75- log .Fatalf ("Mounting [%s] -> [%s] error [%v]" , blockDevice , mountAction , err )
76- }
77-
78- log .Infof ("Mounted [%s] -> [%s]" , blockDevice , mountAction )
85+ logger .Error ("Mounting block device" , "blockDevice" , blockDevice , "mountAction" , mountAction , "error" , err )
86+ os .Exit (1 )
87+ }
88+ defer func () {
89+ if err := syscall .Unmount (mountAction , 0 ); err != nil {
90+ logger .Error ("Error unmounting device" , "source" , blockDevice , "destination" , mountAction , "error" , err )
91+ } else {
92+ logger .Info ("Unmounted device successfully" , "source" , blockDevice , "destination" , mountAction )
93+ }
94+ }()
95+ logger .Info ("Mounted device successfully" , "source" , blockDevice , "destination" , mountAction )
7996
8097 if err := recursiveEnsureDir (mountAction , dirPath , newDirMode , fileUID , fileGID ); err != nil {
81- log .Fatalf ("Failed to ensure directory exists: %v" , err )
98+ logger .Error ("Failed to ensure directory exists" , "error" , err )
99+ os .Exit (1 )
82100 }
83101
84102 fqFilePath := filepath .Join (mountAction , filePath )
85103 // Write the file to disk
86104 if err := os .WriteFile (fqFilePath , []byte (contents ), fileMode ); err != nil {
87- log .Fatalf ("Could not write file %s: %v" , filePath , err )
105+ logger .Error ("Could not write file" , "filePath" , filePath , "error" , err )
106+ os .Exit (1 )
88107 }
89108
90109 if err := os .Chown (fqFilePath , fileUID , fileGID ); err != nil {
91- log .Fatalf ("Could not modify ownership of file %s: %v" , filePath , err )
110+ logger .Error ("Could not modify ownership of file" , "filePath" , filePath , "error" , err )
111+ os .Exit (1 )
92112 }
93113
94- log . Infof ("Successfully wrote file [%s] to device [%s]" , filePath , blockDevice )
114+ logger . Info ("Successfully wrote file" , "filePath" , filePath , "blockDevice" , blockDevice )
95115}
96116
97117func dirExists (mountPath , path string ) (bool , error ) {
0 commit comments