]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
Move implementation of cmd/safcm-remote/ to remote/
authorSimon Ruderich <simon@ruderich.org>
Tue, 18 May 2021 10:29:48 +0000 (12:29 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 18 May 2021 10:29:48 +0000 (12:29 +0200)
This permits users of the safcm library to easily "implement"
`safcm-remote` simply by calling safcm/remote.Main(). They cannot use
the existing cmd/safcm-remote/ because the source tree is not directly
available when importing the library.

27 files changed:
cmd/safcm-remote/main.go
cmd/safcm/config/files_test.go
cmd/safcm/fixperms.go
cmd/safcm/main_sync_test.go
remote/ainsl/ainsl.go [moved from cmd/safcm-remote/ainsl/ainsl.go with 98% similarity]
remote/ainsl/ainsl_test.go [moved from cmd/safcm-remote/ainsl/ainsl_test.go with 98% similarity]
remote/info/info.go [moved from cmd/safcm-remote/info/info.go with 93% similarity]
remote/log/logger.go [moved from cmd/safcm-remote/log/logger.go with 100% similarity]
remote/main.go [new file with mode: 0644]
remote/run/cmd.go [moved from cmd/safcm-remote/run/cmd.go with 98% similarity]
remote/run/runner.go [moved from cmd/safcm-remote/run/runner.go with 100% similarity]
remote/sync/commands.go [moved from cmd/safcm-remote/sync/commands.go with 98% similarity]
remote/sync/commands_test.go [moved from cmd/safcm-remote/sync/commands_test.go with 100% similarity]
remote/sync/files.go [moved from cmd/safcm-remote/sync/files.go with 100% similarity]
remote/sync/files_test.go [moved from cmd/safcm-remote/sync/files_test.go with 99% similarity]
remote/sync/files_windows.go [moved from cmd/safcm-remote/sync/files_windows.go with 100% similarity]
remote/sync/filetest/filetest.go [moved from cmd/safcm-remote/sync/filetest/filetest.go with 100% similarity]
remote/sync/packages.go [moved from cmd/safcm-remote/sync/packages.go with 100% similarity]
remote/sync/packages_debian.go [moved from cmd/safcm-remote/sync/packages_debian.go with 100% similarity]
remote/sync/packages_debian_test.go [moved from cmd/safcm-remote/sync/packages_debian_test.go with 100% similarity]
remote/sync/services.go [moved from cmd/safcm-remote/sync/services.go with 100% similarity]
remote/sync/services_systemd.go [moved from cmd/safcm-remote/sync/services_systemd.go with 100% similarity]
remote/sync/services_systemd_test.go [moved from cmd/safcm-remote/sync/services_systemd_test.go with 100% similarity]
remote/sync/sync.go [moved from cmd/safcm-remote/sync/sync.go with 95% similarity]
remote/sync/sync_test.go [moved from cmd/safcm-remote/sync/sync_test.go with 97% similarity]
remote/sync/triggers.go [moved from cmd/safcm-remote/sync/triggers.go with 100% similarity]
remote/sync/triggers_test.go [moved from cmd/safcm-remote/sync/triggers_test.go with 100% similarity]

index a29f20c171747ac6f3a72935c33a1ca6e162dd8a..b6d784d827697deca168cd6d38cb7ce6f646a355 100644 (file)
 package main
 
 import (
-       "fmt"
-       "log"
-       "os"
-
-       "golang.org/x/term"
-
-       "ruderich.org/simon/safcm"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/ainsl"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/info"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/run"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/sync"
+       "ruderich.org/simon/safcm/remote"
 )
 
-func usage() {
-       log.Fatalf("usage: %[1]s sync\n"+
-               "usage: %[1]s ainsl [options] <path> <line>",
-               os.Args[0])
-}
-
 func main() {
-       // Timestamps are added by `safcm`
-       log.SetFlags(0)
-
-       if len(os.Args) < 2 {
-               usage()
-       }
-
-       var err error
-       switch os.Args[1] {
-       case "sync":
-               if len(os.Args) != 2 {
-                       usage()
-               }
-               err = mainLoop()
-       case "ainsl":
-               err = ainsl.Main(os.Args)
-       default:
-               usage()
-       }
-
-       if err != nil {
-               log.Fatalf("%s: %v", os.Args[0], err)
-       }
-}
-
-func mainLoop() error {
-       if term.IsTerminal(int(os.Stdin.Fd())) ||
-               term.IsTerminal(int(os.Stdout.Fd())) {
-               return fmt.Errorf("sync should only be called from `safcm` " +
-                       "(redirect stdin/stdout to circumvent this check)")
-       }
-
-       conn := safcm.NewGobConn(os.Stdin, os.Stdout)
-
-       var logLevel safcm.LogLevel
-       logFunc := func(level safcm.LogLevel, msg string) {
-               if logLevel >= level {
-                       conn.Send(safcm.MsgLog{
-                               Level: level,
-                               Text:  msg,
-                       })
-               }
-       }
-
-       var quitResp safcm.MsgQuitResp
-       for {
-               x, err := conn.Recv()
-               if err != nil {
-                       return err
-               }
-
-               var resp safcm.Msg
-               switch x := x.(type) {
-               case safcm.MsgInfoReq:
-                       logLevel = x.LogLevel // set log level globally
-                       resp = info.Handle(x, run.ExecRunner{}, logFunc)
-               case safcm.MsgSyncReq:
-                       resp = sync.Handle(x, run.ExecRunner{}, logFunc)
-               case safcm.MsgQuitReq:
-                       resp = quitResp
-               default:
-                       return fmt.Errorf("unsupported message %#v", x)
-               }
-
-               err = conn.Send(resp)
-               if err != nil {
-                       return err
-               }
-               if resp == quitResp {
-                       break
-               }
-       }
-       return nil
+       remote.Main()
 }
index 7ad12d62f32c6516efa2736e770924dfdab2ce42..110c012a18125c3a50360586c631353547be1859 100644 (file)
@@ -23,7 +23,7 @@ import (
        "testing"
 
        "ruderich.org/simon/safcm"
-       ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+       ft "ruderich.org/simon/safcm/remote/sync/filetest"
        "ruderich.org/simon/safcm/testutil"
 )
 
index 57c081171ed3b71b9ce2937e309874e7ca5d3000..36d618f7fe1b12a0b2197f9186a0673f71c483ba 100644 (file)
@@ -24,8 +24,8 @@ import (
        "os"
        "path/filepath"
 
-       "ruderich.org/simon/safcm/cmd/safcm-remote/sync"
        "ruderich.org/simon/safcm/cmd/safcm/config"
+       "ruderich.org/simon/safcm/remote/sync"
 )
 
 func MainFixperms() error {
index fdbe98fba8764565551f7d4e8845789957c12db4..387caeebef07972ebbb8fce8d24097fc1d8ea4c1 100644 (file)
@@ -26,7 +26,7 @@ import (
        "testing"
        "time"
 
-       ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+       ft "ruderich.org/simon/safcm/remote/sync/filetest"
        "ruderich.org/simon/safcm/testutil"
 )
 
similarity index 98%
rename from cmd/safcm-remote/ainsl/ainsl.go
rename to remote/ainsl/ainsl.go
index f03ecc8c8fba22b880b4cba1e56b5ab4adcc476b..d716b2b4d5ce0868b4006a19ed47acb3f7da5ece 100644 (file)
@@ -32,7 +32,7 @@ import (
        "strings"
        "syscall"
 
-       "ruderich.org/simon/safcm/cmd/safcm-remote/sync"
+       "ruderich.org/simon/safcm/remote/sync"
 )
 
 func Main(args []string) error {
similarity index 98%
rename from cmd/safcm-remote/ainsl/ainsl_test.go
rename to remote/ainsl/ainsl_test.go
index 8e9b0c121a268747d2a234dab0e1a045927fd55f..3d391f0ce49ceaf5e68eb1052e0ddae1bc3671fa 100644 (file)
@@ -24,7 +24,7 @@ import (
        "syscall"
        "testing"
 
-       ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+       ft "ruderich.org/simon/safcm/remote/sync/filetest"
        "ruderich.org/simon/safcm/testutil"
 )
 
similarity index 93%
rename from cmd/safcm-remote/info/info.go
rename to remote/info/info.go
index 15bf0489990eb2e9d75c5cf3316a5899f0c95a5a..b58752d3931b91e0d711a282e341f89bb29abf47 100644 (file)
@@ -22,8 +22,8 @@ import (
        "runtime"
 
        "ruderich.org/simon/safcm"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/log"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/run"
+       "ruderich.org/simon/safcm/remote/log"
+       "ruderich.org/simon/safcm/remote/run"
 )
 
 type Info struct {
diff --git a/remote/main.go b/remote/main.go
new file mode 100644 (file)
index 0000000..61a6237
--- /dev/null
@@ -0,0 +1,114 @@
+// Helper copied to the remote host to run commands and deploy configuration
+
+// 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 remote
+
+import (
+       "fmt"
+       "log"
+       "os"
+
+       "golang.org/x/term"
+
+       "ruderich.org/simon/safcm"
+       "ruderich.org/simon/safcm/remote/ainsl"
+       "ruderich.org/simon/safcm/remote/info"
+       "ruderich.org/simon/safcm/remote/run"
+       "ruderich.org/simon/safcm/remote/sync"
+)
+
+func usage() {
+       log.Fatalf("usage: %[1]s sync\n"+
+               "usage: %[1]s ainsl [options] <path> <line>",
+               os.Args[0])
+}
+
+func Main() {
+       // Timestamps are added by `safcm`
+       log.SetFlags(0)
+
+       if len(os.Args) < 2 {
+               usage()
+       }
+
+       var err error
+       switch os.Args[1] {
+       case "sync":
+               if len(os.Args) != 2 {
+                       usage()
+               }
+               err = mainLoop()
+       case "ainsl":
+               err = ainsl.Main(os.Args)
+       default:
+               usage()
+       }
+
+       if err != nil {
+               log.Fatalf("%s: %v", os.Args[0], err)
+       }
+}
+
+func mainLoop() error {
+       if term.IsTerminal(int(os.Stdin.Fd())) ||
+               term.IsTerminal(int(os.Stdout.Fd())) {
+               return fmt.Errorf("sync should only be called from `safcm` " +
+                       "(redirect stdin/stdout to circumvent this check)")
+       }
+
+       conn := safcm.NewGobConn(os.Stdin, os.Stdout)
+
+       var logLevel safcm.LogLevel
+       logFunc := func(level safcm.LogLevel, msg string) {
+               if logLevel >= level {
+                       conn.Send(safcm.MsgLog{
+                               Level: level,
+                               Text:  msg,
+                       })
+               }
+       }
+
+       var quitResp safcm.MsgQuitResp
+       for {
+               x, err := conn.Recv()
+               if err != nil {
+                       return err
+               }
+
+               var resp safcm.Msg
+               switch x := x.(type) {
+               case safcm.MsgInfoReq:
+                       logLevel = x.LogLevel // set log level globally
+                       resp = info.Handle(x, run.ExecRunner{}, logFunc)
+               case safcm.MsgSyncReq:
+                       resp = sync.Handle(x, run.ExecRunner{}, logFunc)
+               case safcm.MsgQuitReq:
+                       resp = quitResp
+               default:
+                       return fmt.Errorf("unsupported message %#v", x)
+               }
+
+               err = conn.Send(resp)
+               if err != nil {
+                       return err
+               }
+               if resp == quitResp {
+                       break
+               }
+       }
+       return nil
+}
similarity index 98%
rename from cmd/safcm-remote/run/cmd.go
rename to remote/run/cmd.go
index 8f3d6134b287702f8ae72a5f52be8d7bb25a89c2..a1c5e7f7de760deadfef7fbfc2b063b2c5ae83ce 100644 (file)
@@ -23,7 +23,7 @@ import (
        "os/exec"
        "strings"
 
-       "ruderich.org/simon/safcm/cmd/safcm-remote/log"
+       "ruderich.org/simon/safcm/remote/log"
 )
 
 type Cmd struct {
similarity index 98%
rename from cmd/safcm-remote/sync/commands.go
rename to remote/sync/commands.go
index 5a3c59c1d1d4c156396992733c1ed27131fe2364..c0d47d19cfd65a1fb8f03c9147a820d941af9d8c 100644 (file)
@@ -24,7 +24,7 @@ import (
        "strings"
 
        "ruderich.org/simon/safcm"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/run"
+       "ruderich.org/simon/safcm/remote/run"
 )
 
 func (s *Sync) syncCommands() error {
similarity index 99%
rename from cmd/safcm-remote/sync/files_test.go
rename to remote/sync/files_test.go
index 88a5b51f85cd3fa4a4d36f1422ecbe37424565de..39714594382445369936666719d625d4351cd820 100644 (file)
@@ -25,7 +25,7 @@ import (
        "testing"
 
        "ruderich.org/simon/safcm"
-       ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+       ft "ruderich.org/simon/safcm/remote/sync/filetest"
        "ruderich.org/simon/safcm/testutil"
 )
 
similarity index 95%
rename from cmd/safcm-remote/sync/sync.go
rename to remote/sync/sync.go
index 6835ca48f7b177d09963bea8c1a9ea336221d709..52873180303b4c60e609fc0b33c871cd7227c124 100644 (file)
@@ -22,8 +22,8 @@ import (
        "os/user"
 
        "ruderich.org/simon/safcm"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/log"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/run"
+       "ruderich.org/simon/safcm/remote/log"
+       "ruderich.org/simon/safcm/remote/run"
 )
 
 type Sync struct {
similarity index 97%
rename from cmd/safcm-remote/sync/sync_test.go
rename to remote/sync/sync_test.go
index 0953c0490e2fd17dda565b8d357615a1d8117758..f5d2e1120748b98704c8865ab2c4183d24552b67 100644 (file)
@@ -27,8 +27,8 @@ import (
        "github.com/google/go-cmp/cmp/cmpopts"
 
        "ruderich.org/simon/safcm"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/log"
-       "ruderich.org/simon/safcm/cmd/safcm-remote/run"
+       "ruderich.org/simon/safcm/remote/log"
+       "ruderich.org/simon/safcm/remote/run"
 )
 
 // testRunner implements run.Runner to test commands without actually running