From ad062d777d394fcd2dbd29bf857c0c1ac610a992 Mon Sep 17 00:00:00 2001 From: kota kanbe Date: Thu, 30 Jun 2016 09:01:47 +0900 Subject: [PATCH] Add some validation of loading config. user, host and port --- config/tomlloader.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/config/tomlloader.go b/config/tomlloader.go index 172b7dd1..64e06761 100644 --- a/config/tomlloader.go +++ b/config/tomlloader.go @@ -61,9 +61,14 @@ func (c TOMLLoader) Load(pathToToml, keyPass, sudoPass string) (err error) { } s := ServerInfo{ServerName: name} - s.User = v.User - if s.User == "" { + + switch { + case v.User != "": + s.User = v.User + case d.User != "": s.User = d.User + default: + return fmt.Errorf("%s is invalid. User is empty", name) } // s.Password = sudoPass @@ -73,10 +78,17 @@ func (c TOMLLoader) Load(pathToToml, keyPass, sudoPass string) (err error) { } s.Host = v.Host + if s.Host == "" { + return fmt.Errorf("%s is invalid. host is empty", name) + } - s.Port = v.Port - if s.Port == "" { + switch { + case v.Port != "": + s.Port = v.Port + case d.Port != "": s.Port = d.Port + default: + s.Port = "22" } s.KeyPath = v.KeyPath @@ -86,7 +98,7 @@ func (c TOMLLoader) Load(pathToToml, keyPass, sudoPass string) (err error) { if s.KeyPath != "" { if _, err := os.Stat(s.KeyPath); err != nil { return fmt.Errorf( - "config.toml is invalid. keypath: %s not exists", s.KeyPath) + "%s is invalid. keypath: %s not exists", name, s.KeyPath) } }