mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-12-22 23:17:29 +00:00
Extract function for loading profile files
This commit is contained in:
parent
acacba6f03
commit
c56e41b56b
44
app/app.go
44
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.")
|
||||
|
Loading…
Reference in New Issue
Block a user