/
utils.go
63 lines (50 loc) · 1.29 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package utils
import (
"encoding/json"
"os/exec"
"strings"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/domain/entities/types"
)
type PodInfo struct {
ID string
InfraID string
Name string
}
func PodInformation(name string) (*PodInfo, error) {
var podmanVersion types.SystemVersionReport
podmanVersionReport, err := exec.Command("podman", "version", "-f", "json").Output()
if err != nil {
return nil, err
}
err = json.Unmarshal(podmanVersionReport, &podmanVersion)
if err != nil {
return nil, err
}
podInspectResult, err := exec.Command("podman", "pod", "inspect", name).Output()
if err != nil {
return nil, err
}
if strings.Index(podmanVersion.Client.Version, "5") == 0 {
var podInspect []entities.PodInspectReport
err = json.Unmarshal(podInspectResult, &podInspect)
if err != nil {
return nil, err
}
return &PodInfo{
Name: podInspect[0].Name,
InfraID: podInspect[0].InfraContainerID[0:12],
ID: podInspect[0].ID[0:12],
}, nil
}
var podInspect entities.PodInspectReport
err = json.Unmarshal(podInspectResult, &podInspect)
if err != nil {
return nil, err
}
return &PodInfo{
Name: podInspect.Name,
InfraID: podInspect.InfraContainerID[0:12],
ID: podInspect.ID[0:12],
}, nil
}