forked from struckchure/gv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
80 lines (66 loc) · 1.87 KB
/
utils.go
File metadata and controls
80 lines (66 loc) · 1.87 KB
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
package gv
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
func CopyFile(src, dst string) error {
// Create directory structure for the destination file if it doesn't exist
dstDir := filepath.Dir(dst)
if err := os.MkdirAll(dstDir, 0755); err != nil {
return fmt.Errorf("failed to create destination directory: %w", err)
}
// Open source file
sourceFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %w", err)
}
defer sourceFile.Close()
// Create destination file
destFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer destFile.Close()
// Copy data
_, err = io.Copy(destFile, sourceFile)
if err != nil {
return fmt.Errorf("failed to copy data: %w", err)
}
// Ensure data is written to disk
err = destFile.Sync()
if err != nil {
return fmt.Errorf("failed to flush to disk: %w", err)
}
return nil
}
// ExecCommand executes a command with arguments and returns the combined output
func ExecCommand(command string, args []string) (string, error) {
cmd := exec.Command(command, args...)
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &output
err := cmd.Run()
if err != nil {
return output.String(), fmt.Errorf("command failed: %w", err)
}
return output.String(), nil
}
// ExecCommandWithCallback executes a command and calls the callback with the output
func ExecCommandWithCallback(command string, args []string, callback func(output string)) error {
output, err := ExecCommand(command, args)
callback(output)
return err
}
// ExecStringCommand executes a command from a string with space-separated arguments
func ExecStringCommand(cmdString string) (string, error) {
parts := strings.Fields(cmdString)
if len(parts) == 0 {
return "", fmt.Errorf("empty command")
}
return ExecCommand(parts[0], parts[1:])
}