]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/sync.go
Move embedded remote helpers to cmd/safcm/
[safcm/safcm.git] / remote / sync / sync.go
1 // MsgSyncReq: sync data on the 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 sync
19
20 import (
21         "fmt"
22         "os/user"
23
24         "ruderich.org/simon/safcm"
25         "ruderich.org/simon/safcm/remote/log"
26         "ruderich.org/simon/safcm/remote/run"
27 )
28
29 type Sync struct {
30         req  safcm.MsgSyncReq
31         resp safcm.MsgSyncResp
32
33         defaultUser  string
34         defaultGroup string
35
36         triggers       []string
37         triggersActive map[string]bool
38
39         cmd *run.Cmd
40         log *log.Logger
41 }
42
43 func Handle(req safcm.MsgSyncReq,
44         runner run.Runner, fun log.LogFunc) safcm.MsgSyncResp {
45
46         s := &Sync{
47                 req: req,
48                 log: log.NewLogger(fun),
49         }
50         s.cmd = run.NewCmd(runner, s.log)
51
52         err := s.setDefaults()
53         if err != nil {
54                 s.resp.Error = fmt.Sprintf("%v", err)
55                 return s.resp
56         }
57
58         err = s.syncFiles()
59         if err != nil {
60                 s.resp.Error = fmt.Sprintf("files: %v", err)
61                 return s.resp
62         }
63         err = s.syncPackages()
64         if err != nil {
65                 s.resp.Error = fmt.Sprintf("packages: %v", err)
66                 return s.resp
67         }
68         err = s.syncServices()
69         if err != nil {
70                 s.resp.Error = fmt.Sprintf("services: %v", err)
71                 return s.resp
72         }
73         err = s.syncCommands()
74         if err != nil {
75                 s.resp.Error = fmt.Sprintf("commands: %v", err)
76                 return s.resp
77         }
78         return s.resp
79 }
80
81 func (s *Sync) setDefaults() error {
82         u, err := user.Current()
83         if err != nil {
84                 return err
85         }
86         s.defaultUser = u.Username
87         g, err := user.LookupGroupId(u.Gid)
88         if err != nil {
89                 return err
90         }
91         s.defaultGroup = g.Name
92
93         s.triggersActive = make(map[string]bool)
94
95         return nil
96 }