]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/main.go
remote: add ainsl sub-command ("append if no such line")
[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         "ruderich.org/simon/safcm"
26         "ruderich.org/simon/safcm/cmd/safcm-remote/ainsl"
27         "ruderich.org/simon/safcm/cmd/safcm-remote/info"
28         "ruderich.org/simon/safcm/cmd/safcm-remote/run"
29         "ruderich.org/simon/safcm/cmd/safcm-remote/sync"
30 )
31
32 func main() {
33         // Timestamps are added by `safcm`
34         log.SetFlags(0)
35
36         var err error
37         if len(os.Args) == 1 {
38                 err = mainLoop()
39         } else if len(os.Args) >= 2 && os.Args[1] == "ainsl" {
40                 err = ainsl.Main(os.Args)
41         } else {
42                 log.Fatalf("usage: %[1]s\n"+
43                         "usage: %[1]s ainsl [options] <path> <line>",
44                         os.Args[0])
45         }
46
47         if err != nil {
48                 log.Fatalf("%s: %v", os.Args[0], err)
49         }
50 }
51
52 func mainLoop() error {
53         conn := safcm.NewGobConn(os.Stdin, os.Stdout)
54
55         var logLevel safcm.LogLevel
56         logFunc := func(level safcm.LogLevel, format string, a ...interface{}) {
57                 if logLevel >= level {
58                         conn.Send(safcm.MsgLog{
59                                 Level: level,
60                                 Text:  fmt.Sprintf(format, a...),
61                         })
62                 }
63         }
64
65         var quitResp safcm.MsgQuitResp
66         for {
67                 x, err := conn.Recv()
68                 if err != nil {
69                         return err
70                 }
71
72                 var resp safcm.Msg
73                 switch x := x.(type) {
74                 case safcm.MsgInfoReq:
75                         logLevel = x.LogLevel // set log level globally
76                         resp = info.Handle(x, run.ExecRunner{}, logFunc)
77                 case safcm.MsgSyncReq:
78                         resp = sync.Handle(x, run.ExecRunner{}, logFunc)
79                 case safcm.MsgQuitReq:
80                         resp = quitResp
81                 default:
82                         return fmt.Errorf("unsupported message %#v", x)
83                 }
84
85                 err = conn.Send(resp)
86                 if err != nil {
87                         return err
88                 }
89                 if resp == quitResp {
90                         break
91                 }
92         }
93         return nil
94 }