mirror of
https://github.com/kubevirt/containerized-data-importer.git
synced 2025-06-03 06:30:22 +00:00

The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. [1]: https://golang.org/doc/go1.16#ioutil Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package importer
|
|
|
|
import (
|
|
"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 := os.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
|