Skip to content

Commit

Permalink
fix err handling in yaml unmarshal
Browse files Browse the repository at this point in the history
yaml.Unmarshal handles empty files just fine, no need to do prior checks for
empty or space only files
  • Loading branch information
marcoandredinis committed Mar 30, 2022
1 parent ee0bfc7 commit ee119d6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
19 changes: 4 additions & 15 deletions tool/tsh/tshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"errors"
"io"
"io/fs"
"os"

Expand Down Expand Up @@ -48,27 +47,17 @@ type ExtraProxyHeaders struct {
}

func loadConfig(fullConfigPath string) (*TshConfig, error) {
emptyConfig := &TshConfig{}
configFile, err := os.Open(fullConfigPath)
bs, err := os.ReadFile(fullConfigPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return emptyConfig, nil
return &TshConfig{}, nil
}
return nil, trace.ConvertSystemError(err)
}
defer configFile.Close()

bs, err := io.ReadAll(configFile)
if err != nil {
return nil, trace.ConvertSystemError(err)
}
if len(bs) == 0 {
return emptyConfig, nil
}

cfg := TshConfig{}
if yaml.Unmarshal(bs, &cfg); err != nil {
return emptyConfig, trace.ConvertSystemError(err)
if err := yaml.Unmarshal(bs, &cfg); err != nil {
return nil, trace.ConvertSystemError(err)
}
return &cfg, nil
}
1 change: 1 addition & 0 deletions tool/tsh/tshconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestLoadConfigNonExistingFile(t *testing.T) {

func TestLoadConfigEmptyFile(t *testing.T) {
file, err := os.CreateTemp("", "test-telelport")
file.Write([]byte(" "))
require.NoError(t, err)
defer os.Remove(file.Name())

Expand Down

0 comments on commit ee119d6

Please sign in to comment.