9deb71e082
parse .capture.ini add information os start
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
"os"
|
|
)
|
|
|
|
// Config has all the configuration parsed from the command line.
|
|
type Config struct {
|
|
TargetURL string
|
|
ProxyPort string
|
|
DashboardPort string
|
|
MaxCaptures int
|
|
}
|
|
|
|
// ReadConfig reads the arguments from the command line.
|
|
func ReadConfig() Config {
|
|
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,
|
|
MaxCaptures: *maxCaptures,
|
|
DashboardPort: *dashboardPort,
|
|
}
|
|
}
|