@@ -2,9 +2,9 @@ package files
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/pterm/pterm"
5
6
"io"
6
7
"io/fs"
7
- "log"
8
8
"net/http"
9
9
"os"
10
10
"os/exec"
@@ -23,7 +23,9 @@ func RemoveFile(path string) {
23
23
if _ , err := os .Stat (path ); err == nil {
24
24
err = os .Remove (path )
25
25
if err != nil {
26
- log .Fatalf ("Error removing %s file: %v" , path , err )
26
+ pterm .Println ()
27
+ pterm .Error .Println (fmt .Sprintf ("Failed to remove %s file: %v" , path , err ))
28
+ os .Exit (1 )
27
29
}
28
30
}
29
31
}
@@ -32,31 +34,39 @@ func RemoveFile(path string) {
32
34
func CopyFile (fileToCopy , destDir string ) {
33
35
err := exec .Command ("cp" , fileToCopy , destDir ).Run ()
34
36
if err != nil {
35
- log .Fatalf ("Error copying %s file: %v" , fileToCopy , err )
37
+ pterm .Println ()
38
+ pterm .Error .Println (fmt .Sprintf ("Failed to copy %s file: %v" , fileToCopy , err ))
39
+ os .Exit (1 )
36
40
}
37
41
}
38
42
39
43
// Function to set owner and group of a file
40
44
func SetOwnerAndGroup (owner , group , file string ) {
41
45
err := exec .Command ("chown" , fmt .Sprintf ("%s:%s" , owner , group ), file ).Run ()
42
46
if err != nil {
43
- log .Fatalf ("Error setting ownership of %s file: %v" , file , err )
47
+ pterm .Println ()
48
+ pterm .Error .Println (fmt .Sprintf ("Failed to set ownership of %s file: %v" , file , err ))
49
+ os .Exit (1 )
44
50
}
45
51
}
46
52
47
53
// Function to set permissions of a file
48
54
func SetPermissions (path string , mode FileMode ) {
49
55
err := os .Chmod (path , mode )
50
56
if err != nil {
51
- log .Fatalf ("Error setting %s file permissions: %v" , path , err )
57
+ pterm .Println ()
58
+ pterm .Error .Println (fmt .Sprintf ("Failed to set %s file permissions: %v" , path , err ))
59
+ os .Exit (1 )
52
60
}
53
61
}
54
62
55
63
// Function to write content to a file
56
64
func WriteFile (path , content string , permissions FileMode ) {
57
65
err := os .WriteFile (path , []byte (content ), permissions )
58
66
if err != nil {
59
- log .Fatalf ("Error writing content to %s file: %v" , path , err )
67
+ pterm .Println ()
68
+ pterm .Error .Println (fmt .Sprintf ("Failed to write content to %s file: %v" , path , err ))
69
+ os .Exit (1 )
60
70
}
61
71
}
62
72
@@ -66,7 +76,9 @@ func InPlaceEdit(command, path string) {
66
76
67
77
// Execute the command
68
78
if err := cmd .Run (); err != nil {
69
- log .Fatalf ("Error editing %s in-place: %v" , path , err )
79
+ pterm .Println ()
80
+ pterm .Error .Println (fmt .Sprintf ("Failed to edit %s file in-place: %v" , path , err ))
81
+ os .Exit (1 )
70
82
}
71
83
}
72
84
@@ -75,33 +87,43 @@ func DownloadAndCopyFile(tmpFilePath, downloadURL string) {
75
87
// Create a temporary file
76
88
out , err := os .Create (tmpFilePath )
77
89
if err != nil {
78
- log .Fatalf ("Error creating %s file: %v" , tmpFilePath , err )
90
+ pterm .Println ()
91
+ pterm .Error .Println (fmt .Sprintf ("Failed to create %s file: %v" , tmpFilePath , err ))
92
+ os .Exit (1 )
79
93
}
80
94
defer out .Close ()
81
95
82
96
// Download the file
83
97
resp , err := http .Get (downloadURL )
84
98
if err != nil {
85
- log .Fatalf ("Error downloading file: %v" , err )
99
+ pterm .Println ()
100
+ pterm .Error .Println (fmt .Sprintf ("Failed to download file: %v" , err ))
101
+ os .Exit (1 )
86
102
}
87
103
defer resp .Body .Close ()
88
104
89
105
// Check server response
90
106
if resp .StatusCode != http .StatusOK {
91
- log .Fatalf ("Bad status: %s" , resp .Status )
107
+ pterm .Println ()
108
+ pterm .Error .Println (fmt .Sprintf ("Bad repsonse status code: %s" , resp .Status ))
109
+ os .Exit (1 )
92
110
}
93
111
94
112
// Write the body to the temporary file
95
113
_ , err = io .Copy (out , resp .Body )
96
114
if err != nil {
97
- log .Fatalf ("Error writing to temporary file: %v" , err )
115
+ pterm .Println ()
116
+ pterm .Error .Println (fmt .Sprintf ("Failed to write to temporary file: %v" , err ))
117
+ os .Exit (1 )
98
118
}
99
119
}
100
120
101
121
// Function to extract a file
102
122
func ExtractFile (tmpFilePath , destDir string ) {
103
123
err := exec .Command ("tar" , "-xf" , tmpFilePath , "-C" , destDir ).Run ()
104
124
if err != nil {
105
- log .Fatalf ("Error extracting binary to %s: %v" , destDir , err )
125
+ pterm .Println ()
126
+ pterm .Error .Println (fmt .Sprintf ("Failed to extract binary to %s: %v" , destDir , err ))
127
+ os .Exit (1 )
106
128
}
107
129
}
0 commit comments