Skip to content

Commit

Permalink
mv applet.
Browse files Browse the repository at this point in the history
  • Loading branch information
udhos committed Apr 29, 2019
1 parent 66aef97 commit c2dcfb7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
7 changes: 1 addition & 6 deletions applets/cp/run.go
Expand Up @@ -49,19 +49,14 @@ func usage(flagSet *flag.FlagSet) {
flagSet.PrintDefaults()
}

func isDir(path string) bool {
stat, errStat := os.Stat(path)
return errStat == nil && stat.IsDir()
}

func cp(recursive bool, list []string) error {
if len(list) < 2 {
return fmt.Errorf("cp: missing operand")
}

last := len(list) - 1
dst := list[last]
dstDir := isDir(dst)
dstDir := common.IsDir(dst)

srcMultiple := len(list) > 2
if srcMultiple {
Expand Down
90 changes: 90 additions & 0 deletions applets/mv/run.go
@@ -0,0 +1,90 @@
package mv

import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/udhos/conbox/common"
)

// Run executes the applet.
func Run(tab map[string]common.AppletFunc, args []string) int {

flagSet := flag.NewFlagSet("mv", flag.ContinueOnError)
help := flagSet.Bool("h", false, "Show command-line help")

if err := flagSet.Parse(args); err != nil {
usage(flagSet)
return 1 // exit status
}

if *help {
usage(flagSet)
return 2 // exit status
}

if flagSet.NArg() < 2 {
fmt.Println("mv: missing operand")
usage(flagSet)
return 3
}

list := flagSet.Args()

if errMv := mv(list); errMv != nil {
fmt.Printf("mv: %v\n", errMv)
return 4
}

return 0
}

func usage(flagSet *flag.FlagSet) {
common.ShowVersion()
fmt.Println("mv [OPTION]... SOURCE... DESTINATION")
flagSet.PrintDefaults()
}

func mv(list []string) error {
if len(list) < 2 {
return fmt.Errorf("mv: missing operand")
}

last := len(list) - 1
dst := list[last]
dstDir := common.IsDir(dst)

srcMultiple := len(list) > 2
if srcMultiple {
if !dstDir {
// create dst dir
if errMkdir := os.Mkdir(dst, 0777); errMkdir != nil {
return errMkdir
}
dstDir = true
}
}

var errLast error
for i := 0; i < last; i++ {
src := list[i]
var dstFull string
if dstDir {
dstFull = filepath.Join(dst, filepath.Base(src))
} else {
dstFull = dst
}
errMv := mvFile(src, dstFull)
if errMv != nil {
fmt.Printf("mv: %v\n", errMv)
errLast = errMv
}
}
return errLast
}

func mvFile(src, dst string) error {
return os.Rename(src, dst)
}
7 changes: 7 additions & 0 deletions common/common.go
Expand Up @@ -2,6 +2,7 @@ package common

import (
"fmt"
"os"
"runtime"
"strings"
)
Expand All @@ -20,3 +21,9 @@ func ShowVersion() {
func Tokenize(line string) []string {
return strings.Fields(line) // FIXME WRITEME
}

// IsDir reports if path exists as directory.
func IsDir(path string) bool {
stat, errStat := os.Stat(path)
return errStat == nil && stat.IsDir()
}
2 changes: 2 additions & 0 deletions conbox/applets.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/udhos/conbox/applets/echo"
"github.com/udhos/conbox/applets/ls"
"github.com/udhos/conbox/applets/mkdir"
"github.com/udhos/conbox/applets/mv"
"github.com/udhos/conbox/applets/printenv"
"github.com/udhos/conbox/applets/ps"
"github.com/udhos/conbox/applets/pwd"
Expand All @@ -24,6 +25,7 @@ func loadApplets() map[string]common.AppletFunc {
"echo": echo.Run,
"ls": ls.Run,
"mkdir": mkdir.Run,
"mv": mv.Run,
"printenv": printenv.Run,
"pwd": pwd.Run,
"ps": ps.Run,
Expand Down

0 comments on commit c2dcfb7

Please sign in to comment.