Skip to content

Commit ee449a7

Browse files
authored
docker/info use format JSON (#1008)
- use host CPU from Host property if available - this allows to use podman
1 parent 1405961 commit ee449a7

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

internal/batches/docker/info.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package docker
33
import (
44
"bytes"
55
"context"
6-
"strconv"
7-
6+
"encoding/json"
87
"github.com/sourcegraph/sourcegraph/lib/errors"
98

109
"github.com/sourcegraph/src-cli/internal/exec"
@@ -35,6 +34,13 @@ func CurrentContext(ctx context.Context) (string, error) {
3534
return name, nil
3635
}
3736

37+
type Info struct {
38+
Host struct {
39+
CPUs int `json:"cpus"`
40+
} `json:"host"` // Podman engine
41+
NCPU int `json:"NCPU"` // Docker Engine
42+
}
43+
3844
// NCPU returns the number of CPU cores available to Docker.
3945
func NCPU(ctx context.Context) (int, error) {
4046
dctx, cancel, err := withFastCommandContext(ctx)
@@ -43,18 +49,20 @@ func NCPU(ctx context.Context) (int, error) {
4349
}
4450
defer cancel()
4551

46-
args := []string{"info", "--format", "{{ .NCPU }}"}
52+
args := []string{"info", "--format", "{{ json .}}"}
4753
out, err := exec.CommandContext(dctx, "docker", args...).CombinedOutput()
4854
if errors.IsDeadlineExceeded(err) || errors.IsDeadlineExceeded(dctx.Err()) {
4955
return 0, newFastCommandTimeoutError(dctx, args...)
5056
} else if err != nil {
5157
return 0, err
5258
}
5359

54-
dcpu, err := strconv.Atoi(string(bytes.TrimSpace(out)))
55-
if err != nil {
56-
return 0, errors.Wrap(err, "parsing docker cpu count")
60+
var info Info
61+
if err := json.Unmarshal(out, &info); err != nil {
62+
return 0, err
5763
}
58-
59-
return dcpu, nil
64+
if info.NCPU > 0 {
65+
return info.NCPU, nil
66+
}
67+
return info.Host.CPUs, nil
6068
}

internal/batches/docker/info_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package docker
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
7+
"strconv"
68
"testing"
79
"time"
810

@@ -88,7 +90,7 @@ func Test_NCPU(t *testing.T) {
8890
assert.Zero(t, ncpu)
8991
var terr *fastCommandTimeoutError
9092
assert.ErrorAs(t, err, &terr)
91-
assert.Equal(t, []string{"info", "--format", "{{ .NCPU }}"}, terr.args)
93+
assert.Equal(t, []string{"info", "--format", "{{ json .}}"}, terr.args)
9294
assert.Equal(t, fastCommandTimeoutDefault, terr.timeout)
9395
})
9496

@@ -118,15 +120,22 @@ func Test_NCPU(t *testing.T) {
118120
}
119121

120122
func infoSuccess(ncpu string) *expect.Expectation {
123+
var b []byte
124+
n, err := strconv.Atoi(ncpu)
125+
if err == nil {
126+
b, _ = json.Marshal(Info{NCPU: n})
127+
} else {
128+
b = []byte(ncpu)
129+
}
121130
return expect.NewLiteral(
122-
expect.Behaviour{Stdout: []byte(fmt.Sprintf("%s\n", ncpu))},
123-
"docker", "info", "--format", "{{ .NCPU }}",
131+
expect.Behaviour{Stdout: b},
132+
"docker", "info", "--format", "{{ json .}}",
124133
)
125134
}
126135

127136
func infoFailure() *expect.Expectation {
128137
return expect.NewLiteral(
129138
expect.Behaviour{ExitCode: 1},
130-
"docker", "info", "--format", "{{ .NCPU }}",
139+
"docker", "info", "--format", "{{ json .}}",
131140
)
132141
}

0 commit comments

Comments
 (0)