From b49b780da21d1966538a1039e4845cc8b8083a47 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Apr 2025 21:11:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=20nil=20?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E7=9A=84=E6=A3=80=E6=9F=A5=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E5=9C=A8=E6=89=A7=E8=A1=8C=E5=AE=B9=E5=99=A8=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=89=8D=E5=AE=B9=E5=99=A8=E5=B7=B2=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/exec/exec.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/service/exec/exec.go b/service/exec/exec.go index a267275..1192b3d 100644 --- a/service/exec/exec.go +++ b/service/exec/exec.go @@ -80,6 +80,9 @@ func NewDockerContainer(memory int64, cpu int64) (*dockerContainer, error) { // CreateAndStartContainer creates a container, sets resource limits, and starts it // The container runs `tail -f /dev/null` to keep running, waiting for new commands func (dc *dockerContainer) CreateAndStartContainer() error { + if dc == nil { + return fmt.Errorf("no container found, please create and start the container first") + } // 如果容器已经存在,则跳过创建 if dc.ContainerID != "" { fmt.Println("Container already exists, skipping creation.") @@ -131,7 +134,7 @@ func (dc *dockerContainer) CreateAndStartContainer() error { // ExecCommandInContainer executes a command in the existing container and returns its output func (dc *dockerContainer) ExecCommandInContainer(command string) (string, error) { - if dc.ContainerID == "" { + if dc == nil { return "", fmt.Errorf("no container found, please create and start the container first") } @@ -173,6 +176,9 @@ func (dc *dockerContainer) ExecCommandInContainer(command string) (string, error // RestartAndCleanContainer stops, removes, and recreates the container func (dc *dockerContainer) RestartAndCleanContainer() error { + if dc == nil { + return nil + } if dc.ContainerID == "" { return fmt.Errorf("no container to restart") } @@ -218,6 +224,9 @@ func (dc *dockerContainer) RestartAndCleanContainer() error { // RemoveContainer stops and removes the container func (dc *dockerContainer) RemoveContainer() error { + if dc == nil { + return nil + } if dc.ContainerID == "" { return fmt.Errorf("no container to remove") }