Use flag values instead of hardcoded file paths

This commit is contained in:
Thomas Schwery 2017-02-06 18:33:51 +01:00
parent f5658a1de1
commit 3ebfbd5edf

16
main.go
View file

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"errors" "errors"
"flag"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -65,12 +66,16 @@ var (
} }
// Some random string, random for each request // Some random string, random for each request
oauthStateString = "random" oauthStateString = "random"
cfgFilePath = flag.String("config", "configuration.toml", "Path to the configuration file.")
cacheDBPath = flag.String("cache", "cache.db", "Path to the cache sqlite database.")
) )
var ctx = context.Background() var ctx = context.Background()
var messages = make(chan *oauth2.Token) var messages = make(chan *oauth2.Token)
func main() { func main() {
flag.Parse()
logfilename := fmt.Sprintf("log-%s.log", time.Now().Format("2006-01-02_15_04_05")) logfilename := fmt.Sprintf("log-%s.log", time.Now().Format("2006-01-02_15_04_05"))
f, err := os.OpenFile(logfilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) f, err := os.OpenFile(logfilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
@ -117,16 +122,15 @@ func main() {
} }
func readConfigurationFile() error { func readConfigurationFile() error {
cfgFilePath := "configuration.toml"
var config configurationFile var config configurationFile
if _, err := toml.DecodeFile(cfgFilePath, &config); err != nil { if _, err := toml.DecodeFile(*cfgFilePath, &config); err != nil {
log.Println(err) log.Println(err)
return err return err
} }
if config.ClientID == "" || config.ClientSecret == "" { if config.ClientID == "" || config.ClientSecret == "" {
log.Println("Missing ClientID or ClientSecret configuration option in configuration file" + cfgFilePath + ".") log.Println("Missing ClientID or ClientSecret configuration option in configuration file" + *cfgFilePath + ".")
return errors.New("Missing ClientID or ClientSecret in configuration file " + cfgFilePath + ".") return errors.New("Missing ClientID or ClientSecret in configuration file " + *cfgFilePath + ".")
} }
googleOauthConfig.ClientID = config.ClientID googleOauthConfig.ClientID = config.ClientID
@ -486,7 +490,7 @@ func handleAuthenticationCallback(w http.ResponseWriter, r *http.Request) {
} }
func getCachedData(key string) string { func getCachedData(key string) string {
db, err := sql.Open("sqlite3", "./foo.db") db, err := sql.Open("sqlite3", *cacheDBPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -515,7 +519,7 @@ func getCachedData(key string) string {
} }
func putCacheData(key string, value string) { func putCacheData(key string, value string) {
db, err := sql.Open("sqlite3", "./foo.db") db, err := sql.Open("sqlite3", *cacheDBPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }