]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_info.go
bfc606a182907395ca6ef1fc1cfc3d10a3565f0e
[safcm/safcm.git] / cmd / safcm / sync_info.go
1 // "sync" sub-command: collect information from remote host
2
3 // Copyright (C) 2021-2024  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         resp, err := s.loop.HostInfoMsg(s, conn, safcm.MsgInfoReq{
32                 LogLevel:     s.config.LogLevel,
33                 DetectGroups: s.config.DetectGroups,
34         })
35         if err != nil {
36                 return nil, err
37         }
38         if resp.Error != "" {
39                 return nil, fmt.Errorf("%s", resp.Error)
40         }
41         return hostInfoRespToGroups(resp), nil
42 }
43
44 // Keep in sync with cmd/safcm/config/groups.go:groupNameRegexp
45 var infoGroupDetectedRegexp = regexp.MustCompile(`[^a-z0-9_-]+`)
46
47 func hostInfoRespToGroups(resp safcm.MsgInfoResp) []string {
48         groups := []string{
49                 hostInfoDetectedGroupName(resp.Goos),
50                 hostInfoDetectedGroupName(resp.Goarch),
51         }
52         for _, x := range resp.Output {
53                 groups = append(groups, hostInfoDetectedGroupName(x))
54         }
55         return groups
56 }
57
58 func hostInfoDetectedGroupName(x string) string {
59         x = strings.TrimSpace(x)
60         x = strings.ToLower(x)
61         x = infoGroupDetectedRegexp.ReplaceAllString(x, "_")
62         x = config.GroupDetectedPrefix + "_" + x
63         return x
64 }