Handle gongs
This commit is contained in:
parent
5c7841fdc6
commit
b2d50972ed
7 changed files with 338 additions and 50 deletions
86
model/gong.go
Normal file
86
model/gong.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package reveil
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.nemunai.re/nemunaire/reveil/config"
|
||||
)
|
||||
|
||||
const CURRENT_GONG = "_current_gong"
|
||||
|
||||
type Gong struct {
|
||||
Id []byte `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
func currentGongPath(cfg *config.Config) string {
|
||||
return filepath.Join(cfg.GongsDir, CURRENT_GONG)
|
||||
}
|
||||
|
||||
func LoadGongs(cfg *config.Config) (gongs []*Gong, err error) {
|
||||
// Retrieve the path of the current gong
|
||||
current_gong, err := os.Readlink(currentGongPath(cfg))
|
||||
if err == nil {
|
||||
current_gong, _ = filepath.Abs(filepath.Join(cfg.GongsDir, current_gong))
|
||||
}
|
||||
|
||||
// Retrieve the list
|
||||
err = filepath.Walk(cfg.GongsDir, func(path string, d fs.FileInfo, err error) error {
|
||||
if d.Mode().IsRegular() {
|
||||
hash := sha512.Sum512([]byte(path))
|
||||
pabs, _ := filepath.Abs(path)
|
||||
gongs = append(gongs, &Gong{
|
||||
Id: hash[:63],
|
||||
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
||||
Path: path,
|
||||
Enabled: current_gong == pabs,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (g *Gong) Rename(newName string) error {
|
||||
newPath := filepath.Join(filepath.Dir(g.Path), newName+filepath.Ext(g.Path))
|
||||
|
||||
err := os.Rename(
|
||||
g.Path,
|
||||
newPath,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.Path = newPath
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gong) SetDefault(cfg *config.Config) error {
|
||||
linkpath := currentGongPath(cfg)
|
||||
os.Remove(linkpath)
|
||||
|
||||
pabs, err := filepath.Abs(g.Path)
|
||||
if err != nil {
|
||||
pabs = g.Path
|
||||
}
|
||||
|
||||
gdirabs, err := filepath.Abs(cfg.GongsDir)
|
||||
if err != nil {
|
||||
gdirabs = cfg.GongsDir
|
||||
}
|
||||
|
||||
return os.Symlink(strings.TrimPrefix(strings.TrimPrefix(pabs, gdirabs), "/"), linkpath)
|
||||
}
|
||||
|
||||
func (g *Gong) Remove() error {
|
||||
return os.Remove(g.Path)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue