summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-27 02:00:50 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-27 02:10:25 -0400
commitc5cf0e19e83029e70a9d294b6a7d9ee973cc0000 (patch)
treeff2a48b8832c30b820b620b878dd76e9f8aba97a
parentdaa48539f5ad8b444637f948b98996d35852b1e8 (diff)
downloadgetwtxt-c5cf0e19e83029e70a9d294b6a7d9ee973cc0000.tar.gz
flags for config file/type, added to -h
-rw-r--r--getwtxt.yml16
-rw-r--r--init.go59
2 files changed, 54 insertions, 21 deletions
diff --git a/getwtxt.yml b/getwtxt.yml
index 58b0af1..460ff4e 100644
--- a/getwtxt.yml
+++ b/getwtxt.yml
@@ -6,8 +6,12 @@
 ##      /etc/                                              ##
 ##      /usr/local/etc/                                    ##
 ##                                                         ##
-##  The first location found wins. The file must be        ##
-##  called getwtxt.yml                                     ##
+##  The first location found wins. The default filename    ##
+##  is getwtxt.yml. This can be configured via command     ##
+##  line flags:                                            ##
+##                                                         ##
+##  For "myconfig.json"                                    ##
+##    ./getwtxt -config myconfig -type json                ##
 ##                                                         ##
 #############################################################
 
@@ -57,15 +61,15 @@ Instance:
   # Your instance's web address.
   URL: "https://twtxt.example.com"
 
-  # Your name.
-  OwnerName: "foo barrington"
+  # Your name. 
+  OwnerName: "Anonymous Microblogger"
   
   # Your email address.
-  Email: "foo@barrington.ext"
+  Email: "nobody@knows"
 
   # A short description of your instance, or your site.
   # This is shown at the top of the default web page
   # below your instance's name.
-  Description: "This is the twtxt registry for the tildeverse network of public unix servers."
+  Description: "A fast, resilient twtxt registry server written in Go!"
 
 
diff --git a/init.go b/init.go
index 6fd6bc8..3f3440c 100644
--- a/init.go
+++ b/init.go
@@ -19,8 +19,10 @@ const getwtxt = "0.1"
 
 // command line flags
 var (
-	flagVersion *bool = pflag.BoolP("version", "v", false, "Display version information, then exit")
-	flagHelp    *bool = pflag.BoolP("help", "h", false, "")
+	flagVersion  *bool   = pflag.BoolP("version", "v", false, "Display version information, then exit.")
+	flagHelp     *bool   = pflag.BoolP("help", "h", false, "Display the help screen")
+	flagConfFile *string = pflag.StringP("config", "c", "getwtxt", "The name of the configuration file without an extension.")
+	flagConfType *string = pflag.StringP("type", "t", "yml", "The filetype of the configuration file.")
 )
 
 // config object
