| 1 |
package plugin |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"sync" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
func NewClient(c io.ReadWriteCloser) (Interface, error) { |
| 19 |
cl := &client{ |
| 20 |
c: c, |
| 21 |
enc: json.NewEncoder(c), |
| 22 |
pending: make(map[uint64]chan rpcResponse), |
| 23 |
} |
| 24 |
go cl.readLoop() |
| 25 |
var res infoResult |
| 26 |
if err := cl.call(methodInfo, infoArgs{Version: protocolVersion}, &res); err != nil { |
| 27 |
c.Close() |
| 28 |
return nil, fmt.Errorf("plugin handshake failed: %v", err) |
| 29 |
} |
| 30 |
if res.Version > protocolVersion { |
| 31 |
c.Close() |
| 32 |
return nil, fmt.Errorf("plugin selected protocol version %d, greater than supported version %d", res.Version, protocolVersion) |
| 33 |
} |
| 34 |
cl.info = res.Info |
| 35 |
return cl, nil |
| 36 |
} |
| 37 |
|
| 38 |
type client struct { |
| 39 |
c io.ReadWriteCloser |
| 40 |
info PluginInfo |
| 41 |
|
| 42 |
|
| 43 |
mu sync.Mutex |
| 44 |
enc *json.Encoder |
| 45 |
seq uint64 |
| 46 |
pending map[uint64]chan rpcResponse |
| 47 |
err error |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func (cl *client) readLoop() { |
| 54 |
dec := json.NewDecoder(cl.c) |
| 55 |
for { |
| 56 |
var resp rpcResponse |
| 57 |
if err := dec.Decode(&resp); err != nil { |
| 58 |
cl.mu.Lock() |
| 59 |
cl.err = err |
| 60 |
pending := cl.pending |
| 61 |
cl.pending = nil |
| 62 |
cl.mu.Unlock() |
| 63 |
for _, ch := range pending { |
| 64 |
close(ch) |
| 65 |
} |
| 66 |
return |
| 67 |
} |
| 68 |
cl.mu.Lock() |
| 69 |
ch := cl.pending[resp.ID] |
| 70 |
delete(cl.pending, resp.ID) |
| 71 |
cl.mu.Unlock() |
| 72 |
if ch != nil { |
| 73 |
ch <- resp |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func (cl *client) call(method string, arg, result any) error { |
| 82 |
var params json.RawMessage |
| 83 |
if arg != nil { |
| 84 |
data, err := json.Marshal(arg) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
params = data |
| 89 |
} |
| 90 |
ch := make(chan rpcResponse, 1) |
| 91 |
cl.mu.Lock() |
| 92 |
if cl.err != nil { |
| 93 |
err := cl.err |
| 94 |
cl.mu.Unlock() |
| 95 |
return err |
| 96 |
} |
| 97 |
cl.seq++ |
| 98 |
id := cl.seq |
| 99 |
cl.pending[id] = ch |
| 100 |
err := cl.enc.Encode(rpcRequest{ID: id, Method: method, Params: params}) |
| 101 |
if err != nil { |
| 102 |
delete(cl.pending, id) |
| 103 |
cl.mu.Unlock() |
| 104 |
return err |
| 105 |
} |
| 106 |
cl.mu.Unlock() |
| 107 |
|
| 108 |
resp, ok := <-ch |
| 109 |
if !ok { |
| 110 |
|
| 111 |
cl.mu.Lock() |
| 112 |
err := cl.err |
| 113 |
cl.mu.Unlock() |
| 114 |
if err == nil || err == io.EOF { |
| 115 |
err = errors.New("plugin connection closed") |
| 116 |
} |
| 117 |
return err |
| 118 |
} |
| 119 |
if resp.Error != "" { |
| 120 |
return errors.New(resp.Error) |
| 121 |
} |
| 122 |
if result != nil && len(resp.Result) > 0 { |
| 123 |
return json.Unmarshal(resp.Result, result) |
| 124 |
} |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
func (cl *client) Info() PluginInfo { return cl.info } |
| 129 |
|
| 130 |
func (cl *client) NewTestInstance(p TestParams) (TestInstance, error) { |
| 131 |
var res newInstanceResult |
| 132 |
if err := cl.call(methodNewInstance, p, &res); err != nil { |
| 133 |
return nil, err |
| 134 |
} |
| 135 |
return &clientInstance{cl: cl, instID: res.InstID, env: res.Env}, nil |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
func (cl *client) Close() { |
| 141 |
cl.c.Close() |
| 142 |
} |
| 143 |
|
| 144 |
type clientInstance struct { |
| 145 |
cl *client |
| 146 |
instID int |
| 147 |
env map[string]string |
| 148 |
} |
| 149 |
|
| 150 |
func (i *clientInstance) Env() map[string]string { return i.env } |
| 151 |
|
| 152 |
func (i *clientInstance) RunCmd(p CmdParams) (CmdResult, error) { |
| 153 |
var res CmdResult |
| 154 |
if err := i.cl.call(methodRunCmd, runCmdArgs{InstID: i.instID, Params: p}, &res); err != nil { |
| 155 |
return CmdResult{}, err |
| 156 |
} |
| 157 |
return res, nil |
| 158 |
} |
| 159 |
|
| 160 |
func (i *clientInstance) Close() { |
| 161 |
i.cl.call(methodCloseInstance, closeInstanceArgs{InstID: i.instID}, nil) |
| 162 |
} |