2023-05-01 14:38:00 +00:00
|
|
|
package diagnostics
|
|
|
|
|
|
|
|
import (
|
2023-09-25 15:24:17 +00:00
|
|
|
"encoding/json"
|
2023-05-01 14:38:00 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2023-08-31 08:04:27 +00:00
|
|
|
func SetupFlagsAccess(ctx *cli.Context, metricsMux *http.ServeMux) {
|
2023-09-25 15:24:17 +00:00
|
|
|
metricsMux.HandleFunc("/flags", func(w http.ResponseWriter, r *http.Request) {
|
2023-05-01 14:38:00 +00:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
2023-09-25 15:24:17 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
flags := map[string]interface{}{}
|
|
|
|
|
|
|
|
ctxFlags := map[string]struct{}{}
|
|
|
|
|
|
|
|
for _, flagName := range ctx.FlagNames() {
|
|
|
|
ctxFlags[flagName] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, flag := range ctx.App.Flags {
|
|
|
|
name := flag.Names()[0]
|
|
|
|
value := ctx.Value(name)
|
2023-05-01 14:38:00 +00:00
|
|
|
|
2023-09-25 15:24:17 +00:00
|
|
|
switch typed := value.(type) {
|
|
|
|
case string:
|
|
|
|
if typed == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case cli.UintSlice:
|
|
|
|
value = typed.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
var usage string
|
|
|
|
|
|
|
|
if docFlag, ok := flag.(cli.DocGenerationFlag); ok {
|
|
|
|
usage = docFlag.GetUsage()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, inCtx := ctxFlags[name]
|
|
|
|
|
|
|
|
flags[name] = struct {
|
|
|
|
Value interface{} `json:"value,omitempty"`
|
|
|
|
Usage string `json:"usage,omitempty"`
|
|
|
|
Default bool `json:"default"`
|
|
|
|
}{
|
|
|
|
Value: value,
|
|
|
|
Usage: usage,
|
|
|
|
Default: !inCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(flags)
|
|
|
|
})
|
2023-05-01 14:38:00 +00:00
|
|
|
}
|