diff --git a/pkg/image/binary/cluster.go b/pkg/image/binary/cluster.go index 17cb63dc4..41fc11623 100644 --- a/pkg/image/binary/cluster.go +++ b/pkg/image/binary/cluster.go @@ -58,6 +58,15 @@ func (*ClusterService) Inspect(name string) (*types.ClusterManifest, error) { return inspectContainer(data) } +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 +} + func inspectContainer(data string) (*types.ClusterManifest, error) { if data != "" { var outStruct map[string]interface{} @@ -78,24 +87,16 @@ func inspectContainer(data string) (*types.ClusterManifest, error) { return nil, errors.New("inspect output is empty") } -type ClusterInfo struct { - ID string `json:"id"` - Builder bool `json:"builder"` - Imageid string `json:"imageid"` - Imagename string `json:"imagename"` - Containername string `json:"containername"` -} - -func listContainer(data string) ([]ClusterInfo, error) { +func listContainer(data string) ([]types.ClusterInfo, error) { if data != "" { - var outStruct []ClusterInfo + var outStruct []types.ClusterInfo err := json.Unmarshal([]byte(data), &outStruct) if err != nil { return nil, errors.Wrap(err, "decode out json from list container failed") } return outStruct, nil } - return nil, errors.New("inspect output is empty") + return nil, errors.New("containers output is empty") } func NewClusterService() (types.ClusterService, error) { diff --git a/pkg/image/types/interface.go b/pkg/image/types/interface.go index 9e0667c07..fc6d075cf 100644 --- a/pkg/image/types/interface.go +++ b/pkg/image/types/interface.go @@ -43,4 +43,5 @@ type ClusterService interface { Delete(name string) error // Inspect return cluster(container) manifest Inspect(name string) (*ClusterManifest, error) + List() ([]ClusterInfo, error) } diff --git a/pkg/image/types/types.go b/pkg/image/types/types.go index 906fa1c1f..7ad8bee38 100644 --- a/pkg/image/types/types.go +++ b/pkg/image/types/types.go @@ -27,3 +27,11 @@ type ClusterManifest struct { ContainerID string MountPoint string } + +type ClusterInfo struct { + ID string `json:"id"` + Builder bool `json:"builder"` + Imageid string `json:"imageid"` + Imagename string `json:"imagename"` + Containername string `json:"containername"` +}