avoid a second map in ExecAllocator

Instead, write the args list directly.
This commit is contained in:
Daniel Martí 2019-03-20 17:57:49 +00:00
parent 6fb5264bbd
commit 2ca3ea3591

View File

@ -61,26 +61,34 @@ type ExecAllocator struct {
} }
func (p *ExecAllocator) Allocate(ctx context.Context) (*Browser, error) { func (p *ExecAllocator) Allocate(ctx context.Context) (*Browser, error) {
removeDir := false var args []string
var cmd *exec.Cmd
// TODO: figure out a nicer way to do this
flags := make(map[string]interface{})
for name, value := range p.initFlags { for name, value := range p.initFlags {
flags[name] = value switch value := value.(type) {
case string:
args = append(args, fmt.Sprintf("--%s=%s", name, value))
case bool:
if value {
args = append(args, fmt.Sprintf("--%s", name))
}
default:
return nil, fmt.Errorf("invalid exec pool flag")
}
} }
dataDir, ok := flags["user-data-dir"].(string) removeDir := false
dataDir, ok := p.initFlags["user-data-dir"].(string)
if !ok { if !ok {
tempDir, err := ioutil.TempDir("", "chromedp-runner") tempDir, err := ioutil.TempDir("", "chromedp-runner")
if err != nil { if err != nil {
return nil, err return nil, err
} }
flags["user-data-dir"] = tempDir args = append(args, "--user-data-dir="+tempDir)
dataDir = tempDir dataDir = tempDir
removeDir = true removeDir = true
} }
args = append(args, "--remote-debugging-port=0")
var cmd *exec.Cmd
p.wg.Add(1) p.wg.Add(1)
go func() { go func() {
<-ctx.Done() <-ctx.Done()
@ -95,22 +103,6 @@ func (p *ExecAllocator) Allocate(ctx context.Context) (*Browser, error) {
p.wg.Done() p.wg.Done()
}() }()
flags["remote-debugging-port"] = "0"
args := []string{}
for name, value := range flags {
switch value := value.(type) {
case string:
args = append(args, fmt.Sprintf("--%s=%s", name, value))
case bool:
if value {
args = append(args, fmt.Sprintf("--%s", name))
}
default:
return nil, fmt.Errorf("invalid exec pool flag")
}
}
cmd = exec.CommandContext(ctx, p.execPath, args...) cmd = exec.CommandContext(ctx, p.execPath, args...)
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {