From: Simon Ruderich Date: Tue, 18 May 2021 10:29:48 +0000 (+0200) Subject: Move implementation of cmd/safcm-remote/ to remote/ X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=9269fa3c94e700afc0be823f58ea473a2db8f3dc;p=safcm%2Fsafcm.git Move implementation of cmd/safcm-remote/ to remote/ 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. --- diff --git a/cmd/safcm-remote/main.go b/cmd/safcm-remote/main.go index a29f20c..b6d784d 100644 --- a/cmd/safcm-remote/main.go +++ b/cmd/safcm-remote/main.go @@ -18,97 +18,9 @@ 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] ", - 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() } diff --git a/cmd/safcm/config/files_test.go b/cmd/safcm/config/files_test.go index 7ad12d6..110c012 100644 --- a/cmd/safcm/config/files_test.go +++ b/cmd/safcm/config/files_test.go @@ -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" ) diff --git a/cmd/safcm/fixperms.go b/cmd/safcm/fixperms.go index 57c0811..36d618f 100644 --- a/cmd/safcm/fixperms.go +++ b/cmd/safcm/fixperms.go @@ -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 { diff --git a/cmd/safcm/main_sync_test.go b/cmd/safcm/main_sync_test.go index fdbe98f..387caee 100644 --- a/cmd/safcm/main_sync_test.go +++ b/cmd/safcm/main_sync_test.go @@ -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" ) diff --git a/cmd/safcm-remote/ainsl/ainsl.go b/remote/ainsl/ainsl.go similarity index 98% rename from cmd/safcm-remote/ainsl/ainsl.go rename to remote/ainsl/ainsl.go index f03ecc8..d716b2b 100644 --- a/cmd/safcm-remote/ainsl/ainsl.go +++ b/remote/ainsl/ainsl.go @@ -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 { diff --git a/cmd/safcm-remote/ainsl/ainsl_test.go b/remote/ainsl/ainsl_test.go similarity index 98% rename from cmd/safcm-remote/ainsl/ainsl_test.go rename to remote/ainsl/ainsl_test.go index 8e9b0c1..3d391f0 100644 --- a/cmd/safcm-remote/ainsl/ainsl_test.go +++ b/remote/ainsl/ainsl_test.go @@ -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" ) diff --git a/cmd/safcm-remote/info/info.go b/remote/info/info.go similarity index 93% rename from cmd/safcm-remote/info/info.go rename to remote/info/info.go index 15bf048..b58752d 100644 --- a/cmd/safcm-remote/info/info.go +++ b/remote/info/info.go @@ -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/cmd/safcm-remote/log/logger.go b/remote/log/logger.go similarity index 100% rename from cmd/safcm-remote/log/logger.go rename to remote/log/logger.go diff --git a/remote/main.go b/remote/main.go new file mode 100644 index 0000000..61a6237 --- /dev/null +++ b/remote/main.go @@ -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 . + +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] ", + 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 +} diff --git a/cmd/safcm-remote/run/cmd.go b/remote/run/cmd.go similarity index 98% rename from cmd/safcm-remote/run/cmd.go rename to remote/run/cmd.go index 8f3d613..a1c5e7f 100644 --- a/cmd/safcm-remote/run/cmd.go +++ b/remote/run/cmd.go @@ -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 { diff --git a/cmd/safcm-remote/run/runner.go b/remote/run/runner.go similarity index 100% rename from cmd/safcm-remote/run/runner.go rename to remote/run/runner.go diff --git a/cmd/safcm-remote/sync/commands.go b/remote/sync/commands.go similarity index 98% rename from cmd/safcm-remote/sync/commands.go rename to remote/sync/commands.go index 5a3c59c..c0d47d1 100644 --- a/cmd/safcm-remote/sync/commands.go +++ b/remote/sync/commands.go @@ -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 { diff --git a/cmd/safcm-remote/sync/commands_test.go b/remote/sync/commands_test.go similarity index 100% rename from cmd/safcm-remote/sync/commands_test.go rename to remote/sync/commands_test.go diff --git a/cmd/safcm-remote/sync/files.go b/remote/sync/files.go similarity index 100% rename from cmd/safcm-remote/sync/files.go rename to remote/sync/files.go diff --git a/cmd/safcm-remote/sync/files_test.go b/remote/sync/files_test.go similarity index 99% rename from cmd/safcm-remote/sync/files_test.go rename to remote/sync/files_test.go index 88a5b51..3971459 100644 --- a/cmd/safcm-remote/sync/files_test.go +++ b/remote/sync/files_test.go @@ -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" ) diff --git a/cmd/safcm-remote/sync/files_windows.go b/remote/sync/files_windows.go similarity index 100% rename from cmd/safcm-remote/sync/files_windows.go rename to remote/sync/files_windows.go diff --git a/cmd/safcm-remote/sync/filetest/filetest.go b/remote/sync/filetest/filetest.go similarity index 100% rename from cmd/safcm-remote/sync/filetest/filetest.go rename to remote/sync/filetest/filetest.go diff --git a/cmd/safcm-remote/sync/packages.go b/remote/sync/packages.go similarity index 100% rename from cmd/safcm-remote/sync/packages.go rename to remote/sync/packages.go diff --git a/cmd/safcm-remote/sync/packages_debian.go b/remote/sync/packages_debian.go similarity index 100% rename from cmd/safcm-remote/sync/packages_debian.go rename to remote/sync/packages_debian.go diff --git a/cmd/safcm-remote/sync/packages_debian_test.go b/remote/sync/packages_debian_test.go similarity index 100% rename from cmd/safcm-remote/sync/packages_debian_test.go rename to remote/sync/packages_debian_test.go diff --git a/cmd/safcm-remote/sync/services.go b/remote/sync/services.go similarity index 100% rename from cmd/safcm-remote/sync/services.go rename to remote/sync/services.go diff --git a/cmd/safcm-remote/sync/services_systemd.go b/remote/sync/services_systemd.go similarity index 100% rename from cmd/safcm-remote/sync/services_systemd.go rename to remote/sync/services_systemd.go diff --git a/cmd/safcm-remote/sync/services_systemd_test.go b/remote/sync/services_systemd_test.go similarity index 100% rename from cmd/safcm-remote/sync/services_systemd_test.go rename to remote/sync/services_systemd_test.go diff --git a/cmd/safcm-remote/sync/sync.go b/remote/sync/sync.go similarity index 95% rename from cmd/safcm-remote/sync/sync.go rename to remote/sync/sync.go index 6835ca4..5287318 100644 --- a/cmd/safcm-remote/sync/sync.go +++ b/remote/sync/sync.go @@ -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 { diff --git a/cmd/safcm-remote/sync/sync_test.go b/remote/sync/sync_test.go similarity index 97% rename from cmd/safcm-remote/sync/sync_test.go rename to remote/sync/sync_test.go index 0953c04..f5d2e11 100644 --- a/cmd/safcm-remote/sync/sync_test.go +++ b/remote/sync/sync_test.go @@ -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 diff --git a/cmd/safcm-remote/sync/triggers.go b/remote/sync/triggers.go similarity index 100% rename from cmd/safcm-remote/sync/triggers.go rename to remote/sync/triggers.go diff --git a/cmd/safcm-remote/sync/triggers_test.go b/remote/sync/triggers_test.go similarity index 100% rename from cmd/safcm-remote/sync/triggers_test.go rename to remote/sync/triggers_test.go