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 (
"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) {