mirror of
https://github.com/Alexey71/opera-proxy.git
synced 2026-05-14 06:30:59 +00:00
config file support
This commit is contained in:
@@ -184,6 +184,7 @@ func parse_args() *CLIArgs {
|
|||||||
flag.StringVar(&args.serverSelectionTestURL, "server-selection-test-url", "https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js",
|
flag.StringVar(&args.serverSelectionTestURL, "server-selection-test-url", "https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js",
|
||||||
"URL used for download benchmark by fastest server selection policy")
|
"URL used for download benchmark by fastest server selection policy")
|
||||||
flag.Int64Var(&args.serverSelectionDLLimit, "server-selection-dl-limit", 0, "restrict amount of downloaded data per connection by fastest server selection")
|
flag.Int64Var(&args.serverSelectionDLLimit, "server-selection-dl-limit", 0, "restrict amount of downloaded data per connection by fastest server selection")
|
||||||
|
flag.Func("config", "read configuration from file with space-separated keys and values", readConfig)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if args.country == "" {
|
if args.country == "" {
|
||||||
arg_fail("Country can't be empty string.")
|
arg_fail("Country can't be empty string.")
|
||||||
@@ -586,6 +587,50 @@ func retryPolicy(retries int, retryInterval time.Duration, logger *clog.CondLogg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readConfig(filename string) error {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to open config file %q: %w", filename, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
r := csv.NewReader(f)
|
||||||
|
r.Comma = ' '
|
||||||
|
r.Comment = '#'
|
||||||
|
r.FieldsPerRecord = -1
|
||||||
|
r.TrimLeadingSpace = true
|
||||||
|
r.ReuseRecord = true
|
||||||
|
for {
|
||||||
|
record, err := r.Read()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("configuration file parsing failed: %w", err)
|
||||||
|
}
|
||||||
|
switch len(record) {
|
||||||
|
case 0:
|
||||||
|
continue
|
||||||
|
case 1:
|
||||||
|
if err := flag.Set(record[0], "true"); err != nil {
|
||||||
|
line, _ := r.FieldPos(0)
|
||||||
|
return fmt.Errorf("error parsing config file %q at line %d (%#v): %w", filename, line, record, err)
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
if err := flag.Set(record[0], record[1]); err != nil {
|
||||||
|
line, _ := r.FieldPos(0)
|
||||||
|
return fmt.Errorf("error parsing config file %q at line %d (%#v): %w", filename, line, record, err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
unified := strings.Join(record[1:], " ")
|
||||||
|
if err := flag.Set(record[0], unified); err != nil {
|
||||||
|
line, _ := r.FieldPos(0)
|
||||||
|
return fmt.Errorf("error parsing config file %q at line %d (%#v): %w", filename, line, record, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func version() string {
|
func version() string {
|
||||||
bi, ok := debug.ReadBuildInfo()
|
bi, ok := debug.ReadBuildInfo()
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user