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