Normalize paths

This commit is contained in:
nemunaire 2022-10-04 19:04:57 +02:00
parent 392d0133f7
commit 8f64a349ec
2 changed files with 44 additions and 1 deletions

34
main.go
View File

@ -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()

View File

@ -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,
})
}