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