func parseCommandline() (cfg lib.Config, err error) { var ( mappingDefs, rewriteDefs string sslAllowInsecure bool noLogging bool showVersion bool configFile string ) flag.StringVar(&mappingDefs, "mappings", "", "mapping definitions, format: local=remote,[local=remote,...]") flag.StringVar(&rewriteDefs, "rewrite", "", "comma-separated list of regexes indetifying routes whose response will be rewritten") flag.BoolVar(&sslAllowInsecure, "allow-insecure", false, "accept insecure upstream connections") flag.BoolVar(&noLogging, "no-logging", false, "disable logging via x-go-repro-log headers") flag.StringVar(&configFile, "config", "", "read YAML config from file (all other options are ignored)") flag.BoolVar(&showVersion, "version", false, "display version") flag.Usage = func() { fmt.Fprint(os.Stdout, "usage: go-repro [options]\n\n") flag.PrintDefaults() } flag.Parse() if showVersion { fmt.Printf("version: %s\n", lib.Version()) os.Exit(0) } if configFile != "" { var yamlConfig YamlConfig yamlConfig, err = UnmarshalYamlConfigFile(configFile) if err == nil { cfg, err = yamlConfig.createReproConfig() } } else { cfg = lib.NewConfig() cfg.SetSSLAllowInsecure(sslAllowInsecure) cfg.SetNoLogging(noLogging) err = addMappings(mappingDefs, &cfg) if err == nil { err = addRewrites(rewriteDefs, &cfg) } } return }
func (c *YamlConfig) createReproConfig() (cfg lib.Config, err error) { cfg = lib.NewConfig() for _, mapping := range c.Mappings { err = cfg.AddMapping(mapping.Local, mapping.Remote) if err != nil { return } } for _, rewritePattern := range c.Rewrites { err = cfg.AddRewriteRoute(rewritePattern) if err != nil { return } } cfg.SetSSLAllowInsecure(c.AllowInsecure) cfg.SetNoLogging(c.NoLogging) return }