-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathfilesystem_manipulation.go
61 lines (52 loc) · 1.21 KB
/
filesystem_manipulation.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
package peirates
import (
"fmt"
"os"
)
func displayFile(filePath string) error {
// Open the file
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed opening file: %w", err)
}
defer file.Close()
// Read the file content
content, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("failed reading file: %w", err)
}
// Print the content of the file
fmt.Println(string(content))
return nil
}
func listDirectory(dirPath string) error {
// Open the directory
dir, err := os.Open(dirPath)
if err != nil {
return fmt.Errorf("failed opening directory: %w", err)
}
defer dir.Close()
// Read the directory contents
files, err := dir.Readdir(-1)
if err != nil {
return fmt.Errorf("failed reading directory: %w", err)
}
// Print the names of the files and directories
for _, file := range files {
fmt.Println(file.Name())
}
return nil
}
func changeDirectory(dirPath string) error {
if err := os.Chdir(dirPath); err != nil {
return fmt.Errorf("failed to change directory: %w", err)
}
return nil
}
func getCurrentDirectory() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get current directory: %w", err)
}
return cwd, nil
}