From 8f64a349ecb84dfe4cee0a911528f25a50c4191c Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 4 Oct 2022 19:04:57 +0200 Subject: [PATCH] Normalize paths --- main.go | 34 ++++++++++++++++++++++++++++++++++ model/action.go | 11 ++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a999bbf..812adbf 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,40 @@ func main() { log.Fatal("Unable to read configuration:", err) } + // Clean paths + if _, err := os.Stat(cfg.SettingsFile); os.IsNotExist(err) { + fd, err := os.Create(cfg.SettingsFile) + if err != nil { + log.Fatal("Unable to create settings file:", err) + } + + fd.Write([]byte{'{', '}'}) + + fd.Close() + } + + if _, err := os.Stat(cfg.TracksDir); os.IsNotExist(err) { + if err := os.Mkdir(cfg.TracksDir, 0755); err != nil { + log.Fatal("Unable to create tracks directory:", err) + } + } + if _, err := os.Stat(cfg.GongsDir); os.IsNotExist(err) { + if err := os.Mkdir(cfg.GongsDir, 0755); err != nil { + log.Fatal("Unable to create gongs directory:", err) + } + } + if _, err := os.Stat(cfg.ActionsDir); os.IsNotExist(err) { + if err := os.Mkdir(cfg.ActionsDir, 0755); err != nil { + log.Fatal("Unable to create actions directory:", err) + } + } + if _, err := os.Stat(cfg.RoutinesDir); os.IsNotExist(err) { + if err := os.Mkdir(cfg.RoutinesDir, 0755); err != nil { + log.Fatal("Unable to create routines directory:", err) + } + } + + // Start app a := NewApp(cfg) go a.Start() diff --git a/model/action.go b/model/action.go index 1cf73d9..9c1ce81 100644 --- a/model/action.go +++ b/model/action.go @@ -65,6 +65,11 @@ func LoadAction(path string) (string, string, error) { } func LoadActions(cfg *config.Config) (actions []*Action, err error) { + actionsDir, err := filepath.Abs(cfg.ActionsDir) + if err != nil { + return nil, err + } + err = filepath.Walk(cfg.ActionsDir, func(path string, d fs.FileInfo, err error) error { if d.Mode().IsRegular() { hash := sha512.Sum512([]byte(path)) @@ -81,11 +86,15 @@ func LoadActions(cfg *config.Config) (actions []*Action, err error) { return nil } + if apath, err := filepath.Abs(path); err == nil { + path = apath + } + actions = append(actions, &Action{ Id: hash[:], Name: name, Description: description, - Path: path, + Path: strings.TrimPrefix(path, actionsDir+"/"), Enabled: d.Mode().Perm()&0111 != 0, }) }