File: rm.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (42 lines) | stat: -rw-r--r-- 695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main

import (
	"errors"
	"fmt"
	"os"
)

func rm(paths []string, recursive bool, force bool) {
	expanded, client, err := getClientAndExpandedPaths(paths)
	if err != nil {
		fatal(err)
	}

	for _, p := range expanded {
		info, err := client.Stat(p)
		if err != nil {
			if force && os.IsNotExist(err) {
				continue
			}

			if pathErr, ok := err.(*os.PathError); ok {
				pathErr.Op = "remove"
			}

			fmt.Fprintln(os.Stderr, err)
			status = 1
			continue
		}

		if !recursive && info.IsDir() {
			fmt.Fprintln(os.Stderr, &os.PathError{"remove", p, errors.New("file is a directory")})
			status = 1
			continue
		}

		err = client.RemoveAll(p)
		if err != nil {
			fatal(err)
		}
	}
}