Skip to content

fix: ignore surronding whitespace for cli config #12250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
All reads normalized
  • Loading branch information
Emyrk committed Feb 21, 2024
commit b396c25e66bbcebb24e9e0d987428b6810910570
13 changes: 3 additions & 10 deletions cli/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,14 @@ func (f File) Write(s string) error {
return write(string(f), 0o600, []byte(s))
}

func (f File) ReadNormalizeSpace() (string, error) {
str, err := f.Read()
if err != nil {
return "", err
}
return strings.TrimSpace(str), nil
}

// 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,
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func (r *RootCmd) initClientInternal(client *codersdk.Client, allowTokenMissing
}

if r.token == "" {
r.token, err = conf.Session().ReadNormalizeSpace()
r.token, err = conf.Session().Read()
// If the configuration files are absent, the user is logged out
if os.IsNotExist(err) {
if !allowTokenMissing {
Expand Down