From 9b68510eaa77a2e9bd2b5bbdae7150d336fd135a Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 19 Aug 2018 12:06:40 -0700
Subject: [PATCH] Basic config and logging

---
 config-sample.json |   6 +++
 glrdomon.go        | 108 +++++++++++++++++++++++++++++++++++++++++++++
 glrdomon_test.go   |  16 +++++++
 3 files changed, 130 insertions(+)
 create mode 100644 config-sample.json

diff --git a/config-sample.json b/config-sample.json
new file mode 100644
index 0000000..ed3c51e
--- /dev/null
+++ b/config-sample.json
@@ -0,0 +1,6 @@
+{
+  "debug-dest": "os.Stdout",
+  "debug": false,
+  "api-key": "",
+  "threshold": 3600
+}
diff --git a/glrdomon.go b/glrdomon.go
index 7905807..6b69772 100644
--- a/glrdomon.go
+++ b/glrdomon.go
@@ -1,5 +1,113 @@
 package main
 
+import (
+	"encoding/json"
+	"flag"
+	"io/ioutil"
+	"log"
+	"os"
+	"os/signal"
+	"path/filepath"
+	"syscall"
+)
+
+type config struct {
+	DebugDest string `json:"debug-dest"`
+	Debug     bool   `json:"debug"`
+	ApiKey    string `json:"api-key"`
+	Threshold int    `json:"threshold"`
+}
+
+var (
+	configPath string
+
+	logger    *log.Logger
+	debugDest string
+	debug     bool
+)
+
+func init() {
+	flag.StringVar(&configPath, "config", "./config.json", "Path to configuration file")
+	flag.Parse()
+
+	cfgPathValid := validatePath(&configPath)
+	if !cfgPathValid {
+		usage()
+	}
+
+	configFile, err := ioutil.ReadFile(configPath)
+	if err != nil {
+		usage()
+	}
+
+	cfg := config{}
+	if err = json.Unmarshal(configFile, &cfg); err != nil {
+		usage()
+	}
+
+	debugDest = cfg.DebugDest
+	debug = cfg.Debug
+
+	setUpLogger()
+}
+
 func main() {
+	logger.Printf("Starting GitLab Runner monitoring with config %s", configPath)
+
+	sig := make(chan os.Signal, 1)
+	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
+
+	// TODO: something!
+	if debug {
+		logger.Println("Test")
+	}
+
+	caughtSig := <-sig
+
+	logger.Printf("Stopping, got signal %s", caughtSig)
+}
+
+func setUpLogger() {
+	logOpts := log.Ldate | log.Ltime | log.LUTC | log.Lshortfile
+
+	if debugDest == "os.Stdout" {
+		logger = log.New(os.Stdout, "DEBUG: ", logOpts)
+	} else {
+		path, err := filepath.Abs(debugDest)
+		if err != nil {
+			logger.Fatal(err)
+		}
+
+		logFile, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		logger = log.New(logFile, "", logOpts)
+	}
+}
+
+func validatePath(path *string) bool {
+	if len(*path) <= 1 {
+		return false
+	}
+
+	var err error
+	*path, err = filepath.Abs(*path)
+
+	if err != nil {
+		logger.Printf("Error: %s", err.Error())
+		return false
+	}
+
+	if _, err = os.Stat(*path); os.IsNotExist(err) {
+		return false
+	}
+
+	return true
+}
 
+func usage() {
+	flag.Usage()
+	os.Exit(3)
 }
diff --git a/glrdomon_test.go b/glrdomon_test.go
index 20adc55..4c11ca1 100644
--- a/glrdomon_test.go
+++ b/glrdomon_test.go
@@ -1,3 +1,19 @@
 package main
 
 import "testing"
+
+func TestValidatePath(t *testing.T) {
+	emptyString := ""
+	notValid := validatePath(&emptyString)
+
+	if notValid == true {
+		t.Error("Empty path shouldn't validate")
+	}
+
+	sampleConfig := "./config-sample.json"
+	valid := validatePath(&sampleConfig)
+
+	if valid != true {
+		t.Error("Couldn't validate path to sample config")
+	}
+}
-- 
GitLab