@@ -80,8 +82,8 @@ func checkFlags() {
 
 func initConfig() {
 
-	viper.SetConfigName("getwtxt")
-	viper.SetConfigType("yml")
+	viper.SetConfigName(*flagConfFile)
+	viper.SetConfigType(*flagConfType)
 	viper.AddConfigPath(".")
 	viper.AddConfigPath("/usr/local/getwtxt")
 	viper.AddConfigPath("/etc")
@@ -89,18 +91,18 @@ func initConfig() {
 
 	log.Printf("Loading configuration ...\n")
 	if err := viper.ReadInConfig(); err != nil {
-		log.Printf("Error reading config file: %v\n", err)
+		log.Printf("%v\n", err)
 		log.Printf("Using defaults ...\n")
+	} else {
+		// separate thread to watch for config file changes.
+		// will log event then run rebindConfig()
+		viper.WatchConfig()
+		viper.OnConfigChange(func(e fsnotify.Event) {
+			log.Printf("Config file change detected. Reloading...\n")
+			rebindConfig()
+		})
 	}
 
-	// separate thread to watch for config file changes.
-	// will log event then run rebindConfig()
-	viper.WatchConfig()
-	viper.OnConfigChange(func(e fsnotify.Event) {
-		log.Printf("Config file change detected. Reloading...\n")
-		rebindConfig()
-	})
-
 	viper.SetDefault("ListenPort", 9001)
 	viper.SetDefault("LogFile", "getwtxt.log")
 	viper.SetDefault("DatabasePath", "getwtxt.db")
@@ -108,29 +110,43 @@ func initConfig() {
 	viper.SetDefault("ReCacheInterval", "1h")
 	viper.SetDefault("DatabasePushInterval", "5m")
 
+	viper.SetDefault("Instance.SiteName", "getwtxt")
+	viper.SetDefault("Instance.OwnerName", "Anonymous Microblogger")
+	viper.SetDefault("Instance.Email", "nobody@knows")
+	viper.SetDefault("Instance.URL", "https://twtxt.example.com")
+	viper.SetDefault("Instance.Description", "A fast, resilient twtxt registry server written in Go!")
+
 	confObj.Mu.Lock()
+
 	confObj.Port = viper.GetInt("ListenPort")
 	confObj.LogFile = viper.GetString("LogFile")
+
 	confObj.DBPath = viper.GetString("DatabasePath")
 	log.Printf("Using database: %v\n", confObj.DBPath)
+
 	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
 	if confObj.StdoutLogging {
 		log.Printf("Logging to stdout\n")
 	} else {
 		log.Printf("Logging to %v\n", confObj.LogFile)
 	}
+
 	confObj.CacheInterval = viper.GetDuration("StatusFetchInterval")
 	log.Printf("User status fetch interval: %v\n", confObj.CacheInterval)
+
 	confObj.DBInterval = viper.GetDuration("DatabasePushInterval")
 	log.Printf("Database push interval: %v\n", confObj.DBInterval)
+
 	confObj.LastCache = time.Now()
 	confObj.LastPush = time.Now()
 	confObj.Version = getwtxt
+
 	confObj.Instance.Name = viper.GetString("Instance.SiteName")
 	confObj.Instance.URL = viper.GetString("Instance.URL")
 	confObj.Instance.Owner = viper.GetString("Instance.OwnerName")
 	confObj.Instance.Mail = viper.GetString("Instance.Email")
 	confObj.Instance.Desc = viper.GetString("Instance.Description")
+
 	confObj.Mu.Unlock()
 
 }
@@ -181,16 +197,19 @@ func rebindConfig() {
 
 	// reassign values to the config object
 	confObj.Mu.Lock()
+
 	confObj.LogFile = viper.GetString("LogFile")
 	confObj.DBPath = viper.GetString("DatabasePath")
 	confObj.StdoutLogging = viper.GetBool("StdoutLogging")
 	confObj.CacheInterval = viper.GetDuration("StatusFetchInterval")
 	confObj.DBInterval = viper.GetDuration("DatabasePushInterval")
+
 	confObj.Instance.Name = viper.GetString("Instance.SiteName")
 	confObj.Instance.URL = viper.GetString("Instance.URL")
 	confObj.Instance.Owner = viper.GetString("Instance.OwnerName")
 	confObj.Instance.Mail = viper.GetString("Instance.Email")
 	confObj.Instance.Desc = viper.GetString("Instance.Description")
+
 	confObj.Mu.Unlock()
 
 	// reinitialize logging
@@ -271,8 +290,18 @@ func titleScreen() {
 
 func helpScreen() {
 	fmt.Printf(`
-              Help File
+              getwtxt Help
+
+Command line options:
+    -h               Print this help screen.
+    -v               Print the version information and quit.
+    -c [--config]    Name of an alternate configuration file
+                       to use. Do not include the file extention,
+                       such as ".yml". Must be in the expected
+                       locations.
+    -t [--type]      The file type / extension of the config file.
+                       json, yml, etc.
+
 
-  Sections:
 `)
 }