]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/info/info.go
Use SPDX license identifiers
[safcm/safcm.git] / remote / info / info.go
1 // MsgInfoReq: collect information about the remote host
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package info
7
8 import (
9         "fmt"
10         "runtime"
11
12         "ruderich.org/simon/safcm"
13         "ruderich.org/simon/safcm/remote/log"
14         "ruderich.org/simon/safcm/remote/run"
15 )
16
17 type Info struct {
18         req  safcm.MsgInfoReq
19         resp safcm.MsgInfoResp
20
21         cmd    *run.Cmd
22         logger *log.Logger
23 }
24
25 func Handle(req safcm.MsgInfoReq,
26         runner run.Runner, fun log.LogFunc) safcm.MsgInfoResp {
27
28         i := Info{
29                 req:    req,
30                 logger: log.NewLogger(fun),
31         }
32         i.cmd = run.NewCmd(runner, i.logger)
33
34         err := i.handle()
35         if err != nil {
36                 i.resp.Error = fmt.Sprintf("%v", err)
37         }
38         return i.resp
39 }
40
41 func (i *Info) handle() error {
42         i.resp.Goos = runtime.GOOS
43         i.resp.Goarch = runtime.GOARCH
44
45         for _, x := range i.req.DetectGroups {
46                 stdout, _, err := i.cmd.Run("detect group",
47                         "/bin/sh", "-c", x)
48                 if err != nil {
49                         return err
50                 }
51                 i.resp.Output = append(i.resp.Output, string(stdout))
52         }
53
54         return nil
55 }