]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/main.go
e9ce1cd0ccac627e6573bc5b07c040166cb4be65
[safcm/safcm.git] / cmd / safcm-remote / main.go
1 // Helper copied to the remote hosts to run commands and deploy configuration
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
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.
9 //
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.
14 //
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/>.
17
18 package main
19
20 import (
21         "fmt"
22         "log"
23         "os"
24
25         "golang.org/x/term"
26
27         "ruderich.org/simon/safcm"
28         "ruderich.org/simon/safcm/cmd/safcm-remote/ainsl"
29         "ruderich.org/simon/safcm/cmd/safcm-remote/info"
30         "ruderich.org/simon/safcm/cmd/safcm-remote/run"
31         "ruderich.org/simon/safcm/cmd/safcm-remote/sync"
32 )
33
34 func usage() {
35         log.Fatalf("usage: %[1]s sync\n"+
36                 "usage: %[1]s ainsl [options] <path> <line>",
37                 os.Args[0])
38 }
39
40 func main() {
41         // Timestamps are added by `safcm`
42         log.SetFlags(0)
43
44         if len(os.Args) < 2 {
45                 usage()
46         }
47
48         var err error
49         switch os.Args[1] {
50         case "sync":
51                 if len(os.Args) != 2 {
52                         usage()
53                 }
54                 err = mainLoop()
55         case "ainsl":
56                 err = ainsl.Main(os.Args)
57         default:
58                 usage()
59         }
60
61         if err != nil {
62                 log.Fatalf("%s: %v", os.Args[0], err)
63         }
64 }
65
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)")
71         }
72
73         conn := safcm.NewGobConn(os.Stdin, os.Stdout)
74
75         var logLevel safcm.LogLevel
76         logFunc := func(level safcm.LogLevel, format string, a ...interface{}) {
77                 if logLevel >= level {
78                         conn.Send(safcm.MsgLog{
79                                 Level: level,
80                                 Text:  fmt.Sprintf(format, a...),
81                         })
82                 }
83         }
84
85         var quitResp safcm.MsgQuitResp
86         for {
87                 x, err := conn.Recv()
88                 if err != nil {
89                         return err
90                 }
91
92                 var resp safcm.Msg
93                 switch x := x.(type) {
94                 case safcm.MsgInfoReq:
95                         logLevel = x.LogLevel // set log level globally
96                         resp = info.Handle(x, run.ExecRunner{}, logFunc)
97                 case safcm.MsgSyncReq:
98                         resp = sync.Handle(x, run.ExecRunner{}, logFunc)
99                 case safcm.MsgQuitReq:
100                         resp = quitResp
101                 default:
102                         return fmt.Errorf("unsupported message %#v", x)
103                 }
104
105                 err = conn.Send(resp)
106                 if err != nil {
107                         return err
108                 }
109                 if resp == quitResp {
110                         break
111                 }
112         }
113         return nil
114 }