-
Notifications
You must be signed in to change notification settings - Fork 258
/
util.go
45 lines (40 loc) · 1.21 KB
/
util.go
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
43
44
45
package importer
import (
"io/ioutil"
"net/url"
"os"
"path/filepath"
"github.com/pkg/errors"
"k8s.io/klog"
"kubevirt.io/containerized-data-importer/pkg/common"
"kubevirt.io/containerized-data-importer/pkg/util"
)
// ParseEndpoint parses the required endpoint and return the url struct.
func ParseEndpoint(endpt string) (*url.URL, error) {
if endpt == "" {
// Because we are passing false, we won't decode anything and there is no way to error.
endpt, _ = util.ParseEnvVar(common.ImporterEndpoint, false)
if endpt == "" {
return nil, errors.Errorf("endpoint %q is missing or blank", common.ImporterEndpoint)
}
}
return url.Parse(endpt)
}
// CleanDir cleans the contents of a directory including its sub directories, but does NOT remove the
// directory itself.
func CleanDir(dest string) error {
dir, err := ioutil.ReadDir(dest)
if err != nil {
klog.Errorf("Unable read directory to clean: %s, %v", dest, err)
return err
}
for _, d := range dir {
klog.V(1).Infoln("deleting file: " + filepath.Join(dest, d.Name()))
err = os.RemoveAll(filepath.Join(dest, d.Name()))
if err != nil {
klog.Errorf("Unable to delete file: %s, %v", filepath.Join(dest, d.Name()), err)
return err
}
}
return nil
}