From 9deb71e0824becc7e9ff23d5649fac3a08be9edb Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 29 May 2023 17:07:39 +0200 Subject: [PATCH] allow to use a configuration file parse .capture.ini add information os start --- README.md | 11 ++++++++++- config.go | 36 ++++++++++++++++++++++++++++++++---- go.mod | 2 ++ go.sum | 2 ++ main.go | 1 + 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9ee3d7e..68bdfc7 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Forked from [ofabricio/capture](https://github.com/ofabricio/capture). ## Running ``` -./capture -url=https://example.com/ +./capture -url=https://example.com/ -port 9000 -dashboard 9001 -captures 16 ``` #### Settings @@ -21,6 +21,15 @@ Forked from [ofabricio/capture](https://github.com/ofabricio/capture). | `-captures` | Set how many captures to show in the dashboard. Default: *16* | +You can create a file named `.capture.ini` and set the configuration inside: + +``` +url = https://example.com/ +port = 9000 +dashboard = 9001 +captures = 16 +``` + ## Using If you set your base url as `http://example.com/api`, now `http://localhost:9000` points to that diff --git a/config.go b/config.go index 73b5647..c571ebe 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,9 @@ package main import ( "flag" + "fmt" + "gopkg.in/ini.v1" + "os" ) // Config has all the configuration parsed from the command line. @@ -14,11 +17,36 @@ type Config struct { // ReadConfig reads the arguments from the command line. func ReadConfig() Config { - targetURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the url you want to proxy") - proxyPort := flag.String("port", "9000", "Set the proxy port") - dashboardPort := flag.String("dashboard", "9001", "Set the dashboard port") - maxCaptures := flag.Int("captures", 16, "Set how many captures to show in the dashboard") + defaultTargetURL := "https://jsonplaceholder.typicode.com" + defaultProxyPort := "9000" + defaultDashboardPort := "9001" + defaultMaxCaptures := 16 + + configFile := ".capture.ini" + + if _, err := os.Stat(configFile); err == nil { + cfg, err := ini.Load(configFile) + + if err != nil { + fmt.Printf("Fail to read file %s: %v", configFile, err) + os.Exit(1) + } + + section := cfg.Section("") + + defaultTargetURL = section.Key("url").MustString(defaultTargetURL) + defaultProxyPort = section.Key("port").MustString(defaultProxyPort) + defaultDashboardPort = section.Key("dashboard").MustString(defaultDashboardPort) + defaultMaxCaptures = section.Key("captures").MustInt(defaultMaxCaptures) + } + + targetURL := flag.String("url", defaultTargetURL, "Required. Set the url you want to proxy") + proxyPort := flag.String("port", defaultProxyPort, "Set the proxy port") + dashboardPort := flag.String("dashboard", defaultDashboardPort, "Set the dashboard port") + maxCaptures := flag.Int("captures", defaultMaxCaptures, "Set how many captures to show in the dashboard") + flag.Parse() + return Config{ TargetURL: *targetURL, ProxyPort: *proxyPort, diff --git a/go.mod b/go.mod index d33b670..baa223a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/ofabricio/capture go 1.16 + +require gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index e69de29..a8937af 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/main.go b/main.go index b7dee43..7dac2f0 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ var dashboardHTML []byte func main() { cfg := ReadConfig() + fmt.Printf("Target is %s", cfg.TargetURL) fmt.Printf("\nListening on http://localhost:%s", cfg.ProxyPort) fmt.Printf("\nDashboard on http://localhost:%s", cfg.DashboardPort) fmt.Println()