Use go module as dependency tool instead of glide
[src/xds/xds-server.git] / lib / xdsconfig / builderconfig.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package xdsconfig
19
20 import (
21         "errors"
22         "net"
23
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
25 )
26
27 // NewBuilderConfig creates a new BuilderConfig instance
28 func NewBuilderConfig(stID string) (xsapiv1.BuilderConfig, error) {
29         // Do we really need it ? may be not accessible from client side
30         ip, err := getLocalIP()
31         if err != nil {
32                 return xsapiv1.BuilderConfig{}, err
33         }
34
35         b := xsapiv1.BuilderConfig{
36                 IP:          ip, // TODO currently not used
37                 Port:        "", // TODO currently not used
38                 SyncThingID: stID,
39         }
40         return b, nil
41 }
42
43 /*** Private ***/
44
45 func getLocalIP() (string, error) {
46         addrs, err := net.InterfaceAddrs()
47         if err != nil {
48                 return "", err
49         }
50         for _, address := range addrs {
51                 // check the address type and if it is not a loopback the display it
52                 if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
53                         if ipnet.IP.To4() != nil {
54                                 return ipnet.IP.String(), nil
55                         }
56                 }
57         }
58         return "", errors.New("Cannot determined local IP")
59 }