summaryrefslogtreecommitdiffhomepage
path: root/device
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josh@tailscale.com>2020-12-22 10:08:25 -0800
committerJason A. Donenfeld <Jason@zx2c4.com>2021-01-07 14:49:44 +0100
commita1c265b0c5bea2c1c4a62acac0045fe93cd691cd (patch)
treea29303936a5013dede0ad65df1f1738bd26e1967 /device
parent25b01723ddc8bcd2993eb4b21e4e61164f55fa95 (diff)
device: simplify UAPI helper methods
bufio is not required. strings.Builder is cheaper than bytes.Buffer for constructing strings. io.Writer is more flexible than io.StringWriter, and just as cheap (when used with io.WriteString). Run gofmt. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Diffstat (limited to 'device')
-rw-r--r--device/uapi.go17
1 files changed, 5 insertions, 12 deletions
diff --git a/device/uapi.go b/device/uapi.go
index 2843a4c..370ad4e 100644
--- a/device/uapi.go
+++ b/device/uapi.go
@@ -7,7 +7,6 @@ package device
import (
"bufio"
- "bytes"
"errors"
"fmt"
"io"
@@ -33,7 +32,7 @@ func (s IPCError) ErrorCode() int64 {
return s.int64
}
-func (device *Device) IpcGetOperation(w io.StringWriter) error {
+func (device *Device) IpcGetOperation(w io.Writer) error {
lines := make([]string, 0, 100)
send := func(line string) {
lines = append(lines, line)
@@ -99,7 +98,7 @@ func (device *Device) IpcGetOperation(w io.StringWriter) error {
// send lines (does not require resource locks)
for _, line := range lines {
- _, err := w.WriteString(line + "\n")
+ _, err := io.WriteString(w, line+"\n")
if err != nil {
return &IPCError{ipc.IpcErrorIO}
}
@@ -397,22 +396,16 @@ func (device *Device) IpcSetOperation(r io.Reader) error {
return nil
}
-
-
func (device *Device) IpcGet() (string, error) {
- buf := new(bytes.Buffer)
- writer := bufio.NewWriter(buf)
- if err := device.IpcGetOperation(writer); err != nil {
- return "", err
- }
- if err := writer.Flush(); err != nil {
+ buf := new(strings.Builder)
+ if err := device.IpcGetOperation(buf); err != nil {
return "", err
}
return buf.String(), nil
}
func (device *Device) IpcSet(uapiConf string) error {
- return device.IpcSetOperation(bufio.NewReader(strings.NewReader(uapiConf)))
+ return device.IpcSetOperation(strings.NewReader(uapiConf))
}
func (device *Device) IpcHandle(socket net.Conn) {