-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
111 lines (94 loc) · 2.78 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package socketcmd
/* Copyright 2017 Ryan Clarke
This file is part of Socketcmd.
Socketcmd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Socketcmd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Socketcmd. If not, see <http://www.gnu.org/licenses/>
*/
import (
"errors"
"net"
"os"
"os/exec"
)
/* A Wrapper provides I/O redirection for a process. Input to the Wrapper's network socket
* will be forwarded to the process, with the resulting lines of stdout returned to the
* socket client.
*/
type Wrapper interface {
// Addr returns the address of the underlying net Listener.
Addr() net.Addr
// Run the wrapped process.
Run() error
// Start the wrapped process.
Start() error
// Wait for the wrapped process to exit.
Wait() error
// ExposeAPI for high-level network operations.
ExposeAPI(ParseFunc) WrapperAPI
}
/* NewUnix returns a new socket Wrapper around the given command using a new UNIX domain
* socket Listener with the given address.
*/
func NewUnix(socket string, cmd *exec.Cmd) (Wrapper, error) {
os.Remove(socket)
listener, err := net.Listen("unix", socket)
if err != nil {
return nil, err
}
return New(listener, cmd)
}
/* New returns a new socket Wrapper around the given command using the given net Listener.
*/
func New(listener net.Listener, cmd *exec.Cmd) (Wrapper, error) {
if listener == nil || cmd == nil {
return nil, errors.New("missing required parameters")
}
// Create pipes for I/O redirection
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
// Initialize socket Handler for the wrapped process
return &wrapper{cmd, NewHandler(listener, stdin, stdout)}, nil
}
/* Cmd returns a new exec.Cmd for use with a wrapper.
*/
func Cmd(cmd string, args ...string) *exec.Cmd {
return exec.Command(cmd, args...)
}
type wrapper struct {
Cmd *exec.Cmd
h Handler
}
func (w *wrapper) Addr() net.Addr {
return w.h.Addr()
}
func (w *wrapper) Run() error {
w.h.Start()
defer w.h.Close()
return w.Cmd.Run()
}
func (w *wrapper) Start() error {
w.h.Start()
return w.Cmd.Start()
}
func (w *wrapper) Wait() error {
defer w.h.Close()
return w.Cmd.Wait()
}
func (w *wrapper) ExposeAPI(parser ParseFunc) WrapperAPI {
client := NewClient(w.Addr().Network(), w.Addr().String(), parser)
return &wrapperAPI{w, client}
}