Commit c56e41b5 by Iwasaki Yudai

Extract function for loading profile files

1 parent acacba6f
Showing with 23 additions and 15 deletions
......@@ -51,12 +51,32 @@ func New(options Options) (*App, error) {
return nil, errors.New("Title format string syntax error")
}
prefMap, err := loadProfileFile(options)
if err != nil {
return nil, err
}
return &App{
options: options,
upgrader: &websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Subprotocols: []string{"gotty"},
},
preferences: prefMap,
titleTemplate: titleTemplate,
}, 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 _, err := os.Stat(prefPath); os.IsNotExist(err) {
if options.ProfileFile != DefaultProfileFilePath {
return nil, err
}
......@@ -68,23 +88,11 @@ func New(options Options) (*App, error) {
prefString = []byte(("{}"))
}
var prefMap map[string]interface{}
err = json.Unmarshal(prefString, &prefMap)
err := json.Unmarshal(prefString, &prefMap)
if err != nil {
return nil, err
}
return &App{
options: options,
upgrader: &websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Subprotocols: []string{"gotty"},
},
preferences: prefMap,
titleTemplate: titleTemplate,
}, nil
return prefMap, nil
}
func (app *App) Run() error {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!