From c56e41b56bfe781bb230f4aae3ee4098f4dc053a Mon Sep 17 00:00:00 2001 From: Iwasaki Yudai Date: Mon, 24 Aug 2015 16:13:22 +0900 Subject: [PATCH] Extract function for loading profile files --- app/app.go | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/app/app.go b/app/app.go index 9843ab6..816133d 100644 --- a/app/app.go +++ b/app/app.go @@ -51,24 +51,7 @@ func New(options Options) (*App, error) { return nil, errors.New("Title format string syntax error") } - prefString := []byte{} - prefPath := options.ProfileFile - if options.ProfileFile == DefaultProfileFilePath { - prefPath = os.Getenv("HOME") + "/.gotty" - } - if _, err = os.Stat(prefPath); os.IsNotExist(err) { - if options.ProfileFile != DefaultProfileFilePath { - return nil, err - } - } else { - log.Printf("Loading profile path: %s", prefPath) - prefString, _ = ioutil.ReadFile(prefPath) - } - if len(prefString) == 0 { - prefString = []byte(("{}")) - } - var prefMap map[string]interface{} - err = json.Unmarshal(prefString, &prefMap) + prefMap, err := loadProfileFile(options) if err != nil { return nil, err } @@ -87,6 +70,31 @@ func New(options Options) (*App, error) { }, nil } +func loadProfileFile(options Options) (map[string]interface{}, error) { + prefString := []byte{} + prefPath := options.ProfileFile + if options.ProfileFile == DefaultProfileFilePath { + prefPath = os.Getenv("HOME") + "/.gotty" + } + if _, err := os.Stat(prefPath); os.IsNotExist(err) { + if options.ProfileFile != DefaultProfileFilePath { + return nil, err + } + } else { + log.Printf("Loading profile path: %s", prefPath) + prefString, _ = ioutil.ReadFile(prefPath) + } + if len(prefString) == 0 { + prefString = []byte(("{}")) + } + var prefMap map[string]interface{} + err := json.Unmarshal(prefString, &prefMap) + if err != nil { + return nil, err + } + return prefMap, nil +} + func (app *App) Run() error { if app.options.PermitWrite { log.Printf("Permitting clients to write input to the PTY.")