]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - remote/sync/sync.go
Move implementation of cmd/safcm-remote/ to remote/
[safcm/safcm.git] / remote / sync / sync.go
diff --git a/remote/sync/sync.go b/remote/sync/sync.go
new file mode 100644 (file)
index 0000000..5287318
--- /dev/null
@@ -0,0 +1,96 @@
+// 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/remote/log"
+       "ruderich.org/simon/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.Logger
+}
+
+func Handle(req safcm.MsgSyncReq,
+       runner run.Runner, fun log.LogFunc) safcm.MsgSyncResp {
+
+       s := &Sync{
+               req: req,
+               log: log.NewLogger(fun),
+       }
+       s.cmd = run.NewCmd(runner, s.log)
+
+       err := s.setDefaults()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("%v", err)
+               return s.resp
+       }
+
+       err = s.syncFiles()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("files: %v", err)
+               return s.resp
+       }
+       err = s.syncPackages()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("packages: %v", err)
+               return s.resp
+       }
+       err = s.syncServices()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("services: %v", err)
+               return s.resp
+       }
+       err = s.syncCommands()
+       if err != nil {
+               s.resp.Error = fmt.Sprintf("commands: %v", 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
+}