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>
This commit is contained in:
Josh Bleecher Snyder 2020-12-22 10:08:25 -08:00 committed by Jason A. Donenfeld
parent 25b01723dd
commit a1c265b0c5

View file

@ -7,7 +7,6 @@ package device
import ( import (
"bufio" "bufio"
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -33,7 +32,7 @@ func (s IPCError) ErrorCode() int64 {
return s.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) lines := make([]string, 0, 100)
send := func(line string) { send := func(line string) {
lines = append(lines, line) lines = append(lines, line)
@ -99,7 +98,7 @@ func (device *Device) IpcGetOperation(w io.StringWriter) error {
// send lines (does not require resource locks) // send lines (does not require resource locks)
for _, line := range lines { for _, line := range lines {
_, err := w.WriteString(line + "\n") _, err := io.WriteString(w, line+"\n")
if err != nil { if err != nil {
return &IPCError{ipc.IpcErrorIO} return &IPCError{ipc.IpcErrorIO}
} }
@ -397,22 +396,16 @@ func (device *Device) IpcSetOperation(r io.Reader) error {
return nil return nil
} }
func (device *Device) IpcGet() (string, error) { func (device *Device) IpcGet() (string, error) {
buf := new(bytes.Buffer) buf := new(strings.Builder)
writer := bufio.NewWriter(buf) if err := device.IpcGetOperation(buf); err != nil {
if err := device.IpcGetOperation(writer); err != nil {
return "", err
}
if err := writer.Flush(); err != nil {
return "", err return "", err
} }
return buf.String(), nil return buf.String(), nil
} }
func (device *Device) IpcSet(uapiConf string) error { 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) { func (device *Device) IpcHandle(socket net.Conn) {