Skip to content

Commit 31ac9a0

Browse files
GetDockerClient for valid docker api version
1 parent 3a05484 commit 31ac9a0

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

shared.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import (
6161
"golang.org/x/crypto/bcrypt"
6262

6363
"github.com/Masterminds/semver"
64+
dockerclient "github.com/docker/docker/client"
6465
)
6566

6667
var project ShuffleStorage
@@ -33713,3 +33714,49 @@ func getPrioritisedAppActions(ctx context.Context, inputApp string, maxAmount in
3371333714

3371433715
return returnActions
3371533716
}
33717+
33718+
func GetDockerClient() (*dockerclient.Client, string, error) {
33719+
ctx := context.Background()
33720+
dockerApiVersion := os.Getenv("DOCKER_API_VERSION")
33721+
cli, err := dockerclient.NewEnvClient()
33722+
if err != nil {
33723+
return nil, dockerApiVersion,err
33724+
}
33725+
33726+
_, err = cli.Info(ctx)
33727+
if err == nil {
33728+
return cli, dockerApiVersion,nil
33729+
}
33730+
33731+
if strings.Contains(err.Error(), "Minimum supported API version is") {
33732+
re := regexp.MustCompile(`Minimum supported API version is ([0-9\.]+)`)
33733+
match := re.FindStringSubmatch(err.Error())
33734+
if len(match) == 2 {
33735+
required := match[1]
33736+
os.Setenv("DOCKER_API_VERSION", required)
33737+
cli, err = dockerclient.NewEnvClient()
33738+
if err == nil {
33739+
dockerApiVersion = required
33740+
_, err = cli.Info(ctx)
33741+
return cli, dockerApiVersion, err
33742+
}
33743+
}
33744+
}
33745+
33746+
if strings.Contains(err.Error(), "Maximum supported API version is") {
33747+
re := regexp.MustCompile(`Maximum supported API version is ([0-9\.]+)`)
33748+
match := re.FindStringSubmatch(err.Error())
33749+
if len(match) == 2 {
33750+
required := match[1]
33751+
os.Setenv("DOCKER_API_VERSION", required)
33752+
cli, err = dockerclient.NewEnvClient()
33753+
if err == nil {
33754+
dockerApiVersion = required
33755+
_, err = cli.Info(ctx)
33756+
return cli, dockerApiVersion, err
33757+
}
33758+
}
33759+
}
33760+
33761+
return cli, dockerApiVersion, err
33762+
}

0 commit comments

Comments
 (0)