6fc18144be71202fa28e320c628b205dbb3b3e0f
[src/xds/xds-server.git] / lib / xdsconfig / builderconfig.go
1 package xdsconfig
2
3 import (
4         "errors"
5         "net"
6 )
7
8 // BuilderConfig represents the builder container configuration
9 type BuilderConfig struct {
10         IP          string `json:"ip"`
11         Port        string `json:"port"`
12         SyncThingID string `json:"syncThingID"`
13 }
14
15 // NewBuilderConfig creates a new BuilderConfig instance
16 func NewBuilderConfig(stID string) (BuilderConfig, error) {
17         // Do we really need it ? may be not accessible from client side
18         ip, err := getLocalIP()
19         if err != nil {
20                 return BuilderConfig{}, err
21         }
22
23         b := BuilderConfig{
24                 IP:          ip, // TODO currently not used
25                 Port:        "", // TODO currently not used
26                 SyncThingID: stID,
27         }
28         return b, nil
29 }
30
31 /*** Private ***/
32
33 func getLocalIP() (string, error) {
34         addrs, err := net.InterfaceAddrs()
35         if err != nil {
36                 return "", err
37         }
38         for _, address := range addrs {
39                 // check the address type and if it is not a loopback the display it
40                 if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
41                         if ipnet.IP.To4() != nil {
42                                 return ipnet.IP.String(), nil
43                         }
44                 }
45         }
46         return "", errors.New("Cannot determined local IP")
47 }