Skip to content

Commit

Permalink
refactor: improved the config initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jan 3, 2023
1 parent d863ad1 commit bed0f40
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 48 deletions.
16 changes: 8 additions & 8 deletions cmd/goread/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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())
Expand Down
68 changes: 28 additions & 40 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit bed0f40

Please sign in to comment.