X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm-remote%2Fmain.go;h=d17446017b89940786420e07d2b0415a84cf6c47;hb=HEAD;hp=9c58d3af8175c337aa60a2b47bc1383105cc0226;hpb=ddd21f01f764a4ff61204d8e9d0ef8421ebf685c;p=safcm%2Fsafcm.git diff --git a/cmd/safcm-remote/main.go b/cmd/safcm-remote/main.go index 9c58d3a..d174460 100644 --- a/cmd/safcm-remote/main.go +++ b/cmd/safcm-remote/main.go @@ -1,94 +1,14 @@ -// Helper copied to the remote hosts to run commands and deploy configuration +// 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 . +// SPDX-License-Identifier: GPL-3.0-or-later +// Copyright (C) 2021-2024 Simon Ruderich package main import ( - "fmt" - "log" - "os" - - "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 main() { - // Timestamps are added by `safcm` - log.SetFlags(0) - - var err error - if len(os.Args) == 1 { - err = mainLoop() - } else if len(os.Args) >= 2 && os.Args[1] == "ainsl" { - err = ainsl.Main(os.Args) - } else { - log.Fatalf("usage: %[1]s\n"+ - "usage: %[1]s ainsl [options] ", - os.Args[0]) - } - - if err != nil { - log.Fatalf("%s: %v", os.Args[0], err) - } -} - -func mainLoop() error { - conn := safcm.NewGobConn(os.Stdin, os.Stdout) - - var logLevel safcm.LogLevel - logFunc := func(level safcm.LogLevel, format string, a ...interface{}) { - if logLevel >= level { - conn.Send(safcm.MsgLog{ - Level: level, - Text: fmt.Sprintf(format, a...), - }) - } - } - - 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() }