git » chasquid » commit e9d62e0

chasquid: Do a Chdir into the configuration directory

author Alberto Bertogli
2016-09-18 05:07:59 UTC
committer Alberto Bertogli
2016-10-09 23:51:04 UTC
parent f375f276afb90b08a2863c56a0f6b8393b472389

chasquid: Do a Chdir into the configuration directory

In some cases, it's be useful to have references to directories relative
to the configuration itself.

So this patch makes chasquid do a Chdir into it, so we can assume it in
the rest of the code and config.

chasquid.go +10 -2
test/util/lib.sh +2 -0
test/util/test-mda +5 -5

diff --git a/chasquid.go b/chasquid.go
index 0b176c5..f58337f 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -60,6 +60,13 @@ func main() {
 		glog.Fatalf("Error reading config")
 	}
 
+	// Change to the config dir.
+	// This allow us to use relative paths for configuration directories.
+	// It also can be useful in unusual environments and for testing purposes,
+	// where paths inside the configuration itself could be relative, and this
+	// fixes the point of reference.
+	os.Chdir(*configDir)
+
 	if conf.MonitoringAddress != "" {
 		glog.Infof("Monitoring HTTP server listening on %s",
 			conf.MonitoringAddress)
@@ -74,7 +81,8 @@ func main() {
 	s.MaxDataSize = conf.MaxDataSizeMb * 1024 * 1024
 
 	// Load domains.
-	domainDirs, err := ioutil.ReadDir(*configDir + "/domains/")
+	// They live inside the config directory, so the relative path works.
+	domainDirs, err := ioutil.ReadDir("domains/")
 	if err != nil {
 		glog.Fatalf("Error in glob: %v", err)
 	}
@@ -85,7 +93,7 @@ func main() {
 		glog.Infof("Domain config paths:")
 		for _, info := range domainDirs {
 			name := info.Name()
-			dir := filepath.Join(*configDir, "domains", name)
+			dir := filepath.Join("domains", name)
 			loadDomain(s, name, dir)
 		}
 	}
diff --git a/test/util/lib.sh b/test/util/lib.sh
index e6ddb7e..0dfd423 100644
--- a/test/util/lib.sh
+++ b/test/util/lib.sh
@@ -27,8 +27,10 @@ function generate_cert() {
 function chasquid() {
 	# HOSTALIASES: so we "fake" hostnames.
 	# PATH: so chasquid can call test-mda without path issues.
+	# MDA_DIR: so our test-mda knows where to deliver emails.
 	HOSTALIASES=${TBASE}/hosts \
 	PATH=${UTILDIR}:${PATH} \
+	MDA_DIR=${TBASE}/.mail \
 		go run ${TBASE}/../../chasquid.go "$@"
 }
 
diff --git a/test/util/test-mda b/test/util/test-mda
index 617e393..4a1775f 100755
--- a/test/util/test-mda
+++ b/test/util/test-mda
@@ -2,13 +2,13 @@
 
 set -e
 
-mkdir -p .mail
+mkdir -p ${MDA_DIR}
 
 # TODO: use flock to lock the file, to prevent atomic writes.
-echo "From ${1}" >> .mail/.tmp-${1}
-cat >> .mail/.tmp-${1}
+echo "From ${1}" >> ${MDA_DIR}/.tmp-${1}
+cat >> ${MDA_DIR}/.tmp-${1}
 X=$?
-if [ -e .mail/.tmp-${1} ]; then
-	mv .mail/.tmp-${1} .mail/${1}
+if [ -e ${MDA_DIR}/.tmp-${1} ]; then
+	mv ${MDA_DIR}/.tmp-${1} ${MDA_DIR}/${1}
 fi
 exit $X