Skip to content

Commit

Permalink
Emulate rm -f option
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Jan 20, 2020
1 parent 7770a63 commit 13626f8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,26 +269,32 @@ func (c CLI) Remove(args []string) error {
return errors.New("too few aruments")
}

var files []File
files := make([]File, len(args))
groupID := xid.New().String()

var eg errgroup.Group
for _, arg := range args {
_, err := os.Stat(arg)
if os.IsNotExist(err) {
return fmt.Errorf("%s: no such file or directory", arg)
}
file, err := makeFile(groupID, arg)
if err != nil {
return err
}
files = append(files, file)

for i, arg := range args {
i, arg := i, arg // https://golang.org/doc/faq#closures_and_goroutines
eg.Go(func() error {
_, err := os.Stat(arg)
if os.IsNotExist(err) {
return fmt.Errorf("%s: no such file or directory", arg)
}
file, err := makeFile(groupID, arg)
if err != nil {
return err
}
files[i] = file
os.MkdirAll(filepath.Dir(file.To), 0777)
return os.Rename(file.From, file.To)
})
}
defer c.Inventory.Save(files)

if c.Option.RmOption.Force {
return nil
}
return eg.Wait()
}

Expand Down

0 comments on commit 13626f8

Please sign in to comment.