98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package mnt
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/go-errors/errors"
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"gensokyo.cafe/xmnt/msg"
|
|
"gensokyo.cafe/xmnt/util"
|
|
)
|
|
|
|
type Preset struct {
|
|
Name string `yaml:"name"`
|
|
Type string `yaml:"type"`
|
|
Path string `yaml:"path"`
|
|
MountPoint string `yaml:"mountpoint"`
|
|
|
|
AuthCmd string `yaml:"auth_cmd"` // e.g. for loading the encryption key
|
|
MountCmd string `yaml:"mount_cmd"`
|
|
CheckCmd string `yaml:"check_cmd"` // check if the mounting was successful
|
|
|
|
UnmountCmd string `yaml:"unmount_cmd"`
|
|
UnAuthCmd string `yaml:"unauth_cmd"` // e.g. for unloading the encryption key
|
|
UnmountCheckCmd string `yaml:"unmount_check_cmd"`
|
|
}
|
|
|
|
func (p *Preset) String() string {
|
|
return p.Type + ": " + p.Name + " (" + p.Path + ")"
|
|
}
|
|
|
|
func readPreset(path string) (*Preset, error) {
|
|
buf, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, 0)
|
|
}
|
|
var preset *Preset
|
|
if err = yaml.Unmarshal(buf, &preset); err != nil {
|
|
return nil, errors.Wrap(err, 0)
|
|
}
|
|
|
|
if preset.Name == "" {
|
|
preset.Name = filepath.Base(path)[:len(filepath.Base(path))-4] // remove .yml
|
|
}
|
|
return preset, nil
|
|
}
|
|
|
|
func ReadPresets() ([]*Preset, error) {
|
|
cfgDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return nil, errors.WrapPrefix(err, "cannot obtain user config dir", 0)
|
|
}
|
|
|
|
presetsDir := filepath.Join(cfgDir, "xmnt", "presets")
|
|
dirExist, err := util.DirExists(presetsDir)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, 0)
|
|
}
|
|
if !dirExist {
|
|
return nil, nil
|
|
}
|
|
|
|
var ret []*Preset
|
|
|
|
entries, err := os.ReadDir(presetsDir)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, 0)
|
|
}
|
|
for _, entry := range entries {
|
|
if !entry.Type().IsRegular() || !strings.HasSuffix(entry.Name(), ".yml") {
|
|
continue
|
|
}
|
|
preset, err := readPreset(filepath.Join(presetsDir, entry.Name()))
|
|
if err != nil {
|
|
msg.Errorf("Failed to read preset %s: %v", entry.Name(), err)
|
|
continue
|
|
}
|
|
ret = append(ret, preset)
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func match(s string) ([]*Preset, error) {
|
|
presets, err := ReadPresets()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, 0)
|
|
}
|
|
for _, p := range presets {
|
|
// for presets, only return exact match.
|
|
if p.Name == s {
|
|
return []*Preset{p}, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|