]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_info.go
Improve and add comments
[safcm/safcm.git] / cmd / safcm / sync_info.go
1 // "sync" sub-command: collect information from remote host
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 package main
19
20 import (
21         "fmt"
22         "regexp"
23         "strings"
24
25         "ruderich.org/simon/safcm"
26         "ruderich.org/simon/safcm/cmd/safcm/config"
27         "ruderich.org/simon/safcm/rpc"
28 )
29
30 func (s *Sync) hostInfo(conn *rpc.Conn) ([]string, error) {
31         x, err := s.sendRecv(conn, safcm.MsgInfoReq{
32                 LogLevel:     s.config.LogLevel,
33                 DetectGroups: s.config.DetectGroups,
34         })
35         if err != nil {
36                 return nil, err
37         }
38         resp, ok := x.(safcm.MsgInfoResp)
39         if !ok {
40                 return nil, fmt.Errorf("unexpected response %v", x)
41         }
42         if resp.Error != "" {
43                 return nil, fmt.Errorf("%s", resp.Error)
44         }
45         return hostInfoRespToGroups(resp), nil
46 }
47
48 // Keep in sync with cmd/safcm/config/groups.go:groupNameRegexp
49 var infoGroupDetectedRegexp = regexp.MustCompile(`[^a-z0-9_-]+`)
50
51 func hostInfoRespToGroups(resp safcm.MsgInfoResp) []string {
52         groups := []string{
53                 config.GroupDetectedPrefix + "_" + resp.Goos,
54                 config.GroupDetectedPrefix + "_" + resp.Goarch,
55         }
56         for _, x := range resp.Output {
57                 x = strings.TrimSpace(x)
58                 x = strings.ToLower(x)
59                 x = infoGroupDetectedRegexp.ReplaceAllString(x, "_")
60                 groups = append(groups, config.GroupDetectedPrefix+"_"+x)
61         }
62         return groups
63 }