Streamline config and make config file variable
This commit is contained in:
parent
523ff9d2db
commit
013cddc157
@ -5,8 +5,10 @@ tmp_dir = "tmp"
|
|||||||
[build]
|
[build]
|
||||||
args_bin = [
|
args_bin = [
|
||||||
"-articles tmp/articles",
|
"-articles tmp/articles",
|
||||||
|
"-config tmp/config.toml",
|
||||||
"-desc 'Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität'",
|
"-desc 'Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität'",
|
||||||
"-domain localhost",
|
"-domain localhost",
|
||||||
|
"-firebase tmp/firebase.json",
|
||||||
"-key tmp/key.gob",
|
"-key tmp/key.gob",
|
||||||
"-link https://distrikt-ni-st.de",
|
"-link https://distrikt-ni-st.de",
|
||||||
"-log tmp/cpolis.log",
|
"-log tmp/cpolis.log",
|
||||||
|
@ -3,6 +3,7 @@ package backend
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -12,6 +13,7 @@ import (
|
|||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ArticleDir string
|
ArticleDir string
|
||||||
|
ConfigFile string
|
||||||
DBName string
|
DBName string
|
||||||
Description string
|
Description string
|
||||||
Domain string
|
Domain string
|
||||||
@ -30,56 +32,72 @@ type Config struct {
|
|||||||
func newConfig() *Config {
|
func newConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
ArticleDir: "/var/www/cpolis/articles",
|
ArticleDir: "/var/www/cpolis/articles",
|
||||||
|
ConfigFile: "/etc/cpolis/config.toml",
|
||||||
DBName: "cpolis",
|
DBName: "cpolis",
|
||||||
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||||
KeyFile: "/var/www/cpolis/cpolis.key",
|
KeyFile: "/var/www/cpolis/cpolis.key",
|
||||||
LogFile: "/var/log/cpolis.log",
|
LogFile: "/var/log/cpolis.log",
|
||||||
PDFDir: "/var/www/cpolis/pdfs",
|
PDFDir: "/var/www/cpolis/pdfs",
|
||||||
|
Port: ":8080",
|
||||||
PicsDir: "/var/www/cpolis/pics",
|
PicsDir: "/var/www/cpolis/pics",
|
||||||
RSSFile: "/var/www/cpolis/cpolis.rss",
|
RSSFile: "/var/www/cpolis/cpolis.rss",
|
||||||
WebDir: "/var/www/cpolis/web",
|
WebDir: "/var/www/cpolis/web",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) readFile() error {
|
func mkDir(path string, perm fs.FileMode) (string, error) {
|
||||||
cfgFile, err := filepath.Abs(os.Getenv("HOME") + "/.config/cpolis/config.toml")
|
var err error
|
||||||
|
|
||||||
|
stringSlice := strings.Split(path, "/")
|
||||||
|
name := stringSlice[len(stringSlice)-1]
|
||||||
|
|
||||||
|
path, err = filepath.Abs(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting absolute path for config file: %v", err)
|
return "", fmt.Errorf("error finding absolute path for %v directory: %v", name, err)
|
||||||
|
}
|
||||||
|
if err = os.MkdirAll(path, perm); err != nil {
|
||||||
|
return "", fmt.Errorf("error creating %v directory: %v", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat(cfgFile)
|
return path, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mkFile(path string, filePerm, dirPerm fs.FileMode) (string, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
path, err = filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error finding absolute path for %v: %v", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stringSlice := strings.Split(path, "/")
|
||||||
|
_, err = os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
fileStrings := strings.Split(cfgFile, "/")
|
dir := strings.Join(stringSlice[:len(stringSlice)-1], "/")
|
||||||
|
if err = os.MkdirAll(dir, dirPerm); err != nil {
|
||||||
dir := strings.Join(fileStrings[0:len(fileStrings)-1], "/")
|
return "", fmt.Errorf("error creating %v: %v", dir, err)
|
||||||
if err = os.MkdirAll(dir, 0755); err != nil {
|
|
||||||
return fmt.Errorf("error creating config directory: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileName := fileStrings[len(fileStrings)-1]
|
fileName := stringSlice[len(stringSlice)-1]
|
||||||
file, err := os.Create(dir + "/" + fileName)
|
file, err := os.Create(dir + "/" + fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error creating config file: %v", err)
|
return "", fmt.Errorf("error creating %v: %v", fileName, err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err = file.Chmod(0644); err != nil {
|
if err = file.Chmod(filePerm); err != nil {
|
||||||
return fmt.Errorf("error setting permissions for config file: %v", err)
|
return "", fmt.Errorf("error setting permissions for %v: %v", fileName, err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err = toml.DecodeFile(cfgFile, c)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error reading config file: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) handleCliArgs() error {
|
func (c *Config) handleCliArgs() error {
|
||||||
|
var port int
|
||||||
var err error
|
var err error
|
||||||
port := 8080
|
|
||||||
|
|
||||||
flag.StringVar(&c.ArticleDir, "articles", c.ArticleDir, "articles directory")
|
flag.StringVar(&c.ArticleDir, "articles", c.ArticleDir, "articles directory")
|
||||||
|
flag.StringVar(&c.ConfigFile, "config", c.ConfigFile, "config file")
|
||||||
flag.StringVar(&c.DBName, "db", c.DBName, "DB name")
|
flag.StringVar(&c.DBName, "db", c.DBName, "DB name")
|
||||||
flag.StringVar(&c.Description, "desc", c.Description, "channel description")
|
flag.StringVar(&c.Description, "desc", c.Description, "channel description")
|
||||||
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
||||||
@ -95,58 +113,114 @@ func (c *Config) handleCliArgs() error {
|
|||||||
flag.IntVar(&port, "port", port, "port")
|
flag.IntVar(&port, "port", port, "port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
c.ArticleDir, err = filepath.Abs(c.ArticleDir)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error finding absolute path for articles directory: %v", err)
|
|
||||||
}
|
|
||||||
if err = os.MkdirAll(c.ArticleDir, 0755); err != nil {
|
|
||||||
return fmt.Errorf("error creating articles directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 directory: %v", err)
|
|
||||||
}
|
|
||||||
if err = os.MkdirAll(c.PDFDir, 0755); err != nil {
|
|
||||||
return fmt.Errorf("error creating pdfs directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.PicsDir, err = filepath.Abs(c.PicsDir)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error finding absolute path for pics directory: %v", err)
|
|
||||||
}
|
|
||||||
if err = os.MkdirAll(c.PicsDir, 0755); err != nil {
|
|
||||||
return fmt.Errorf("error creating pics directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Port = fmt.Sprint(":", port)
|
c.Port = fmt.Sprint(":", port)
|
||||||
|
c.ConfigFile, err = mkFile(c.ConfigFile, 0600, 0700)
|
||||||
c.RSSFile, err = filepath.Abs(c.RSSFile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error finding absolute path for RSS file: %v", err)
|
return fmt.Errorf("error setting up file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.WebDir, err = filepath.Abs(c.WebDir)
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) handleFile(configFile string) error {
|
||||||
|
_, err := toml.DecodeFile(configFile, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error finding absolute path for web directory: %v", err)
|
return fmt.Errorf("error reading config file: %v", err)
|
||||||
}
|
}
|
||||||
if err = os.MkdirAll(c.WebDir, 0755); err != nil {
|
|
||||||
return fmt.Errorf("error creating web directory: %v", err)
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) setupConfig(cliConfig *Config) error {
|
||||||
|
var err error
|
||||||
|
defaultConfig := newConfig()
|
||||||
|
|
||||||
|
if cliConfig.ArticleDir != defaultConfig.ArticleDir {
|
||||||
|
c.ArticleDir = cliConfig.ArticleDir
|
||||||
|
}
|
||||||
|
c.ArticleDir, err = mkDir(c.ArticleDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.DBName != defaultConfig.DBName {
|
||||||
|
c.DBName = cliConfig.DBName
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.Description != defaultConfig.Description {
|
||||||
|
c.Description = cliConfig.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.Domain != defaultConfig.Domain {
|
||||||
|
c.Domain = cliConfig.Domain
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.FirebaseKey != defaultConfig.FirebaseKey {
|
||||||
|
c.FirebaseKey = cliConfig.FirebaseKey
|
||||||
|
}
|
||||||
|
c.FirebaseKey, err = mkFile(c.FirebaseKey, 0600, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.KeyFile != defaultConfig.KeyFile {
|
||||||
|
c.KeyFile = cliConfig.KeyFile
|
||||||
|
}
|
||||||
|
c.KeyFile, err = mkFile(c.KeyFile, 0600, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.Link != defaultConfig.Link {
|
||||||
|
c.Link = cliConfig.Link
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.LogFile != defaultConfig.LogFile {
|
||||||
|
c.LogFile = cliConfig.LogFile
|
||||||
|
}
|
||||||
|
c.LogFile, err = mkFile(c.LogFile, 0600, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.PDFDir != defaultConfig.PDFDir {
|
||||||
|
c.PDFDir = cliConfig.PDFDir
|
||||||
|
}
|
||||||
|
c.PDFDir, err = mkDir(c.PDFDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.PicsDir != defaultConfig.PicsDir {
|
||||||
|
c.PicsDir = cliConfig.PicsDir
|
||||||
|
}
|
||||||
|
c.PicsDir, err = mkDir(c.PicsDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.Port != defaultConfig.Port {
|
||||||
|
c.Port = cliConfig.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.RSSFile != defaultConfig.RSSFile {
|
||||||
|
c.RSSFile = cliConfig.RSSFile
|
||||||
|
}
|
||||||
|
c.RSSFile, err = mkFile(c.RSSFile, 0600, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.Title != defaultConfig.Title {
|
||||||
|
c.Title = cliConfig.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
if cliConfig.WebDir != defaultConfig.WebDir {
|
||||||
|
c.WebDir = cliConfig.WebDir
|
||||||
|
}
|
||||||
|
c.WebDir, err = mkDir(c.WebDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error setting up directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -154,13 +228,19 @@ func (c *Config) handleCliArgs() error {
|
|||||||
|
|
||||||
func HandleConfig() (*Config, error) {
|
func HandleConfig() (*Config, error) {
|
||||||
config := newConfig()
|
config := newConfig()
|
||||||
|
cliConfig := newConfig()
|
||||||
|
|
||||||
if err := config.readFile(); err != nil {
|
if err := cliConfig.handleCliArgs(); err != nil {
|
||||||
return nil, fmt.Errorf("error reading config file: %v", err)
|
return nil, fmt.Errorf("error handling cli arguments: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.handleCliArgs(); err != nil {
|
err := config.handleFile(cliConfig.ConfigFile)
|
||||||
return nil, fmt.Errorf("error handling cli arguments: %v", err)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error reading configuration file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = config.setupConfig(cliConfig); err != nil {
|
||||||
|
return nil, fmt.Errorf("error setting up files: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user