Skip to content

Commit

Permalink
add delete operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lylex committed Nov 29, 2018
1 parent 44aaa3e commit e382b1f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
36 changes: 23 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ const (
RootCmdName = "drm"

// TODO replate it
TempFileStorePath = "/home/lylex/workspace/drm/temp"
TempFileStorePath = "/Users/xuq3/workspace/drm/tem"
)

var (
isForce bool
isRecursive bool
)

func init() {
RootCmd.Flags().BoolVarP(&isRecursive, "recursive", "r", false, `ignore
nonexistent files and arguments, never prompt`)
RootCmd.Flags().BoolVarP(&isForce, "force", "f", false, `remove directories
and their contents recursively or not`)
}

// RootCmd represents the base command.
// Actually, we do not valid the sub-command here since we need to execute something
// like:
Expand All @@ -35,10 +42,20 @@ var RootCmd = &cobra.Command{
Long: `This application is used to rm files with a latency.`,
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
for _, item := range args {
dir := files.GetWd()
currentPath := filepath.Join(dir, item)
fmt.Println(currentPath)
for _, path := range args {
if !files.IsAbsolutePath(path) {
path = filepath.Join(files.GetWd(), path)
}
if files.IsDir(path) && !isRecursive {
utils.ErrExit(fmt.Sprintf("%s: %s: is a directory\n", RootCmdName, path), nil)
}
if !files.IsExist(path) {
if isForce {
continue
}
utils.ErrExit(fmt.Sprintf("%s: %s: No such file or directory\n", RootCmdName, path), nil)
}
files.Move(path, filepath.Join(TempFileStorePath, files.Name(path)))
}
},
}
Expand All @@ -47,13 +64,6 @@ var RootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
utils.PrintErr("Failed to execute command: %+v\n", err)
utils.ErrExit("Failed to execute command: %+v\n", err)
}
}

func init() {
RootCmd.Flags().BoolVarP(&isRecursive, "recursive", "r", false, `ignore
nonexistent files and arguments, never prompt`)
RootCmd.Flags().BoolVarP(&isForce, "force", "f", false, `remove directories
and their contents recursively or not`)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/lylex/drm

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/nsf/gocode v0.0.0-20181120081338-6cac7c69a41e // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/nsf/gocode v0.0.0-20181120081338-6cac7c69a41e h1:ihaECqr05sBOt6hJAEIHzbE2RF47BK34Z0LbQnxDiYw=
github.com/nsf/gocode v0.0.0-20181120081338-6cac7c69a41e/go.mod h1:6Q8/OMaaKAgTX7/jt2bOXVDrm1eJhoNd+iwzghR7jvs=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
Expand Down
39 changes: 38 additions & 1 deletion pkg/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func GetWd() string {
dir, err := os.Getwd()
if err != nil {
utils.PrintErr("error getting current directory: %s", err)
utils.ErrExit("error getting current directory: %s\n", err)
}

return dir
Expand All @@ -23,3 +23,40 @@ func GetWd() string {
func IsAbsolutePath(path string) bool {
return strings.HasPrefix(path, "/")
}

// IsExist is used to assert whether a file or directory in the path exists or not.
func IsExist(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
utils.ErrExit("error getting file info: %s\n", err)
}
return true
}

// IsDir is used to assert whether the path is a directory or not.
func IsDir(path string) bool {
return getFileInfo(path).IsDir()
}

// Move is used to move a file or directory from a absolute path to another.
// If the file name differs, it can also act as changing filename.
func Move(src, dir string) {
if err := os.Rename(src, dir); err != nil {
utils.ErrExit("error moving file or directory: %s\n", err)
}
}

// Name is used to retrieve the name of the file or directory of the pointed path.
func Name(path string) string {
return getFileInfo(path).Name()
}

func getFileInfo(path string) os.FileInfo {
fileInfo, err := os.Stat(path)
if err != nil {
utils.ErrExit("error getting file info: %s\n", err)
}
return fileInfo
}
9 changes: 7 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import (
"os"
)

func PrintErr(msg string, err error) {
fmt.Fprintf(os.Stderr, msg, err)
// ErrExit is used to print error to stderr, and then exist with code 1.
func ErrExit(msg string, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, msg, err)
} else {
fmt.Fprintf(os.Stderr, msg)
}
os.Exit(1)
}

0 comments on commit e382b1f

Please sign in to comment.