mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: handle copy feat: handle remove feat: handle move and home ~ path refactor: improve error handling in cmd-hook feat: handlePwd
50 lines
895 B
Go
50 lines
895 B
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type FileType string
|
|
|
|
const (
|
|
FileTypeFile FileType = "file"
|
|
FileTypeDir FileType = "directory"
|
|
)
|
|
|
|
func resolvePath(path string) string {
|
|
if filepath.IsAbs(path) {
|
|
return path
|
|
}
|
|
if strings.HasPrefix(path, "~") {
|
|
homeDir := os.Getenv("HOME")
|
|
return filepath.Join(homeDir, path[1:])
|
|
}
|
|
return filepath.Join(os.Getenv("PWD"), path)
|
|
}
|
|
|
|
func fileExists(path string) (FileType, error) {
|
|
fileInfo, err := os.Stat(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if fileInfo.IsDir() {
|
|
return FileTypeDir, nil
|
|
}
|
|
return FileTypeFile, nil
|
|
}
|
|
|
|
func getFilePathList(cmd string) []string {
|
|
parts := strings.Fields(cmd)[1:]
|
|
results := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
if strings.HasPrefix(part, "-") {
|
|
continue
|
|
}
|
|
absPath := resolvePath(part)
|
|
results = append(results, absPath)
|
|
}
|
|
return results
|
|
}
|