-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
129 lines (114 loc) Β· 3.18 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package files
import (
"fmt"
"github.com/pterm/pterm"
"io"
"io/fs"
"net/http"
"os"
"os/exec"
)
type FileMode = fs.FileMode
// Function to check if a file exists
func FileExists(filename string) bool {
info, err := os.Stat(filename)
return !os.IsNotExist(err) && !info.IsDir()
}
// Function to check if a file exists and removes it if it does
func RemoveFile(path string) {
if _, err := os.Stat(path); err == nil {
err = os.Remove(path)
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to remove %s file: %v", path, err))
os.Exit(1)
}
}
}
// Function to copy a file to a directory
func CopyFile(fileToCopy, destDir string) {
err := exec.Command("cp", fileToCopy, destDir).Run()
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to copy %s file: %v", fileToCopy, err))
os.Exit(1)
}
}
// Function to set owner and group of a file
func SetOwnerAndGroup(owner, group, file string) {
err := exec.Command("chown", fmt.Sprintf("%s:%s", owner, group), file).Run()
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to set ownership of %s file: %v", file, err))
os.Exit(1)
}
}
// Function to set permissions of a file
func SetPermissions(path string, mode FileMode) {
err := os.Chmod(path, mode)
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to set %s file permissions: %v", path, err))
os.Exit(1)
}
}
// Function to write content to a file
func WriteFile(path, content string, permissions FileMode) {
err := os.WriteFile(path, []byte(content), permissions)
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to write content to %s file: %v", path, err))
os.Exit(1)
}
}
// Function to perform in-place editing
func InPlaceEdit(command, path string) {
cmd := exec.Command("sed", "-i", command, path)
// Execute the command
if err := cmd.Run(); err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to edit %s file in-place: %v", path, err))
os.Exit(1)
}
}
// Function to download and copy a file
func DownloadAndCopyFile(tmpFilePath, downloadURL string) {
// Create a temporary file
out, err := os.Create(tmpFilePath)
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to create %s file: %v", tmpFilePath, err))
os.Exit(1)
}
defer out.Close()
// Download the file
resp, err := http.Get(downloadURL)
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to download file: %v", err))
os.Exit(1)
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Bad repsonse status code: %s", resp.Status))
os.Exit(1)
}
// Write the body to the temporary file
_, err = io.Copy(out, resp.Body)
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to write to temporary file: %v", err))
os.Exit(1)
}
}
// Function to extract a file
func ExtractFile(tmpFilePath, destDir string) {
err := exec.Command("tar", "-xf", tmpFilePath, "-C", destDir).Run()
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to extract binary to %s: %v", destDir, err))
os.Exit(1)
}
}