diff --git a/cmd/goread/main.go b/cmd/goread/main.go index d5b2535..a513305 100644 --- a/cmd/goread/main.go +++ b/cmd/goread/main.go @@ -14,17 +14,17 @@ import ( ) // parseCmdLine parses the command line arguments -func parseCmdLine() (configPath string, backend string, testColors bool, err error) { +func parseCmdLine() (urlPath string, backend string, testColors bool, err error) { // Create the flagset backendPtr := flag.String("backend", "cache", "The backend to use for the config file") - configPtr := flag.String("config", "", "The path to the config file") + urlPathPtr := flag.String("url_path", "", "The path to the url file") testColorsPtr := flag.Bool("colors", false, "Test the colorscheme") // Parse the flags flag.Parse() backend = *backendPtr - configPath = *configPtr + urlPath = *urlPathPtr testColors = *testColorsPtr // Check if the backend is valid @@ -33,18 +33,18 @@ func parseCmdLine() (configPath string, backend string, testColors bool, err err } // Check if the config path is valid and writeable - configDir := filepath.Dir(configPath) + configDir := filepath.Dir(urlPath) if unix.Access(configDir, unix.W_OK) != nil { return "", "", false, fmt.Errorf("config file directory is not writable: %s", configDir) } // Return the default path - return configPath, backend, testColors, nil + return urlPath, backend, testColors, nil } func main() { // Parse the command line arguments - configPath, backend, testColors, err := parseCmdLine() + urlPath, backend, testColors, err := parseCmdLine() if err != nil { fmt.Println(err) os.Exit(1) @@ -57,12 +57,12 @@ func main() { } // Create the config - cfg, err := config.New(backend, configPath, "goread") + cfg, err := config.New(backend, urlPath) if err != nil { fmt.Println(err) return } - defer cfg.Getbackend().Close() + defer cfg.Close() // Create the main model model := model.New(cfg.Getbackend()) diff --git a/internal/config/config.go b/internal/config/config.go index fab4ef4..adaf209 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -20,65 +20,53 @@ const ( // Config is the configuration for the program type Config struct { - backend backend.Backend - configPath string - urlPath string + backend backend.Backend + urlPath string } -// NewConfig returns a new Config -func New(backend string, configPath string, urlPath string) (Config, error) { - // If the config path is not supplied, use the default - if configPath == "" { - configDir, err := os.UserConfigDir() - if err != nil { - return Config{}, err - } - - configPath = filepath.Join(configDir, "goread/config.json") - } +// New returns a new Config +func New(backendType string, urlPath string) (Config, error) { + // Create a new config + config := Config{} - // If the url path is not supplied, use the default + // If the config path is not supplied, use the default if urlPath == "" { + // Get the default config path configDir, err := os.UserConfigDir() if err != nil { return Config{}, err } - urlPath = filepath.Join(configDir, "goread/urls.json") + // Create the config path + config.urlPath = filepath.Join(configDir, "goread", "urls.yml") } - // Detect the backend - switch backend { + // Determine the backend + var backend backend.Backend + switch backendType { case BackendFake: - // Return the fake backend - return Config{ - backend: fake.New(), - configPath: configPath, - urlPath: urlPath, - }, nil - + backend = fake.New() case BackendWeb: - // Return the web backend - return Config{ - backend: web.New(), - configPath: configPath, - urlPath: urlPath, - }, nil - + backend = web.New() case BackendCache: - // Return the cache backend - return Config{ - backend: cache.New(), - configPath: configPath, - urlPath: urlPath, - }, nil + backend = cache.New() default: - // No backend was found - return Config{}, fmt.Errorf("Unknown backend: %s", backend) + return Config{}, fmt.Errorf("invalid backend type: %s", backendType) } + + // Set the backend + config.backend = backend + + // Return the config + return config, nil } // Getbackend returns the backend func (c Config) Getbackend() backend.Backend { return c.backend } + +// Close closes the backend +func (c Config) Close() error { + return c.backend.Close() +}