remove duplicate codes (#1376)

Co-authored-by: zzjin <zzjin@users.noreply.github.com>
Signed-off-by: fengxsong <fengxsong@outlook.com>
This commit is contained in:
fengxsong
2022-07-22 10:53:29 +08:00
committed by GitHub
co-authored by zzjin
parent 943c7388a3
commit 3a79cc7a20
+10 -26
View File
@@ -32,39 +32,27 @@ import (
type ClusterService struct {
}
func (d *ClusterService) Create(name string, image string) (*types.ClusterManifest, error) {
data := exec.BashEval("buildah containers --json")
infos, err := listContainer(data)
if err != nil {
func (s *ClusterService) Create(name string, image string) (*types.ClusterManifest, error) {
if err := s.Delete(name); err != nil {
return nil, err
}
for _, info := range infos {
if info.Containername == name {
cmd := fmt.Sprintf("buildah unmount %s && buildah rm %s", info.Containername, info.Containername)
if err = exec.Cmd("bash", "-c", cmd); err != nil {
return nil, err
}
}
}
cmd := fmt.Sprintf(" buildah from --pull=never --name %s %s && buildah mount %s ", name, image, name)
err = exec.Cmd("bash", "-c", cmd)
if err != nil {
cmd := fmt.Sprintf("buildah from --pull=never --name %s %s && buildah mount %s ", name, image, name)
if err := exec.Cmd("bash", "-c", cmd); err != nil {
return nil, err
}
return d.Inspect(name)
return s.Inspect(name)
}
func (*ClusterService) Delete(name string) error {
data := exec.BashEval("buildah containers --json")
infos, err := listContainer(data)
func (s *ClusterService) Delete(name string) error {
infos, err := s.List()
if err != nil {
return err
}
logger.Debug("current container names is: %v", name)
for _, info := range infos {
if info.Containername == name {
cmd := fmt.Sprintf("buildah unmount %s && buildah rm %s", info.Containername, info.Containername)
cmd := fmt.Sprintf("buildah unmount %s && buildah rm %s", info.Containername, info.Containername)
if err = exec.Cmd("bash", "-c", cmd); err != nil {
return err
}
@@ -84,11 +72,7 @@ func (*ClusterService) Inspect(name string) (*types.ClusterManifest, error) {
func (*ClusterService) List() ([]types.ClusterInfo, error) {
data := exec.BashEval("buildah containers --json")
infos, err := listContainer(data)
if err != nil {
return nil, err
}
return infos, nil
return listContainer(data)
}
func inspectContainer(data string) (*types.ClusterManifest, error) {