148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
package backend
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
)
|
|
|
|
type Config struct {
|
|
DBName string
|
|
Description string
|
|
Domain string
|
|
FirebaseKey string
|
|
KeyFile string
|
|
Link string
|
|
LogFile string
|
|
PDFDir string
|
|
PicsDir string
|
|
Port string
|
|
RSSFile string
|
|
Title string
|
|
WebDir string
|
|
}
|
|
|
|
func newConfig() *Config {
|
|
return &Config{
|
|
DBName: "cpolis",
|
|
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
|
KeyFile: "/var/www/cpolis/cpolis.key",
|
|
LogFile: "/var/log/cpolis.log",
|
|
PDFDir: "/var/www/cpolis/pdfs",
|
|
PicsDir: "/var/www/cpolis/pics",
|
|
RSSFile: "/var/www/cpolis/cpolis.rss",
|
|
WebDir: "/var/www/cpolis/web",
|
|
}
|
|
}
|
|
|
|
func (c *Config) readFile() error {
|
|
cfgFile, err := filepath.Abs(os.Getenv("HOME") + "/.config/cpolis/config.toml")
|
|
if err != nil {
|
|
return fmt.Errorf("error getting absolute path for config file: %v", err)
|
|
}
|
|
|
|
_, err = os.Stat(cfgFile)
|
|
if os.IsNotExist(err) {
|
|
fileStrings := strings.Split(cfgFile, "/")
|
|
|
|
dir := strings.Join(fileStrings[0:len(fileStrings)-1], "/")
|
|
if err = os.MkdirAll(dir, 0755); err != nil {
|
|
return fmt.Errorf("error creating config directory: %v", err)
|
|
}
|
|
|
|
fileName := fileStrings[len(fileStrings)-1]
|
|
file, err := os.Create(dir + "/" + fileName)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating config file: %v", err)
|
|
}
|
|
defer file.Close()
|
|
if err = file.Chmod(0644); err != nil {
|
|
return fmt.Errorf("error setting permissions for config file: %v", err)
|
|
}
|
|
} else {
|
|
_, err = toml.DecodeFile(cfgFile, c)
|
|
if err != nil {
|
|
return fmt.Errorf("error reading config file: %v", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) handleCliArgs() error {
|
|
var err error
|
|
port := 8080
|
|
|
|
flag.StringVar(&c.DBName, "db", c.DBName, "DB name")
|
|
flag.StringVar(&c.Description, "desc", c.Description, "channel description")
|
|
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
|
flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file")
|
|
flag.StringVar(&c.KeyFile, "key", c.KeyFile, "key file")
|
|
flag.StringVar(&c.Link, "link", c.Link, "channel Link")
|
|
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
|
|
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
|
|
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
|
|
flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file")
|
|
flag.StringVar(&c.Title, "title", c.Title, "channel title")
|
|
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
|
flag.IntVar(&port, "port", port, "port")
|
|
flag.Parse()
|
|
|
|
c.FirebaseKey, err = filepath.Abs(c.FirebaseKey)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for Firebase service account key file: %v", err)
|
|
}
|
|
|
|
c.KeyFile, err = filepath.Abs(c.KeyFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for key file: %v", err)
|
|
}
|
|
|
|
c.LogFile, err = filepath.Abs(c.LogFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for log file: %v", err)
|
|
}
|
|
|
|
c.PDFDir, err = filepath.Abs(c.PDFDir)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for pdfs dir: %v", err)
|
|
}
|
|
|
|
c.PicsDir, err = filepath.Abs(c.PicsDir)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for pics dir: %v", err)
|
|
}
|
|
|
|
c.Port = fmt.Sprint(":", port)
|
|
|
|
c.RSSFile, err = filepath.Abs(c.RSSFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for RSS file: %v", err)
|
|
}
|
|
|
|
c.WebDir, err = filepath.Abs(c.WebDir)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding absolute path for web dir: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func HandleConfig() (*Config, error) {
|
|
config := newConfig()
|
|
|
|
if err := config.readFile(); err != nil {
|
|
return nil, fmt.Errorf("error reading config file: %v", err)
|
|
}
|
|
|
|
if err := config.handleCliArgs(); err != nil {
|
|
return nil, fmt.Errorf("error handling cli arguments: %v", err)
|
|
}
|
|
|
|
return config, nil
|
|
}
|