-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.1.23 - added simple filesystem operations
- Loading branch information
Showing
3 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters