feat: handle command hooks

feat: handle copy

feat: handle remove

feat: handle move and home ~ path

refactor: improve error handling in cmd-hook

feat: handlePwd
This commit is contained in:
Justin Lin committed 2025-05-05 19:55:40 +08:00
1 parent d755822908
commit 95c4fb71ae
3 files changed
+127 -42

No files matched your search

+112 -36
View File
@@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@@ -26,12 +27,13 @@ type FileObject struct {
var commandHandlers = map[string]CommandHandler{ var commandHandlers = map[string]CommandHandler{
"ls": handleLs, "ls": handleLs,
"cd": handleCd, "cd": handleCd,
"touch": handleTouch, "touch": handleCreate,
"mkdir": handleMkdir, "mkdir": handleCreate,
"cp": handleCp, "cp": handleCopy,
"rm": handleRm, "rm": handleRemove,
"rmdir": handleRmdir, "rmdir": handleRemove,
"mv": handleMv, "mv": handleMove,
"pwd": handlePwd,
} }
func handleCommand(cmd string) (CommandParams, error) { func handleCommand(cmd string) (CommandParams, error) {
@@ -55,27 +57,30 @@ func handleCd(cmd string) (CommandParams, error) {
} }
func handleLs(cmd string) (CommandParams, error) { func handleLs(cmd string) (CommandParams, error) {
fmt.Println("handleLs", cmd) files := getFilePathList(cmd)
parts := strings.Fields(cmd)[1:] if len(files) == 0 {
path := "." files = append(files, ".")
if len(parts) > 1 {
path = parts[1]
} }
files := make([]FileObject, 0) results := make([]FileObject, 0)
err := discover(&files, path, 0) for _, path := range files {
if err != nil { err := discover(&results, path, 0)
return CommandParams{}, err if err != nil {
return CommandParams{}, err
}
} }
return CommandParams{ return CommandParams{
Discover: files, Discover: results,
}, nil }, nil
} }
func discover(files *[]FileObject, path string, level int) error { func discover(files *[]FileObject, path string, level int) error {
absPath := resolvePath(path) absPath := resolvePath(path)
fileType, err := fileExists(absPath) fileType, err := fileExists(absPath)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil { if err != nil {
return fmt.Errorf("ls: cannot access '%s': %v", path, err) return fmt.Errorf("ls: cannot access '%s': %v", path, err)
} }
@@ -111,32 +116,103 @@ func discover(files *[]FileObject, path string, level int) error {
return nil return nil
} }
func handleTouch(cmd string) (CommandParams, error) { func handleCreate(cmd string) (CommandParams, error) {
// TODO: Implement touch command files := getFilePathList(cmd)
return CommandParams{}, nil results := make([]FileObject, 0, len(files))
for _, absPath := range files {
fileType, err := fileExists(absPath)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return CommandParams{}, err
}
results = append(results, FileObject{
Path: absPath,
Type: fileType,
Discovered: true,
})
}
return CommandParams{Add: results}, nil
} }
func handleMkdir(cmd string) (CommandParams, error) { func handleCopy(cmd string) (CommandParams, error) {
// TODO: Implement mkdir command files := getFilePathList(cmd)
return CommandParams{}, nil if len(files) < 2 {
return CommandParams{}, nil
}
dest := files[len(files)-1]
results := make([]FileObject, 0)
discover(&results, dest, 0)
return CommandParams{Discover: results}, nil
} }
func handleCp(cmd string) (CommandParams, error) { func handleRemove(cmd string) (CommandParams, error) {
// TODO: Implement cp command files := getFilePathList(cmd)
return CommandParams{}, nil results := make([]FileObject, 0, len(files))
for _, absPath := range files {
_, err := fileExists(absPath)
// If the file exists, ignore it
if err == nil {
continue
}
if !errors.Is(err, os.ErrNotExist) {
return CommandParams{}, err
}
results = append(results, FileObject{
Path: absPath,
Type: FileTypeFile, // whatever
Discovered: true,
})
}
return CommandParams{Remove: results}, nil
} }
func handleRm(cmd string) (CommandParams, error) { func handleMove(cmd string) (CommandParams, error) {
// TODO: Implement rm command files := getFilePathList(cmd)
return CommandParams{}, nil if len(files) < 2 {
return CommandParams{}, nil
}
sources := files[:len(files)-1]
dest := files[len(files)-1]
result := CommandParams{
Add: make([]FileObject, 0, len(sources)),
Remove: make([]FileObject, 0, len(sources)),
}
for _, source := range sources {
_, err := fileExists(source)
if err == nil {
continue
}
if !errors.Is(err, os.ErrNotExist) {
return CommandParams{}, err
}
result.Remove = append(result.Remove, FileObject{
Path: source,
Type: FileTypeFile, // whatever
Discovered: true,
})
}
discover(&result.Add, dest, 0)
return result, nil
} }
func handleRmdir(cmd string) (CommandParams, error) { func handlePwd(cmd string) (CommandParams, error) {
// TODO: Implement rmdir command pwd := os.Getenv("PWD")
return CommandParams{}, nil parts := strings.Split(pwd, "/")
} currentPath := "/"
results := make([]FileObject, 0, len(parts))
func handleMv(cmd string) (CommandParams, error) { for _, part := range parts {
// TODO: Implement mv command currentPath = filepath.Join(currentPath, part)
return CommandParams{}, nil results = append(results, FileObject{
Path: currentPath,
Type: FileTypeDir,
Discovered: true,
})
}
return CommandParams{
Pwd: os.Getenv("PWD"),
Add: results,
}, nil
} }
-1
View File
@@ -61,7 +61,6 @@ func main() {
os.Exit(1) os.Exit(1)
} }
fmt.Println(string(jsonData))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/commands", backendUrl), bytes.NewBuffer(jsonData)) req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/commands", backendUrl), bytes.NewBuffer(jsonData))
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error creating request: %v\n", err) fmt.Fprintf(os.Stderr, "Error creating request: %v\n", err)
+15 -5
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
type FileType string type FileType string
@@ -16,6 +17,10 @@ func resolvePath(path string) string {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
return path return path
} }
if strings.HasPrefix(path, "~") {
homeDir := os.Getenv("HOME")
return filepath.Join(homeDir, path[1:])
}
return filepath.Join(os.Getenv("PWD"), path) return filepath.Join(os.Getenv("PWD"), path)
} }
@@ -30,10 +35,15 @@ func fileExists(path string) (FileType, error) {
return FileTypeFile, nil return FileTypeFile, nil
} }
func isDir(path string) bool { func getFilePathList(cmd string) []string {
info, err := os.Stat(path) parts := strings.Fields(cmd)[1:]
if err != nil { results := make([]string, 0, len(parts))
return false for _, part := range parts {
if strings.HasPrefix(part, "-") {
continue
}
absPath := resolvePath(part)
results = append(results, absPath)
} }
return info.IsDir() return results
} }