1 // Helper copied to the remote host to run commands and deploy configuration
3 // Copyright (C) 2021-2023 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "ruderich.org/simon/safcm"
28 "ruderich.org/simon/safcm/remote/ainsl"
29 "ruderich.org/simon/safcm/remote/info"
30 "ruderich.org/simon/safcm/remote/run"
31 "ruderich.org/simon/safcm/remote/sync"
35 log.Fatalf("usage: %[1]s sync\n"+
36 "usage: %[1]s ainsl [options] <path> <line>",
41 // Timestamps are added by `safcm`
51 if len(os.Args) != 2 {
56 err = ainsl.Main(os.Args)
62 log.Fatalf("%s: %v", os.Args[0], err)
66 func mainLoop() error {
67 if term.IsTerminal(int(os.Stdin.Fd())) ||
68 term.IsTerminal(int(os.Stdout.Fd())) {
69 return fmt.Errorf("sync should only be called from `safcm` " +
70 "(redirect stdin/stdout to circumvent this check)")
73 conn := safcm.NewGobConn(os.Stdin, os.Stdout)
75 var logLevel safcm.LogLevel
76 logFunc := func(level safcm.LogLevel, msg string) {
77 if logLevel >= level {
78 // Handling errors here is complex and quite verbose.
79 // If it happens the connection is gone anyway so skip
80 // the error handling.
81 conn.Send(safcm.MsgLog{ //nolint:errcheck
88 var quitResp safcm.MsgQuitResp
96 switch x := x.(type) {
97 case safcm.MsgInfoReq:
98 logLevel = x.LogLevel // set log level globally
99 resp = info.Handle(x, run.ExecRunner{}, logFunc)
100 case safcm.MsgSyncReq:
101 resp = sync.Handle(x, run.ExecRunner{}, logFunc)
102 case safcm.MsgQuitReq:
105 return fmt.Errorf("unsupported message %#v", x)
108 err = conn.Send(resp)
112 if resp == quitResp {