-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
45 lines (37 loc) · 912 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Package config manage sqly configuration
package config
import (
"os"
"path/filepath"
"github.com/caarlos0/env/v6"
)
// Config is sqly configuration.
type Config struct {
HistoryDBPath string `env:"SQLY_HISTORY_DB_PATH"`
}
// NewConfig return *Config.
func NewConfig() (*Config, error) {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
return nil, err
}
if err := cfg.CreateDir(); err != nil {
return nil, err
}
if cfg.HistoryDBPath == "" {
cfg.HistoryDBPath = filepath.Join(cfg.Dir(), "history.db")
}
return &cfg, nil
}
// Dir return configuration directory path.
func (c *Config) Dir() string {
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(os.Getenv("HOME"), ".config", "sqly")
}
return filepath.Join(home, ".config", "sqly")
}
// CreateDir make configuration directory.
func (c *Config) CreateDir() error {
return os.MkdirAll(c.Dir(), 0744)
}