2020-01-13 05:13:26 +00:00
|
|
|
package utils
|
|
|
|
|
2020-01-13 07:11:53 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-23 07:18:06 +00:00
|
|
|
"fmt"
|
2020-01-13 07:11:53 +00:00
|
|
|
"os"
|
2020-05-23 07:18:06 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2020-01-13 07:11:53 +00:00
|
|
|
)
|
2020-01-13 05:13:26 +00:00
|
|
|
|
|
|
|
func DirWritable(path string) (bool, error) {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("path doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
return false, errors.New("path isn't a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().Perm()&(1<<(uint(7))) == 0 {
|
|
|
|
return false, errors.New("write permission bit is not set on this file for user")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-05-21 07:10:35 +00:00
|
|
|
|
|
|
|
func Ping(address string, secondsTimeout int) error {
|
|
|
|
ping, err := exec.LookPath("ping")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-24 00:32:51 +00:00
|
|
|
out, _, err := Command(ping, address, "-n 1", fmt.Sprintf("-w %v", secondsTimeout*1000))
|
2020-05-21 07:10:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "Destination Host Unreachable") {
|
|
|
|
return errors.New("destination host unreachable")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|