fix: ignore surronding whitespace for cli config (#12250)

* fix: ignore surronding whitespace for cli config

Cli config files break if you edit them manually with any editor.
Editors drop a newline at the end, and we not break on this.
If a developer manually edits a file, it should still work
This commit is contained in:
Steven Masley 2024-02-21 13:03:41 -06:00 committed by GitHub
parent 475c3650ca
commit 3f65bd14cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"github.com/kirsle/configdir"
"golang.org/x/xerrors"
@ -85,13 +86,14 @@ func (f File) Write(s string) error {
return write(string(f), 0o600, []byte(s))
}
// Read reads the file to a string.
// Read reads the file to a string. All leading and trailing whitespace
// is removed.
func (f File) Read() (string, error) {
if f == "" {
return "", xerrors.Errorf("empty file path")
}
byt, err := read(string(f))
return string(byt), err
return strings.TrimSpace(string(byt)), err
}
// open opens a file in the configuration directory,