Skip to content

Commit

Permalink
Add ls and pwd command
Browse files Browse the repository at this point in the history
  • Loading branch information
victpork committed Jan 17, 2018
1 parent 19ec43d commit b7cc346
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
58 changes: 58 additions & 0 deletions os/command/ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package command

import (
"fmt"
"sort"
"strings"

"github.com/mkishere/sshsyrup/os"
)

type ls struct{}

func init() {
os.RegisterCommand("ls", ls{})
}

func (cmd ls) GetHelp() string {
return ""
}

func (cmd ls) Where() string {
return "/bin/ls"
}

func (cmd ls) Exec(args []string, sys *os.System) int {
var path string
if len(args) > 0 {
path = args[0]
} else {
path = sys.Getcwd()
}
dirList, err := sys.FSys.ReadDir(path)
if err != nil {
fmt.Fprintf(sys.Out(), "ls: cannot access %v: No such file or directory\n", path)
return 1
}
// Sort directory list
dirName := make([]string, 0, len(dirList))
maxlen := 0
for k := range dirList {
if len(k) > maxlen {
maxlen = len(k)
}
dirName = append(dirName, k)
}
sort.Strings(dirName)

itemPerRow := int(sys.Width / (maxlen + 1))

for i := 0; i < len(dirName); i++ {
fmt.Fprintf(sys.Out(), "%v%v ", dirName[i], strings.Repeat(" ", maxlen-len(dirName[i])))
if (i+1)%itemPerRow == 0 {
fmt.Fprint(sys.Out(), "\n")
}
}
fmt.Fprint(sys.Out(), "\n")
return 0
}
26 changes: 26 additions & 0 deletions os/command/pwd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package command

import (
"fmt"

"github.com/mkishere/sshsyrup/os"
)

type pwd struct{}

func init() {
os.RegisterCommand("pwd", pwd{})
}

func (p pwd) GetHelp() string {
return ""
}

func (p pwd) Exec(args []string, sys *os.System) int {
fmt.Fprintln(sys.Out(), sys.Getcwd())
return 0
}

func (p pwd) Where() string {
return "/bin/pwd"
}

0 comments on commit b7cc346

Please sign in to comment.