~hristoast/mousikofidi

55c59341835a8b128438c40858377eab91c3c801 — Hristos N. Triantafillou a month ago e4373b7
Support using a specific cfg file or none at all
4 files changed, 76 insertions(+), 29 deletions(-)

M cfg/main.go
M cli/args.go
M cli/main.go
M cmd/mousikofidi/main.go
M cfg/main.go => cfg/main.go +44 -23
@@ 58,6 58,7 @@ var Version string = "0.0.0"
type Config struct {
	AllowDelete  bool
	AllowSave    bool
	Cfg          string
	Debug        bool
	Dirs         []string
	FaviconPath  string


@@ 79,6 80,7 @@ func DefaultConfig() *Config {
	return &Config{
		AllowDelete:  true,
		AllowSave:    true,
		Cfg:          defaultCfgPath,
		Debug:        false,
		Dirs:         []string{},
		FaviconPath:  "/static/img/fidi.png",


@@ 118,7 120,7 @@ func (c *Config) ListenAddress() string {
// Validate returns an error for any problem found in the Config.
func (c *Config) Validate() []error {
	var errs []error
	if c.Dirs == nil {
	if len(c.Dirs) == 0 {
		errs = append(errs, errors.New("no media dirs have been configured"))
	}
	if c.PlaylistDir == "" {


@@ 175,39 177,58 @@ func CfgDir() string {
	return filepath.Join(os.Getenv("HOME"), ".config", "mousikofidi")
}

func Read() (*Config, error) {
	defaultDir := CfgDir()
	info, err := os.Stat(defaultDir)
	if err != nil {
		log.Printf("creating dir path %s...", defaultDir)
		err = os.MkdirAll(defaultDir, 0755)
func Read(path string) (*Config, error) {
	cfgFile := defaultCfgPath
	usingDefault := true
	if path != "" {
		cfgFile = path
		usingDefault = false
	}

	if usingDefault {
		defaultDir := CfgDir()
		info, err := os.Stat(defaultDir)
		if err != nil {
			return nil, err
			log.Printf("creating dir path %s...", defaultDir)
			err = os.MkdirAll(defaultDir, 0755)
			if err != nil {
				return nil, err
			}
		}
	}

	if info != nil && !info.IsDir() {
		return nil, fmt.Errorf("%s exists but is not a directory", CfgDir())
	}
		if info != nil && !info.IsDir() {
			return nil, fmt.Errorf("%s exists but is not a directory", CfgDir())
		}

	info, err = os.Stat(defaultCfgPath)
	if err != nil {
		log.Printf("creating default cfg: %s...", defaultCfgPath)
		home := os.Getenv("HOME")
		now := time.Now().Format("2006-01-02 15:04:05")
		cfgData := fmt.Sprintf(defaultCfg, now, home, home, home)
		err = os.WriteFile(defaultCfgPath, []byte(cfgData), 0644)
		info, err = os.Stat(defaultCfgPath)
		if err != nil {
			log.Printf("creating default cfg: %s...", defaultCfgPath)
			home := os.Getenv("HOME")
			now := time.Now().Format("2006-01-02 15:04:05")
			cfgData := fmt.Sprintf(defaultCfg, now, home, home, home)
			err = os.WriteFile(defaultCfgPath, []byte(cfgData), 0644)
			if err != nil {
				return nil, err
			}
		}

		if info != nil && info.IsDir() {
			return nil, fmt.Errorf("%s exists but is a directory", defaultCfgPath)
		}

	} else {
		info, err := os.Stat(cfgFile)
		if err != nil {
			return nil, err
		}
	}

	if info != nil && info.IsDir() {
		return nil, fmt.Errorf("%s exists but is a directory", defaultCfgPath)
		if info != nil && info.IsDir() {
			return nil, fmt.Errorf("%s exists but is a directory", cfgFile)
		}
	}

	var c tomlCfg
	md, err := toml.DecodeFile(defaultCfgPath, &c)
	md, err := toml.DecodeFile(cfgFile, &c)
	if err != nil {
		return nil, err
	}

M cli/args.go => cli/args.go +4 -2
@@ 6,23 6,25 @@ import (
	"git.sr.ht/~hristoast/mousikofidi/cfg"
)

//TODO: cfg file arg
//TODO: manpage arg
//TODO: tutorial arg
type args struct {
	NoAllowDelete  bool `arg:"--no-allow-delete" help:"globally disable deletion of playlists."`
	NoAllowSave    bool `arg:"--no-allow-save" help:"globally disable saving of playlists."`
	NoCfg          bool `arg:"--no-cfg" help:"don't use a config file."`
	NoHolidays     bool `arg:"--no-holidays" help:"disable holiday icons."`
	NoIcons        bool `arg:"--no-icons" help:"disable FontAwesome icons."`
	NoKbdControls  bool `arg:"--no-kbd-controls" help:"disable audio player keyboard controls."`
	NoShowCoverArt bool `arg:"--no-show-cover-art" help:"globally disable rendering cover art."`
	NoShowErrors   bool `arg:"--no-show-errors" help:"globally disable showing errors in the UI."`

	Cfg         string   `arg:"--cfg" help:"path to a cfg file to use for configuration."`
	Debug       bool     `arg:"--debug" help:"print extra log messages in the console and render them in the UI."`
	Dirs        []string `arg:"--dirs" help:"a list of library directories." placeholder:"DIR1, ..."`
	Dirs        []string `arg:"--library-dirs" help:"a list of library directories." placeholder:"DIR1, ..."`
	FaviconPath string   `arg:"--favicon-path" help:"path to a file to use as favicon." placeholder:"PATH"`
	Host        string   `arg:"--host" help:"Listen host."`
	LogoPath    string   `arg:"--logo-path" help:"path to a file to use as logo." placeholder:"PATH"`
	PlaylistDir string   `arg:"--playlist-dir" help:"Path to the directory for playlist files." placeholder:"PATH"`
	Port        string   `arg:"--port" help:"Listen host."`
	SiteName    string   `arg:"--site-name" help:"string to display as the site name." placeholder:"NAME"`
	Theme       string   `arg:"--theme" help:"Name of the default theme." placeholder:"NAME"`

M cli/main.go => cli/main.go +15 -0
@@ 29,6 29,11 @@ func applyArgs(a args, c *cfg.Config) {
		c.AllowSave = false
	}

	if a.NoCfg {
		log.Println("Disabling usage of a cfg file from cli...")
		c.Cfg = ""
	}

	if a.NoHolidays {
		log.Println("Disabling ui.holidays from cli...")
		c.Holidays = false


@@ 54,6 59,11 @@ func applyArgs(a args, c *cfg.Config) {
		c.ShowErrors = false
	}

	if a.Cfg != "" {
		log.Println("Using a config file specified from cli...")
		c.Cfg = a.Cfg
	}

	if a.Dirs != nil {
		log.Println("Applying library.dirs from cli...")
		c.Dirs = a.Dirs


@@ 74,6 84,11 @@ func applyArgs(a args, c *cfg.Config) {
		c.LogoPath = a.LogoPath
	}

	if a.PlaylistDir != "" {
		log.Println("Applying playlist.dir from cli...")
		c.PlaylistDir = a.PlaylistDir
	}

	if a.Port != "" {
		log.Println("Applying server.port from cli...")
		c.Port = a.Port

M cmd/mousikofidi/main.go => cmd/mousikofidi/main.go +13 -4
@@ 8,13 8,22 @@ import (
	"git.sr.ht/~hristoast/mousikofidi/http/router"
)

//TODO: an internal log package
func main() {
	var err error
	conf := cfg.NewConfig()
	c := cfg.DefaultConfig()
	parsed := cli.ParseArgs()
	conf, err := cfg.Read()
	if err != nil {
		log.Printf("FATAL ERROR: %s", err)
		log.Fatal("Cannot start due to a fatal error")

	if parsed.Cfg == "" {
		c.Cfg = ""

	} else {
		conf, err = cfg.Read(parsed.Cfg)
		if err != nil {
			log.Printf("FATAL ERROR: %s", err)
			log.Fatal("Cannot start due to a fatal error")
		}
	}

	cfg.MergeConfigs(c, conf, parsed)