From bddd3e0e9262aa167dcfb517ccf01ff4da85530e Mon Sep 17 00:00:00 2001 From: Marco Dinis Date: Wed, 30 Mar 2022 08:03:06 +0100 Subject: [PATCH] fix err handling in yaml unmarshal yaml.Unmarshal handles empty files just fine, no need to do prior checks for empty or space only files --- tool/tsh/tshconfig.go | 19 ++++--------------- tool/tsh/tshconfig_test.go | 1 + 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/tool/tsh/tshconfig.go b/tool/tsh/tshconfig.go index cd9b54b3a0422..f12e9b47fe550 100644 --- a/tool/tsh/tshconfig.go +++ b/tool/tsh/tshconfig.go @@ -18,7 +18,6 @@ package main import ( "errors" - "io" "io/fs" "os" @@ -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 } diff --git a/tool/tsh/tshconfig_test.go b/tool/tsh/tshconfig_test.go index 4820003369425..b3d11c90e8903 100644 --- a/tool/tsh/tshconfig_test.go +++ b/tool/tsh/tshconfig_test.go @@ -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())