From ed5308076426bfbabc1e1c1b26e1d827362595d6 Mon Sep 17 00:00:00 2001 From: egvimo Date: Tue, 12 Sep 2023 08:07:03 +0000 Subject: [PATCH] feat: switch to command line flags for files --- main.go | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 9d12b45..98dcb78 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,8 @@ package main import ( - "encoding/json" "flag" + "fmt" "log" "net/http" "os" @@ -11,8 +11,15 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -type config struct { - Files []string +type arrayFlag []string + +func (i *arrayFlag) String() string { + return fmt.Sprintf("%v", *i) +} + +func (i *arrayFlag) Set(value string) error { + *i = append(*i, value) + return nil } type fileMetric struct { @@ -61,29 +68,20 @@ func (collector *fileCollector) Collect(ch chan<- prometheus.Metric) { } func main() { - var ( - conf = flag.String("config", "/etc/exporter/config.json", "Path to the config.") - addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") - ) + var files arrayFlag + var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.") + flag.Var(&files, "file", "File to export metric for.") flag.Parse() - configBytes, err := os.ReadFile(*conf) - if err != nil { - log.Fatal(err) - return - } - - var config config - err = json.Unmarshal(configBytes, &config) - if err != nil { - log.Fatal(err) + if len(files) == 0 { + log.Fatal("No files provided") return } - log.Printf("Initializing exporter for files: %v", config.Files) + log.Printf("Initializing exporter for files: %v", files) - fileCollector := newFileCollector(config.Files) + fileCollector := newFileCollector(files) reg := prometheus.NewRegistry() reg.MustRegister(fileCollector)