From 003233d60da4a2b69260642e57757bba48021ca1 Mon Sep 17 00:00:00 2001 From: r Date: Sun, 2 Jan 2022 10:52:15 +0000 Subject: [PATCH] Change config file lookup - Look for both local and global config file - Directly generate the global config file with make install --- .gitignore | 1 - INSTALL | 12 ++++++------ Makefile | 13 +++++-------- README | 4 ++-- config/config.go | 39 ++++++++++++++++++++++++--------------- main.go | 6 +++--- 6 files changed, 40 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 037bea6..516f77b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ bloat database -bloat.def.conf diff --git a/INSTALL b/INSTALL index 032f612..c73fd78 100644 --- a/INSTALL +++ b/INSTALL @@ -15,12 +15,12 @@ This will perform a system wide installation of bloat. By default, it will install the binary in /usr/local/bin and data files in /usr/local/share/bloat. You can change these paths by editing the Makefile. -3. Edit and copy the config file -Edit the generated config file to you liking and then copy it to the default -config location. Comments in the config file describe what each config value -does. For most cases, you only need to change the value of "client_website". -$ $EDITOR bloat.def.conf -# cp bloat.def.conf /etc/bloat.conf +3. Edit the config file +bloat looks for a file named bloat.conf in the working directory and +/etc/bloat in that order. You can also specify another file by using the -f +flag. Comments in the config file describe what each config value does. For +most cases, you only need to change the value of "client_website". +# $EDITOR /etc/bloat.conf 4. Create database directory Create a directory to store session information. Optionally, create a user diff --git a/Makefile b/Makefile index 4231015..1b32268 100644 --- a/Makefile +++ b/Makefile @@ -14,17 +14,11 @@ SRC=main.go \ service/*.go \ util/*.go \ -all: bloat bloat.def.conf +all: bloat bloat: $(SRC) $(TMPL) $(GO) build $(GOFLAGS) -o bloat main.go -bloat.def.conf: - sed -e "s%=database%=/var/bloat%g" \ - -e "s%=templates%=$(SHAREPATH)/templates%g" \ - -e "s%=static%=$(SHAREPATH)/static%g" \ - < bloat.conf > bloat.def.conf - install: bloat mkdir -p $(DESTDIR)$(BINPATH) \ $(DESTDIR)$(SHAREPATH)/templates \ @@ -35,6 +29,10 @@ install: bloat chmod 0644 $(DESTDIR)$(SHAREPATH)/templates/* cp -r static/* $(DESTDIR)$(SHAREPATH)/static chmod 0644 $(DESTDIR)$(SHAREPATH)/static/* + sed -e "s%=database%=/var/bloat%g" \ + -e "s%=templates%=$(SHAREPATH)/templates%g" \ + -e "s%=static%=$(SHAREPATH)/static%g" \ + < bloat.conf > /etc/bloat.conf uninstall: rm -f $(DESTDIR)$(BINPATH)/bloat @@ -42,4 +40,3 @@ uninstall: clean: rm -f bloat - rm -f bloat.def.conf diff --git a/README b/README index 9c76da1..b00592f 100644 --- a/README +++ b/README @@ -15,11 +15,11 @@ Building and Installation: Typing make will build the binary $ make -Edit the provided config file. See the bloat.conf file for more details. +Edit the default config file. See the bloat.conf file for more details. $ ed bloat.conf Run the binary -$ ./bloat -f bloat.conf +$ ./bloat You can now access the frontend at http://127.0.0.1:8080, which is the default listen address. See the INSTALL file for more details. diff --git a/config/config.go b/config/config.go index 8678f52..bbb327c 100644 --- a/config/config.go +++ b/config/config.go @@ -108,21 +108,30 @@ func Parse(r io.Reader) (c *config, err error) { return } -func ParseFile(file string) (c *config, err error) { - f, err := os.Open(file) - if err != nil { - return +func ParseFiles(files []string) (c *config, err error) { + var lastErr error + for _, file := range files { + f, err := os.Open(file) + if err != nil { + lastErr = err + if os.IsNotExist(err) { + continue + } + return nil, err + } + defer f.Close() + info, err := f.Stat() + if err != nil { + lastErr = err + return nil, err + } + if info.IsDir() { + continue + } + return Parse(f) } - defer f.Close() - - info, err := f.Stat() - if err != nil { - return + if lastErr == nil { + lastErr = errors.New("invalid config file") } - - if info.IsDir() { - return nil, errors.New("invalid config file") - } - - return Parse(f) + return nil, lastErr } diff --git a/main.go b/main.go index cac5eee..85ee6cc 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( ) var ( - configFile = "/etc/bloat.conf" + configFiles = []string{"bloat.conf", "/etc/bloat.conf"} ) func errExit(err error) { @@ -34,11 +34,11 @@ func main() { for _, opt := range opts { switch opt.Option { case 'f': - configFile = opt.Value + configFiles = []string{opt.Value} } } - config, err := config.ParseFile(configFile) + config, err := config.ParseFiles(configFiles) if err != nil { errExit(err) }