69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/fs"
|
|
"net"
|
|
"os"
|
|
"os/user"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------
|
|
// filesystem helpers
|
|
// ---------------------------------------------------------------------
|
|
|
|
func copyFile(src, dst string) error {
|
|
data, err := os.ReadFile(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(dst, data, 0o644)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// EN: `chownDev` contains core logic for chown dev.
|
|
// RU: `chownDev` - содержит основную логику для chown dev.
|
|
// ---------------------------------------------------------------------
|
|
func chownDev(paths ...string) {
|
|
usr, err := user.Lookup("dev")
|
|
if err != nil {
|
|
return
|
|
}
|
|
uid, _ := strconv.Atoi(usr.Uid)
|
|
gid, _ := strconv.Atoi(usr.Gid)
|
|
for _, p := range paths {
|
|
_ = os.Chown(p, uid, gid)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// EN: `chmodPaths` contains core logic for chmod paths.
|
|
// RU: `chmodPaths` - содержит основную логику для chmod paths.
|
|
// ---------------------------------------------------------------------
|
|
func chmodPaths(mode fs.FileMode, paths ...string) {
|
|
for _, p := range paths {
|
|
_ = os.Chmod(p, mode)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// readiness helpers
|
|
// ---------------------------------------------------------------------
|
|
|
|
func waitDNS(attempts int, delay time.Duration) error {
|
|
target := "openai.com"
|
|
for i := 0; i < attempts; i++ {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
_, err := net.DefaultResolver.LookupHost(ctx, target)
|
|
cancel()
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
time.Sleep(delay)
|
|
}
|
|
return fmt.Errorf("dns lookup failed after %d attempts", attempts)
|
|
}
|