2024-05-06 18:29:51 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2024-11-13 22:24:50 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-06 18:29:51 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-11-13 22:24:50 +00:00
|
|
|
var errInvalidDockerConfig = fmt.Errorf("invalid docker config")
|
|
|
|
|
2024-05-06 18:29:51 +00:00
|
|
|
func GetContainerIP() (string, error) {
|
|
|
|
netInterfaceAddrList, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, netInterfaceAddr := range netInterfaceAddrList {
|
|
|
|
netIP, ok := netInterfaceAddr.(*net.IPNet)
|
|
|
|
if ok && !netIP.IP.IsLoopback() && netIP.IP.To4() != nil {
|
|
|
|
return netIP.IP.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteDockerConf(path, conf string) error {
|
2024-11-13 22:24:50 +00:00
|
|
|
var jsonData interface{}
|
|
|
|
if err := json.Unmarshal([]byte(conf), &jsonData); err != nil {
|
|
|
|
return fmt.Errorf("%w: %w", errInvalidDockerConfig, err)
|
|
|
|
}
|
2024-05-06 18:29:51 +00:00
|
|
|
|
2024-11-13 22:24:50 +00:00
|
|
|
jsonBytes, err := json.Marshal(jsonData)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %w", errInvalidDockerConfig, err)
|
2024-05-06 18:29:51 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 22:24:50 +00:00
|
|
|
err = os.WriteFile(path, jsonBytes, strictFilePerm)
|
2024-05-06 18:29:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|