package main import ( "bufio" "fmt" "io" "io/ioutil" "os" "path" "strings" "github.com/rosti-cz/server_lobby/server" "github.com/shirou/gopsutil/v3/host" ) func getIdentification() (server.Discovery, error) { discovery := server.Discovery{} localLabels, err := loadLocalLabels() if err != nil { return discovery, err } if len(config.HostName) == 0 { info, err := host.Info() if err != nil { return discovery, err } discovery.Hostname = info.Hostname } else { discovery.Hostname = config.HostName } discovery.Labels = append(config.Labels, localLabels...) return discovery, nil } // loadLocalLabels scans local directory where labels are stored and adds them to the labels configured as environment variables. // Filename in LabelsPath is not importent and each file can contain multiple labels, one per each line. func loadLocalLabels() ([]string, error) { labels := []string{} if _, err := os.Stat(config.LabelsPath); !os.IsNotExist(err) { files, err := ioutil.ReadDir(config.LabelsPath) if err != nil { return labels, err } for _, filename := range files { fullPath := path.Join(config.LabelsPath, filename.Name()) fp, err := os.OpenFile(fullPath, os.O_RDONLY, os.ModePerm) if err != nil { return labels, fmt.Errorf("open file error: %v", err) } defer fp.Close() rd := bufio.NewReader(fp) for { line, err := rd.ReadString('\n') if err != nil { if err == io.EOF { break } return labels, fmt.Errorf("read file line error: %v", err) } line = strings.TrimSpace(line) if len(line) > 0 { labels = append(labels, line) } } } } return labels, nil }