]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_info.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / sync_info.go
1 // "sync" sub-command: collect information from remote host
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package main
7
8 import (
9         "fmt"
10         "regexp"
11         "strings"
12
13         "ruderich.org/simon/safcm"
14         "ruderich.org/simon/safcm/cmd/safcm/config"
15         "ruderich.org/simon/safcm/rpc"
16 )
17
18 func (s *Sync) hostInfo(conn *rpc.Conn) ([]string, error) {
19         resp, err := s.loop.HostInfoMsg(s, conn, safcm.MsgInfoReq{
20                 LogLevel:     s.config.LogLevel,
21                 DetectGroups: s.config.DetectGroups,
22         })
23         if err != nil {
24                 return nil, err
25         }
26         if resp.Error != "" {
27                 return nil, fmt.Errorf("%s", resp.Error)
28         }
29         return hostInfoRespToGroups(resp), nil
30 }
31
32 // Keep in sync with cmd/safcm/config/groups.go:groupNameRegexp
33 var infoGroupDetectedRegexp = regexp.MustCompile(`[^a-z0-9_-]+`)
34
35 func hostInfoRespToGroups(resp safcm.MsgInfoResp) []string {
36         groups := []string{
37                 hostInfoDetectedGroupName(resp.Goos),
38                 hostInfoDetectedGroupName(resp.Goarch),
39         }
40         for _, x := range resp.Output {
41                 groups = append(groups, hostInfoDetectedGroupName(x))
42         }
43         return groups
44 }
45
46 func hostInfoDetectedGroupName(x string) string {
47         x = strings.TrimSpace(x)
48         x = strings.ToLower(x)
49         x = infoGroupDetectedRegexp.ReplaceAllString(x, "_")
50         x = config.GroupDetectedPrefix + "_" + x
51         return x
52 }