package cd.casic.module.execute.docker.service.impl;

import cd.casic.module.execute.docker.DockerClientFactory;
import cd.casic.module.execute.docker.service.IDockerPingService;
import com.github.dockerjava.api.DockerClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Locale;
import java.util.Optional;

/**
 * @description: TODO
 * @author: mianbin
 * @date: 2025/5/26 14:32
 * @version: 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DockerPingService implements IDockerPingService {

    private final DockerClientFactory dockerClientFactory;

    @Override
    public Boolean ping(String clientId) {
        DockerClient dockerClient = Optional.of(StringUtils.hasText(clientId) ? clientId : "DEFAULT")
                .map(id -> id.toUpperCase(Locale.ROOT))
                .map(dockerClientFactory::getdockerClient)
                .orElseThrow(() -> new RuntimeException("客户端不存在"));
        dockerClient.pingCmd().exec();
        return Boolean.TRUE;
    }
}
