refactor: simplify homeDir and appConfig usage
This commit is contained in:
@@ -15,6 +15,7 @@ type Config struct {
|
||||
ArlCookie string `mapstructure:"arl_cookie"`
|
||||
SecretKey string `mapstructure:"secret_key"`
|
||||
OutputDir string `mapstructure:"output_dir"`
|
||||
HomeDir string
|
||||
}
|
||||
|
||||
func New(cfgPath string) (*Config, error) {
|
||||
@@ -49,11 +50,11 @@ func New(cfgPath string) (*Config, error) {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
cfg := &Config{HomeDir: homeDir}
|
||||
if err := viper.Unmarshal(cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||||
}
|
||||
if err := cfg.Validate(homeDir); err != nil {
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid config: %w", err)
|
||||
}
|
||||
|
||||
@@ -61,10 +62,10 @@ func New(cfgPath string) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) Validate(homeDir string) error {
|
||||
func (c *Config) Validate() error {
|
||||
if c.ArlCookie == "" {
|
||||
return fmt.Errorf("arl_cookie is not set")
|
||||
}
|
||||
@@ -75,7 +76,7 @@ func (c *Config) Validate(homeDir string) error {
|
||||
return fmt.Errorf("secret_key must be 16 bytes long")
|
||||
}
|
||||
if c.OutputDir == "" {
|
||||
c.OutputDir = filepath.Join(homeDir, "Music", "GoDeez")
|
||||
c.OutputDir = filepath.Join(c.HomeDir, "Music", "GoDeez")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -7,18 +7,18 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func EnsureAutostart() error {
|
||||
if isAutostartInstalled() || isTemporaryExecutable() {
|
||||
func EnsureAutostart(homeDir string) error {
|
||||
if isAutostartInstalled(homeDir) || isTemporaryExecutable() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return installAutostart()
|
||||
return installAutostart(homeDir)
|
||||
}
|
||||
|
||||
func isAutostartInstalled() bool {
|
||||
func isAutostartInstalled(homeDir string) bool {
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
path := filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "com.godeez.watch.plist")
|
||||
path := filepath.Join(homeDir, "Library", "LaunchAgents", "com.godeez.watch.plist")
|
||||
_, err := os.Stat(path)
|
||||
|
||||
return err == nil
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func installAutostart() error {
|
||||
func installAutostart(homeDir string) error {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -39,7 +39,7 @@ func installAutostart() error {
|
||||
</dict>
|
||||
</plist>`, exe)
|
||||
|
||||
path := filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "com.godeez.watch.plist")
|
||||
path := filepath.Join(homeDir, "Library", "LaunchAgents", "com.godeez.watch.plist")
|
||||
if err := os.WriteFile(path, []byte(plist), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -20,8 +20,7 @@ type Watcher struct {
|
||||
}
|
||||
|
||||
func New(appConfig *config.Config) *Watcher {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
logFile := filepath.Join(homeDir, ".godeez", "watcher.log")
|
||||
logFile := filepath.Join(appConfig.HomeDir, ".godeez", "watcher.log")
|
||||
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open log file: %v\n", err)
|
||||
|
||||
Reference in New Issue
Block a user