forked from sloum/bombadillo
Adds support for listing directories as well as getting file contents
This commit is contained in:
parent
484fb77aa3
commit
ee9fc8332c
|
@ -4,18 +4,57 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Open(address string) (string, error) {
|
func Open(address string) (string, error) {
|
||||||
|
if !pathExists(address) {
|
||||||
|
return "", fmt.Errorf("Invalid system path: %s", address)
|
||||||
|
}
|
||||||
|
|
||||||
file, err := os.Open(address)
|
file, err := os.Open(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Unable to open file: %s", address)
|
return "", fmt.Errorf("Unable to open file: %s", address)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
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)
|
bytes, err := ioutil.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Unable to read file: %s", address)
|
return "", fmt.Errorf("Unable to read file: %s", address)
|
||||||
}
|
}
|
||||||
return string(bytes), nil
|
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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue