@@ -4,11 +4,11 @@ import (
44 "crypto/sha256"
55 "fmt"
66 "io"
7+ "log/slog"
78 "net/http"
89 "os"
910
1011 progressbar "github.com/schollz/progressbar/v3"
11- log "github.com/sirupsen/logrus"
1212
1313 "github.com/eikendev/hackenv/internal/handling"
1414 "github.com/eikendev/hackenv/internal/images"
@@ -23,18 +23,18 @@ type GetCommand struct {
2323
2424// https://golang.org/pkg/crypto/sha256/#example_New_file
2525func calculateFileChecksum (path string ) (string , error ) {
26- log . Printf ("Calculating checksum of %s \n " , path )
26+ slog . Info ("Calculating checksum" , "path " , path )
2727
2828 f , err := os .Open (path ) //#nosec G304
2929 if err != nil {
30- log . Errorf ("Failed to open file: %s \n " , err )
30+ slog . Error ("Failed to open file" , "path" , path , "err " , err )
3131 return "" , err
3232 }
3333 defer handling .Close (f )
3434
3535 h := sha256 .New ()
3636 if _ , err := io .Copy (h , f ); err != nil {
37- log . Errorf ("Failed to copy file content: %s \n " , err )
37+ slog . Error ("Failed to copy file content" , "path" , path , "err " , err )
3838 return "" , err
3939 }
4040
@@ -43,18 +43,18 @@ func calculateFileChecksum(path string) (string, error) {
4343
4444// https://stackoverflow.com/a/11693049
4545func downloadImage (path , url string ) error {
46- log . Printf ("Downloading image to %s \n " , path )
46+ slog . Info ("Downloading image" , "path" , path , "url" , url )
4747
4848 out , err := os .Create (path ) //#nosec G304
4949 if err != nil {
50- log . Errorf ("Cannot write image file: %s \n " , err )
50+ slog . Error ("Cannot create image file" , "path" , path , "err " , err )
5151 return err
5252 }
5353 defer handling .Close (out )
5454
5555 resp , err := http .Get (url ) //#nosec G107
5656 if err != nil {
57- log . Errorf ("Cannot download image file: %s \n " , err )
57+ slog . Error ("Cannot download image file" , "url" , url , "err " , err )
5858 return err
5959 }
6060 if resp == nil {
@@ -63,7 +63,7 @@ func downloadImage(path, url string) error {
6363 defer handling .Close (resp .Body )
6464
6565 if resp .StatusCode != http .StatusOK {
66- log . Errorf ("Cannot download image file: bad status %s \n " , resp .Status )
66+ slog . Error ("Cannot download image file: bad status" , "status " , resp .Status )
6767 return err
6868 }
6969
@@ -74,11 +74,11 @@ func downloadImage(path, url string) error {
7474
7575 _ , err = io .Copy (io .MultiWriter (out , bar ), resp .Body )
7676 if err != nil {
77- log . Errorf ("Cannot write image file: %s \n " , err )
77+ slog . Error ("Cannot write image file" , "path" , path , "err " , err )
7878 return err
7979 }
8080
81- log . Println ("Download successful" )
81+ slog . Info ("Download successful" )
8282 return nil
8383}
8484
@@ -89,17 +89,17 @@ func validateChecksum(localPath, checksum string) error {
8989 }
9090
9191 if newChecksum != checksum {
92- checksumMsg := fmt .Sprintf ("Downloaded image has bad checksum: %s instead of %s" , newChecksum , checksum )
93-
9492 err := os .Remove (localPath )
9593 if err != nil {
96- log .Fatalf ("%s. Unable to remove file.\n " , checksumMsg )
94+ slog .Error ("Downloaded image has bad checksum and cannot be removed" , "path" , localPath , "expected" , checksum , "actual" , newChecksum , "err" , err )
95+ os .Exit (1 )
9796 }
9897
99- log .Fatalf ("%s. File removed.\n " , checksumMsg )
98+ slog .Error ("Downloaded image has bad checksum and was removed" , "path" , localPath , "expected" , checksum , "actual" , newChecksum )
99+ os .Exit (1 )
100100 }
101101
102- log . Println ("Checksum validated successfully" )
102+ slog . Info ("Checksum validated successfully" )
103103 return nil
104104}
105105
@@ -111,7 +111,7 @@ func (c *GetCommand) Run(s *options.Options) error {
111111 return fmt .Errorf ("failed to get download information" )
112112 }
113113
114- log . Printf ("Found file %s with checksum %s \n " , info .Filename , info .Checksum )
114+ slog . Info ("Found image to download" , "filename" , info .Filename , "checksum" , info .Checksum )
115115
116116 localPath := image .GetLocalPath (info .Version )
117117
@@ -120,18 +120,19 @@ func (c *GetCommand) Run(s *options.Options) error {
120120 // The image already exists.
121121
122122 if ! c .Update && ! c .Force {
123- log . Println ("An image is already installed; update with --update" )
123+ slog . Info ("An image is already installed; use --update to refresh " )
124124 return nil
125125 }
126126
127127 localVersion := image .FileVersion (localPath )
128128
129129 if ! c .Force && image .VersionComparer .Eq (info .Version , localVersion ) {
130- log . Println ("Latest image is already installed; force with --force" )
130+ slog . Info ("Latest image is already installed; use --force to overwrite " )
131131 return nil
132132 }
133133 } else if ! os .IsNotExist (err ) {
134- log .Fatalf ("Unable to get file information for path %s\n " , localPath )
134+ slog .Error ("Unable to get file information" , "path" , localPath , "err" , err )
135+ os .Exit (1 )
135136 }
136137
137138 err := downloadImage (localPath , image .ArchiveURL + "/" + info .Filename )
@@ -144,7 +145,7 @@ func (c *GetCommand) Run(s *options.Options) error {
144145 return err
145146 }
146147
147- log .Info ("When using SELinux, don't forget to label the image with the fix command before proceeding" )
148+ slog .Info ("When using SELinux, label the image with the fix command before proceeding" )
148149
149150 return nil
150151}
0 commit comments