]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm-remote/sync/sync.go
First working version
[safcm/safcm.git] / cmd / safcm-remote / sync / sync.go
diff --git a/cmd/safcm-remote/sync/sync.go b/cmd/safcm-remote/sync/sync.go
new file mode 100644 (file)
index 0000000..6f1cb78
--- /dev/null
@@ -0,0 +1,98 @@
+// MsgSyncReq: sync data on the remote host
+
+// Copyright (C) 2021  Simon Ruderich
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+package sync
+
+import (
+       "fmt"
+       "os/user"
+
+       "ruderich.org/simon/safcm"
+       "ruderich.org/simon/safcm/cmd/safcm-remote/log"
+       "ruderich.org/simon/safcm/cmd/safcm-remote/run"
+)
+
+type Sync struct {
+       req  safcm.MsgSyncReq
+       resp safcm.MsgSyncResp
+
+       defaultUser  string
+       defaultGroup string
+
+       triggers       []string
+       triggersActive map[string]bool
+
+       cmd *run.Cmd
+       log *log.PrefixLogger
+}
+
+const logPrefix = "sync remote:"
+
+func Handle(req safcm.MsgSyncReq,
+       runner run.Runner, fun log.LogFunc) safcm.MsgSyncResp {
+
+       s := &Sync{
+               req: req,
+               log: log.NewLogger(logPrefix, fun),
+       }
+       s.cmd = run.NewCmd(runner, s.log)
+
+       err := s.setDefaults()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("%s %s", logPrefix, err)
+               return s.resp
+       }
+
+       err = s.syncFiles()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("%s files: %s", logPrefix, err)
+               return s.resp
+       }
+       err = s.syncPackages()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("%s packages: %s", logPrefix, err)
+               return s.resp
+       }
+       err = s.syncServices()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("%s services: %s", logPrefix, err)
+               return s.resp
+       }
+       err = s.syncCommands()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("%s commands: %s", logPrefix, err)
+               return s.resp
+       }
+       return s.resp
+}
+
+func (s *Sync) setDefaults() error {
+       u, err := user.Current()
+       if err != nil {
+               return err
+       }
+       s.defaultUser = u.Username
+       g, err := user.LookupGroupId(u.Gid)
+       if err != nil {
+               return err
+       }
+       s.defaultGroup = g.Name
+
+       s.triggersActive = make(map[string]bool)
+
+       return nil
+}