-
Notifications
You must be signed in to change notification settings - Fork 254
/
util.go
57 lines (50 loc) · 1.6 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
46
47
48
49
50
51
52
53
54
55
56
57
package importer
import (
"io/ioutil"
"net/url"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/pkg/errors"
"k8s.io/klog/v2"
"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
}
// GetTerminationChannel returns a channel that listens for SIGTERM
func GetTerminationChannel() <-chan os.Signal {
terminationChannel := make(chan os.Signal, 1)
signal.Notify(terminationChannel, os.Interrupt, syscall.SIGTERM)
return terminationChannel
}
// newTerminationChannel should be overriden for unit tests
var newTerminationChannel = GetTerminationChannel