Squash merge audio-player-component into main
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// Package library manages the music library and its configuration.
|
||||
package library
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Config holds Library config data.
|
||||
type Config struct {
|
||||
DirectoryPath Directory `form:"Directory" schema:"directory,required"`
|
||||
}
|
||||
|
||||
// Directory represents a filesystem path to a music directory.
|
||||
type Directory string
|
||||
|
||||
// NewConfig creates a validated library configuration.
|
||||
func NewConfig(dir string) (*Config, error) {
|
||||
config := &Config{
|
||||
DirectoryPath: Directory(dir),
|
||||
}
|
||||
if err := config.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("validation error for new library config: %w", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Validate checks that the configured directory exists.
|
||||
func (c *Config) Validate() error {
|
||||
if len(c.DirectoryPath) != 0 {
|
||||
dirInfo, err := os.Stat(string(c.DirectoryPath))
|
||||
if err != nil {
|
||||
return fmt.Errorf("problem getting info on library dir (%s): %w", c.DirectoryPath, err)
|
||||
}
|
||||
|
||||
if !dirInfo.IsDir() {
|
||||
return fmt.Errorf("%s is not a directory", c.DirectoryPath)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user