diff --git a/cmd/gost/main.go b/cmd/gost/main.go index 7efdcde..2c2b18e 100644 --- a/cmd/gost/main.go +++ b/cmd/gost/main.go @@ -39,6 +39,7 @@ var ( apiAddr string metricsAddr string reload time.Duration + showConfig bool ) func init() { @@ -102,6 +103,7 @@ func init() { flag.StringVar(&apiAddr, "api", "", "api service address") flag.StringVar(&metricsAddr, "metrics", "", "metrics service address") flag.DurationVar(&reload, "R", 0, "auto reload period (e.g. 30s, 1m)") + flag.BoolVar(&showConfig, "P", false, "print config only") flag.Parse() if printVersion { diff --git a/cmd/gost/program.go b/cmd/gost/program.go index a6a9fb4..47630fc 100644 --- a/cmd/gost/program.go +++ b/cmd/gost/program.go @@ -2,7 +2,9 @@ package main import ( "context" + "encoding/json" "errors" + "fmt" "net/http" "os" "os/signal" @@ -23,6 +25,7 @@ import ( metrics "github.com/go-gost/x/metrics/service" "github.com/go-gost/x/registry" "github.com/judwhite/go-svc" + "gopkg.in/yaml.v3" ) type program struct { @@ -90,6 +93,17 @@ func (p *program) run(cfg *config.Config) error { svc.Serve() }() } + if showConfig { + if outputFormat == "json" { + marshal, _ := json.Marshal(cfg) + fmt.Println(string(marshal)) + } else { + marshal, _ := yaml.Marshal(cfg) + fmt.Println(string(marshal)) + } + os.Exit(0) + return nil + } if p.srvApi != nil { p.srvApi.Close()