images/haveImageID: use docker images, not inspect

Docker inspect will not exit 0 if no version of the image is available
on the machine, leading to the method failing. There is no flag to
adjust this in docker inspect, the only solution would be to parse the
error message but that would be fragile.

Instead, we opt to use `docker images` which has a very similar output
with the exception that it will list all tags available for that image
and that it will not fail on absent images.
This commit is contained in:
aviau 2021-01-22 09:19:29 -05:00 committed by Alexandre Viau
parent a039b4fee5
commit bc4e49734b
1 changed files with 18 additions and 3 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package images // import "perkeep.org/internal/images"
import (
"bufio"
"bytes"
"errors"
"fmt"
@ -66,12 +67,26 @@ func haveDocker() bool {
}
func haveImageID(name, id string) (ok bool, err error) {
out, err := exec.Command("docker", "inspect", "-f", "{{.Id}}", name).Output()
out, err := exec.Command(
"docker",
"images",
"--quiet",
"--no-trunc",
"--format",
"{{.ID}}",
name,
).Output()
if err != nil {
return false, err
}
have := strings.TrimSpace(string(out))
return have == id, nil
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
line := strings.TrimSpace((scanner.Text()))
if line == id {
return true, nil
}
}
return false, nil
}
// Pull retrieves the docker image with 'docker pull'.