diff --git a/local/local.go b/local/local.go index c119be7..cbcfe0e 100644 --- a/local/local.go +++ b/local/local.go @@ -4,18 +4,57 @@ import ( "fmt" "io/ioutil" "os" + "strings" ) func Open(address string) (string, error) { + if !pathExists(address) { + return "", fmt.Errorf("Invalid system path: %s", address) + } + file, err := os.Open(address) if err != nil { return "", fmt.Errorf("Unable to open file: %s", address) } defer file.Close() + + if pathIsDir(address) { + fileList, err := file.Readdirnames(0) + if err != nil { + return "", fmt.Errorf("Unable to read from directory: %s", address) + } + var out strings.Builder + out.WriteString(fmt.Sprintf("Current directory: %s\n\n", address)) + for _, obj := range fileList { + out.WriteString(obj) + out.WriteString("\n") + } + return out.String(), nil + } + bytes, err := ioutil.ReadAll(file) if err != nil { return "", fmt.Errorf("Unable to read file: %s", address) } return string(bytes), nil } + + +func pathExists(p string) bool { + exists := true + + if _, err := os.Stat(p); os.IsNotExist(err) { + exists = false + } + + return exists +} + +func pathIsDir(p string) bool { + info, err := os.Stat(p) + if err != nil { + return false + } + return info.IsDir() +} diff --git a/url.go b/url.go index 1850887..21c140c 100644 --- a/url.go +++ b/url.go @@ -130,3 +130,4 @@ func MakeUrl(u string) (Url, error) { return out, nil } +