2022-04-08 17:44:40 +00:00
|
|
|
//go:build !windows
|
2020-02-23 08:48:26 +00:00
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-09-01 17:20:32 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-02-23 08:48:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetFileModeForPath(name string, mode os.FileMode) error {
|
|
|
|
return os.Chmod(name, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetFileModeForFile(file *os.File, mode os.FileMode) error {
|
|
|
|
return file.Chmod(mode)
|
|
|
|
}
|
2022-09-01 17:20:32 +00:00
|
|
|
|
|
|
|
// ReadFile reads from a file
|
|
|
|
func ReadFile(path string) (string, error) {
|
|
|
|
if path == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for start := time.Now(); time.Since(start) < 4*time.Minute; {
|
|
|
|
vpnBytes, err := os.ReadFile(path)
|
|
|
|
if err == nil {
|
|
|
|
return strings.TrimSpace(string(vpnBytes)), nil
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
logrus.Infof("Waiting for %s to be available\n", path)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("Timeout while trying to read the file")
|
|
|
|
}
|