Skip to content
Snippets Groups Projects
Commit 9b68510e authored by Erick Hitter's avatar Erick Hitter
Browse files

Basic config and logging

parent 1ee0492c
No related branches found
No related tags found
1 merge request!1Initial release
Pipeline #171 failed with stages
in 11 minutes and 24 seconds
{
"debug-dest": "os.Stdout",
"debug": false,
"api-key": "",
"threshold": 3600
}
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)
}
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")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment